JDK 1.7 compiler does not recognize generic class - eclipse

I have a class which uses a class from Eclipse OSGI jar (org.eclipse.osgi_3.7.2.v20120110-1415.jar) and which is well compiled within Eclipse IDE and it's compiler. But if I try to compile this class with Ant and JDK 1.7 compiler, the compiler outputs these errors:
[javac] /data/ant/sw_jdk1.7/test-Java7/com.tsystems.favbg.ui.core/src/com/
tsystems/favbg/ui/core/job/AbstractLoader.java:24:
error: type CopyOnWriteIdentityMap does not take parameters
[javac] private final CopyOnWriteIdentityMap<LoaderListener, String>
eventListenerMap = new CopyOnWriteIdentityMap<>();
and
error: cannot infer type arguments for CopyOnWriteIdentityMap;
[javac] private final CopyOnWriteIdentityMap<LoaderListener, String>
eventListenerMap = new CopyOnWriteIdentityMap<>();
[javac] reason: cannot use '<>' with non-generic class CopyOnWriteIdentityMap
Obviously javac does not recognize the class as using generics. But when I open it with Java Decompiler generic parameters are well recognized.
Does anybody know this problem and have a solution?

I would say that the problem is in the way that you are using CopyOnWriteIdentityMap class. I'm assuming that you are using this class:
org.eclipse.osgi.framework.eventmgr.CopyOnWriteIdentityMap
If you look at the javadocs, you will see that it is not a generic class. I also get the impression (from its package location) that it is not intended to be a general purpose utility class.
Perhaps you are using a different version of the JAR when you are compiling in Eclipse ... one that is a generic class. Either way, the compiler you are using via Ant thinks it is not generic.

I am finding discrepancies when reading a certification book for SCJP based on JDK 1.6, and, trying to write (copy and paste) code, compiling within JDK 1.7.
Perhaps, I am making the same mistakes all over, however they seem hard to comprehend. For example see my posts at http://www.coderanch.com/forums/posts/list/80/467890#2679297. I am not an expert, however, I find the curriculum of Angelika Langer (http://www.angelikalanger.com/Publications.html), to be appealing for consulting, given the tutorials available on the website relating to Java Generics.
What is your advice on the matter?

Related

Find Unused case classes in project

im working on a huge project which has a lot of old code.
I found manually found some random case classes which are not used in the project and asked myself how many of those exists.
But I found no method to found them via scalac compiler or via some Intellij magic.
This code often uses sometimes this companion structure
object MyCompanionClass{
final case class TEST(...)
...
}
Running unused declaration in intellj ofcourse delivers only findings in java..
Can anyone help?
regards

No source code is available for type in GWT (abstract class)

I have a business object (called Expense) that I am trying to use in my GWT application. I have it imported in the class, but when I compile, I get the following error:
[ERROR] Line 67: No source code is available for type com.app.pojo.Expense; did you forget to inherit a required module?
Is there anything special that I have to do (addition to gwt.xml?) to use it?
I dont' know how your Expense class looks like, but you can't use either Gson or SimpleDateFormat in GWT code. See the JRE emulation list of the only available types. You have to use both on the server. You can use DateTimeFormat on client/shared sides.
I'm assuming you have already added com.app.pojo as a translatable package (i.e., using source-path in your XML module).
Apparently I cannot use the gson library, nor the SimpleDateFormat class with gwt?

How can I use JDT compiler programmatically?

I use JDT to compile my java classes. BatchCompiler returns a string but I need an array of problems/errors with their column and row information. compiler.compile(units); prints the error to its printwriter, compiler.resolve(unit) does exactly what I want but it can compile only one java file.
I created a compiler object in this way:
Compiler compiler = new Compiler(env, DefaultErrorHandlingPolicies.exitAfterAllProblems(), new CompilerOptions(), requestor, new DefaultProblemFactory());
And create CompilationUnits that contains filenames and file contents to the compiler.
CompilationUnit[] units = project.toCompilationUnit();
AFAIK, there are 2 ways to compile, one of them is compile(units) method that returns void and prints errors and problems to its PrintWriter, because it doesn't return column information it's not useful for me. The other way is resolve(unit) method but it can work with only one CompilationUnit.
compiler.resolve(units[index], true, true, true);
Does anyone know how I can use JDT compiler programmatically to compile multiple files?
org.eclipse.jdt.internal.compiler.Compiler is internal API. According to the JavaDoc of its resolve method: Internal API used to resolve a given compilation unit. Can run a subset of the compilation process.
Instead, the official way of compiling Java files and determining the compilation problems is described here. Basically, you create a Java project and invoke Eclipse's builder on it, then query the project's Java problem markers.

GWT and ReflectionToStringBuilder.toString()

I have a problem with the GWT compiler. When I add a next method to my entity class:
#Override
public String toString() {
return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
the compiler gave me next error:
ERROR: Deferred binding failed for 'com.mvp4g.client.Mvp4gModule'; expect subsequent failures
ERROR: Unable to load module entry point class plaut.wimc.avl.admin.client.Admin (see associated exception for details)
java.lang.RuntimeException: Deferred binding failed for 'com.mvp4g.client.Mvp4gModule' (did you forget to inherit a required module?)
When I remove it, all works fine. I don't understand why gave me compiler such error. This toString method is used in roo IDT's too and there is no such error.
All Java code used in the client side needs to be able to compile to JavaScript. ReflectionToStringBuilder uses reflection which isn't available in JavaScript so this method can't be used in your client side code.
This compile error refers to the fact that all Java code must be accessed by the GWT copmiler via path parameters in GWT module files and must be available in source format. In this case no GWT module file is present, hence the error, because the compiler can't find the sources for the ReflectionToStringBuilder method. Although you can create such a file for this specific case and add the sources, it won't work as reflection won't work.
Looks like the problem is with ReflectionToStringBuilder. Is it a GWT module? If yes, then it needs to be added an inherited module in your project's *.gwt.xml

Importing Groovy classes in a GWT module in Intellij

Basically I can't even import Groovy classes in a regular GWT EntryPoint:
import com.google.gwt.core.client.EntryPoint; // OK
import groovy.lang.Binding; // NOT OK
import groovy.util.GroovyScriptEngine; // NOT OK
Intellij 8.0's inspector keeps telling me this:
Class 'groovy.lang.Binding' is not present in JRE Emulation Library so it cannot be used in client code
Class 'groovy.util.GroovyScriptEngine' is not present in JRE Emulation Library so it cannot be used in client code
Obviously I end up having problems at runtime as well:
[ERROR] Line 11: No source code is available for type groovy.util.GroovyScriptEngine; did you forget to inherit a required module?
[ERROR] Line 12: No source code is available for type groovy.lang.Binding; did you forget to inherit a required module?
I've checked several times my module's dependencies, and groovy-all-1.6.1.jar is correctly defined in there. I'm probably missing something elsewhere...
Any idea?
Attempting to use Groovy makes no sense as the Gwt compiler only understands the Java language syntax along it it's subset of Java libraries. The compiler works on source not byte code. One reason includes the magic comments necessary to stash javascript (JSNI). The compiler is actually scanning the source path for *.java not *.class files.
Read the doco fir a more in depth understanding.
In some sense it would be pretty cool to have a groovy language gwt.
But then why go from one dynamic language to another? The point of gwt I thought was to get static typing and easy debugging of java.