简体   繁体   中英

JDK 1.7 compiler does not recognize generic class

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM