I have this code:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;
public class EmployeeProcessor {
public static void main(String[] args) {
Employee employee = new Employee();
employee.lastName = "Smith";
employee.firstName = "Adam";
employee.id = 123456789;
employee.salary = 50000;
try(FileOutputStream fileOutStr = new FileOutputStream("Employee.ser");
ObjectOutputStream objectOutStr = new ObjectOutputStream(fileOutStr)) {
objectOutStr.writeObject(employee);
System.out.println("An employee is externalized into the file Employee.ser");
} catch (IOException ioError){
ioError.printStackTrace();
}
}
}
But in Intellij IDEA ObjectOutputStream class is strikethrough Like this:
screenshot. When pointing mouse pointer over - this message appears: 'java.io.ObjectOutputStream' is deprecated. What does it mean?
When I run this code, IntelliJ opens "Edit Configurations" windows asking me to introduce VM options. But I leave it blank and run anyway.
IntelliJ IDEA has an intention action to annotate library classes as Deprecated using the External Annotations support. You've probably triggered this intention action by accident.
For the classes deprecated this way there supposed to be the reverse action: Deannotate, but it may not work (bug reported).
To fix it manually, find the annotations.xml file in a directory that is configured in the SDK Annotations tab and edit/remove it.
UPDATE: Deannoate action should work now, but only while inside the annotated class itself, not from its reference.
Related
I made this class in a NetBeans project and cannot figure out why the createNewFile method will not recognize it's import. NetBeans is giving the "cannot find symbol" error for that line. "createNewFile" is the only part underlined in red on that line. It also gives warning on "import java.io.File" saying that it is never used.
I've added try and catch blocks around the method but they make no difference. Got rid of them in the example below for the sake of simplicity.
import java.util.Scanner;
import java.io.File;
public class Bleh {
Scanner in = new Scanner(System.in);
User u = new User();
public void setUserName() {
System.out.print("Name: ");
u.setName(in.nextLine());
}
public void checkForAccount() {
createNewFile(u.getName());
}
}
Your import of java.io.File imports exactly that, the class itself, into your namespace. It doesn't import all of File's methods, and even if it did, there is no such method File.createNewFile(String); you need to create a File object and call the method on it:
new File(u.getName()).createNewFile();
Downloaded latest android Studio (android-studio-bundle-162.3871768-windows).
We were using com.android.sdklib.SdkManager class in our software but in latest Android Studio I'm not able to find the above mentioned class in any jar present inside the tools\lib folder.
Can anyone suggest what is the better alternative for this?
if you want to get a list of all the targets installed for knowledge, then you can just simply run the SDK manager. But since you want to call the getTargets() method, it means you need it for other purposes. check up the documentation on the android studio web page to find out if it the class you are searching for exists and the location of its jar file.
We can find the soure code of all the android classes in the below link.
https://javalibs.com/artifact/com.android.tools/sdklib?className=com.android.sdklib.tool.SdkManagerCli&source
SdkManagerCli class have equivalent method listPackages()which will list the packages.
We need to import sdklib-25.3.2.jar, repository-25.3.2.jar and common-25.3.2.jar to project.
Below is the working code for listing packages:-
import java.io.File;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.TreeSet;
import com.android.repository.Revision;
import com.android.repository.api.ConsoleProgressIndicator;
import com.android.repository.api.LocalPackage;
import com.android.repository.api.ProgressIndicator;
import com.android.repository.api.RepoManager;
import com.android.repository.impl.meta.RepositoryPackages;
import com.android.sdklib.repository.AndroidSdkHandler;
public class AndroidTesting {
public static void main(String[] args) {
listPackages();
}
private static void listPackages() {
AndroidSdkHandler mHandler = AndroidSdkHandler.getInstance(new
File("filePath")); //for eg:-sdk/platforms for API
ProgressIndicator progress = new ConsoleProgressIndicator();
RepoManager mRepoManager = mHandler.getSdkManager(progress);
mRepoManager.loadSynchronously(cacheExpirationMs, progress,
downloader, settings)(0, progress, null, null);
RepositoryPackages packages = mRepoManager.getPackages();
Collection<LocalPackage> locals = new TreeSet<LocalPackage>();
Collection<LocalPackage> localObsoletes = new
TreeSet<LocalPackage>();
for (LocalPackage local : packages.getLocalPackages().values()) {
if (local.obsolete()) {
localObsoletes.add(local);
} else {
locals.add(local);
}
Revision version = local.getVersion();
System.out.println(local.getDisplayName() + " "
+ local.getVersion() );
}
}
}
I am trying to understand GWT generators but facing few issues. I am trying to display the compile time in an app using generators and running into this error -
Rebind result 'com.example.client.Function' must be a class
Here is what i have -
This is how i am calling my generated method -
Function b = GWT.create(Function.class);
label.setText(b.getBuildTime());
gwt.xml-
<generate-with class="example.frontend.client.gin.FunctionGenerator">
<when-type-assignable class="com.example.frontend.client.gin.Function" />
</generate-with>
Function.java
package com.example.frontend.client.gin;
public interface Function{
public String getBuildTime();
}
Generator class -
package com.example.frontend.egenerator;
import java.io.PrintWriter;
import java.util.Date;
import com.google.gwt.core.ext.Generator;
import com.google.gwt.core.ext.GeneratorContext;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.core.ext.typeinfo.TypeOracle;
import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
import com.google.gwt.user.rebind.SourceWriter;
import com.example.frontend.client.gin.Function;
public class FunctionGenerator extends Generator {
private static final String IMPL_TYPE_NAME = Function.class.getSimpleName() + "Impl";
private static final String IMPL_PACKAGE_NAME = Function.class.getPackage().getName();
#Override
public String generate(final TreeLogger logger, final GeneratorContext context, final String requestedClass) throws UnableToCompleteException {
TypeOracle typeOracle = context.getTypeOracle();
JClassType functionType = typeOracle.findType(requestedClass);
assert Function.class.equals(functionType.getClass());
ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(IMPL_PACKAGE_NAME, IMPL_TYPE_NAME);
composerFactory.addImport(Function.class.getCanonicalName());
composerFactory.addImplementedInterface(Function.class.getName());
PrintWriter printWriter = context.tryCreate(logger, IMPL_PACKAGE_NAME, IMPL_TYPE_NAME);
SourceWriter sourceWriter = composerFactory.createSourceWriter(context, printWriter);
if(sourceWriter != null) {
sourceWriter.print("public String getBuildTime() {");
sourceWriter.print(" return \"" + new Date() + "\" ;");
sourceWriter.print("}");
sourceWriter.commit(logger);
}
return IMPL_PACKAGE_NAME + "." + IMPL_TYPE_NAME;
}
}
Any ideas, what I am missing?
I believe you also need to null check the PrintWriter created by tryCreate, as it may return null. On the other hand, createSourceWriter will not return null, so no need to null check that.
Your generate-with is also incorrect, at least for the sample that you have here. It should have a different package (according to your FunctionGenerator source at least), com.example.frontend.egenerator, not com.example.frontend.client.gin:
<generate-with class="com.example.frontend.egenerator.FunctionGenerator">
<when-type-assignable class="com.example.frontend.client.gin.Function" />
</generate-with>
In general, your generators should not be in the client package, if for no other reason than preventing spurious errors which slow down the compiler (and really slow down super dev mode).
Beyond that, the full log could help a lot to track down the issue, though without mapping the generator correctly there wouldn't be much of an error. Also be sure to compile with strict turned on when working on generators to ensure that the compiler fails as soon as possible and you can stop at the very first error.
With all of that said, tend to avoid new Generators at this point - they will slow down Super Dev Mode slightly (since they must be re-run every time you refresh), and they will not be supported in future versions of GWT. Annotation Processors (aka APT) are the preferred way to do this, but in your case you might also just be able to generate the class in ant or maven with a plugin.
I have this code:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;
public class EmployeeProcessor {
public static void main(String[] args) {
Employee employee = new Employee();
employee.lastName = "Smith";
employee.firstName = "Adam";
employee.id = 123456789;
employee.salary = 50000;
try(FileOutputStream fileOutStr = new FileOutputStream("Employee.ser");
ObjectOutputStream objectOutStr = new ObjectOutputStream(fileOutStr)) {
objectOutStr.writeObject(employee);
System.out.println("An employee is externalized into the file Employee.ser");
} catch (IOException ioError){
ioError.printStackTrace();
}
}
}
But in Intellij IDEA ObjectOutputStream class is strikethrough Like this:
screenshot. When pointing mouse pointer over - this message appears: 'java.io.ObjectOutputStream' is deprecated. What does it mean?
When I run this code, IntelliJ opens "Edit Configurations" windows asking me to introduce VM options. But I leave it blank and run anyway.
IntelliJ IDEA has an intention action to annotate library classes as Deprecated using the External Annotations support. You've probably triggered this intention action by accident.
For the classes deprecated this way there supposed to be the reverse action: Deannotate, but it may not work (bug reported).
To fix it manually, find the annotations.xml file in a directory that is configured in the SDK Annotations tab and edit/remove it.
UPDATE: Deannoate action should work now, but only while inside the annotated class itself, not from its reference.
I recently purchased the Book "Programming Google Glass - The Mirror API" By Eric Redmond and in the 2nd chapter we install Freemarker GRE .jar file into the project. There is a part when we have to create a method that renders a template file. I keep getting an error when trying to make a Configuration.
package com.leetinsider.leetfoodfinder;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;
import java.util.Random;
import javax.security.auth.login.Configuration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import com.sun.org.apache.xalan.internal.xsltc.compiler.Template;
public class LeetFoodFinder {
public static String getRandomCuisine()
{
String[] lunchOptions = {
"American", "Chineese", "French", "Italian", "Japenese", "Thai"
};
int choice = new Random().nextInt(lunchOptions.length);
return lunchOptions[choice];
}
public static String render(ServletContext ctx, String template, Map<String, Object> data)
throws IOException, ServletException{
Configuration config = new Configuration();
config.setServletContextForTemplateLoading(ctx, "WEB-INF/views");
config.setDefaultEncoding("UTF-8");
Template ftl = config.getTemplate(template);
try{
//use the data to render the template to the servlet output
StringWriter writer = new StringWriter();
ftl.process(data, writer);
return writer.toString();
}
catch (TemplateException e){
throw new ServletException("Problem while processing template", e);
}
}
}
It tells me that Configuration() cannot be instaniated. Is there an import that I am missing? I put the freemarker-gae2.3.2.0.jar file in the war/WEB-INF/lib directory but am not sure if there is something else I am missing.
Trying to follow along with the book but this is holding me back :/
If you look at your import statement, they're referring to non freemarker classes of the same name.
The jar isn't actually in your build path. Right click the project and choose "Properties", then "Java Build Path". If freemarker isn't in the Libraries list, select Add JARs and find the jar in your project.
Delete the "import javax.security.auth.login.Configuration" line. You need to choose the Freemarker configuration.
Hope that helps.