STS unable to resolve target type with java 8 - eclipse

I have the below code that errors out in STS(3.8.4) saying,
Type mismatch: cannot convert from Map<String,Object> to Map<? extends
K,? extends V>
this.attributeMappings = new HashMap<>(attributeMappings!=null?attributeMappings:Collections.EMPTY_MAP);
Where both the instances of attributeMappings are of type Map<String, Object>.
Ideally, this shouldn't be a problem with Java 8. Could someone help me to resolve it without making any changes to the source?
PS: The same happens with Eclipse Neon 3

You should upgrade, this is an eclipse compiler issue (I've tried searching for it, but no luck) to Eclipse Oxygen let's say.
Map<String, Object> attributeMappings;
#SuppressWarnings("unchecked")
public void test(Map<String, Object> attributeMappings) {
this.attributeMappings = new HashMap<>(attributeMappings != null
? attributeMappings : Collections.EMPTY_MAP);
}
This compiles just fine for me with javac-8 and in eclipse oxygen latest.
Also notice that you can replace Collections.EMPTY_MAP with Collections.emptyMap() and it will work also.

Related

Eclipse IDE 2020‑03 Debugger is doing weird stuff

So I am stuck with a problem when debugging with the most recent version of Eclipse 2020-03, which I installed for a new project I'm working on.
It first struck me that things were not working correctly when I couldn't read a resource with Class.getResource( String name ), as the debugger at the breakpoint in getResource(..) kept telling me that the name was null, while I definitely had provided a path name.
Clearing, cleaning, reloading the target (Running Platform), refreshing and rebuilding did not change anything, so I decided to create a simple OSGI plugin project with just an Activator, and a debug configuration with only the bare minimum bundles.
The Activator looks like this:
public class Activator implements BundleActivator {
public static final String BUNDLE_ID = "test.myapp.core";
private static BundleContext context;
private Logger logger = Logger.getLogger(this.getClass().getName());
static BundleContext getContext() {
return context;
}
public Activator() {
super();
logger.info("STARTED: " + BUNDLE_ID);
}
#Override
public void start(BundleContext bundleContext) throws Exception {
logger.info("ACTIVATED: " + BUNDLE_ID);
Activator.context = bundleContext;
InputStream in = getClass().getResourceAsStream( "/test.cfg" );
}
#Override
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
}
}
EDIT: Changed the original link to build.properties to test.cfg in order to avoid confusion.
But when I start the debugger, it will activate the bundle, but will not show any of the log messages. Also the debugger will not respond to the breakpoints I put in. Strangely enough, selecting 'ss' shows me far more bundles than the ones provided in the debug configuration.
id State Bundle
0 ACTIVE org.eclipse.osgi_3.15.200.v20200214-1600
1 ACTIVE test.myapp.core_1.0.0.qualifier
2 ACTIVE org.apache.lucene.core.source_8.4.1.v20200122-1459
3 ACTIVE javax.annotation.source_1.2.0.v201602091430
....
It seems as if a different debug configuration is launched, and is using an previously built version of my bundle, where the log messages were not yet included. clearing the bin folder, and eventually all the metadata also had no effect.
I'm totally stumped as of what I'm experiencing here. Hopefully someone can help!
Well..as it seems, I found out what was wrong. There were two problems that were occuring all at once:
1: Regarding the problems with getResource( String name). The Bundle-ClassPath setting in Manifest.MF MUST include . (see https://www.eclipse.org/forums/index.php/t/287184/), so in my case:
Bundle-ClassPath: .,
test.myapp.core
Bundle-ClassPath is not added automatically by the plugin wizards, so that can cause some problems.
2: The Debug configuration screen in the new IDE seems to be very slow, and does not change the selected bundles if you switch from "only show selected" and back. As a result, the previous list of bundles remained active, while they were unchecked in the Debug Configuration Editor.
I'll file a report for these issues
ADDITIONAL
So I have made some further investigations on the Bundle-ClassPath issue, and what probably has happened that this entry in the Manifest.MF occured when adding some libraries. After they were removed again, the Bundle-ClassPath entry remained, and caused all sorts of problems. If you ever:
1: Notice that certain classes from bundle A cause a NoClassDefFound exception when used in bundle B
2: The build.properties file from bundle A give a warning that sources are missing
3: Other bundles don't seem to give that problem
Check to see if there is a Bundle-ClassPath entry in your Manifest.MF file. Most probably that entry is causing the problems. Either remove the entry in the manifest, or add ,. at the end.
see:
1: What is the intended use case for Bundle-Classpath in OSGI bundles
2: https://bugs.eclipse.org/bugs/show_bug.cgi?id=139271

Eclipse custom #NonNull annotation ignored

I would like to use the "null analysis" feature of Eclipse but I would rather use the javax.annotation.Nonnull annotation than the org.eclipse.jdt.annotation.NonNull annotation. I can't seem to get Eclipse to recognize any custom annotations I configure.
I'm using Eclipse Luna 4.4.2.
I created a new test project that contains only the following class:
package org.example;
import javax.annotation.Nonnull;
public class OmgNulls {
public static void main(String[] args) {
#Nonnull String test = null;
System.out.println(test);
}
}
My compiler settings are configured to use the javax annotations:
My project's includes the checker-1.8.10 JAR to provide the annotations.
With these settings, Eclipse reports 0 errors.
If I check "Use default annotations for null specifications" and change my test class to use Eclipse's annotation, I get:
Anyone know what I'm doing wrong or is this an Eclipse bug maybe?
I've also tried the jsr305 JAR from google instead of checker in case that was an issue but that didn't help.

JaxB #XmlRootElement result in "Cannot resolve xml element declaration"

I was using JAXB as DTO to set a stable interface between Server and Clients. Anyways this doesn't matter. What matters is I created a set of classes that result in the following compile error.
Cannot resolve XML element declaration with namespace 'namespace' and
name 'name' in this context
Eclipse underlined "name" inside quotes as an error. This class is manually created instead of xjc generated.
#XmlRootElement(name="name", namespace="namespace")
#XmlType(name="")
public class UserDTO {
private UserType userType;
#XmlElement
public UserType getDTO(){
return userType;
}
public void setDTO(UserType userType){
this.userType=userType;
}
}
where UserType is a xjc generated class
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "userType", propOrder = {
"userId",
"userName"
})
public class UserType {getter;setter}
So basically UserDTO is just a wrapper that wraps up sub jaxb types.
I'm not sure if it was platform dependent (which it shouldn't be), anyways, just to mention that this code worked perfectly on Netbeans, but when it come to Eclipse, the error prevented the compilation.
The Environment running the project was:
1. MacOsX Lion
2. JDK: 1.6.0_37
3. Eclipse Version: Juno with Package 1
4. JAXB Platform: Generic JAXB 2.1
Please anyone can share some idea?
ps: I added the JDK info and Libraries setting as Manuel suggested.
I faced this issue, you have more than one schema, at least two, both of them don't have namespace, just assign namespace to one of them.

Eclipse debug can not step into (or F5)

package com.test;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) throws Exception {
TreeMap<String, String> tree = new TreeMap<String, String>();
tree.put("1", "1");//line a
tree.put("1", "1");//line b
System.out.println(tree.size());
}
}
I want to debug TreeMap put method, so i add two breakpoints for the lines (line a,line b).
Eclipse debug tool cannot step into put mentod when debugging.
I have attached source code for eclipse. when i put mouse over put method, press F3 key, it can go to TreeMap put method source code. I am running Eclipse SDK Version: 3.2.2
download the source code from this link : http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/TreeMap.java and then attach this source into eclipse

Eclipse Unresolved compilation

I have this really weird problem working on a bigger project in Eclipse Indigo 3.7.2.
I checked out the project from an SVN repository using the Subclipse plug-in and when I start the application I get the following error message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at anares.preprocess.StanfordParser.getInstance(StanfordParser.java:73)
at anares.start.Startconsole.<init>(Startconsole.java:22)
at anares.start.Startconsole.main(Startconsole.java:52)
This is what Startconsole.class looks like, containing the main method:
package anares.start;
import java.io.FileNotFoundException;
import java.io.IOException;
import anares.core.AnaResAlgorithm;
import anares.preprocess.MorphaDornerSentenceSplitter;
import anares.preprocess.CollectionEquipper;
import anares.preprocess.ParserHandlerInterface;
import anares.preprocess.Preprocessor;
import anares.preprocess.SplitterInterface;
import anares.preprocess.StanfordParser;
import anares.text.AnaResTextObject;
public class Startconsole {
public final ParserHandlerInterface parserint = StanfordParser.getInstance();
public final SplitterInterface splitterint = MorphaDornerSentenceSplitter.getInstance();
public final CollectionEquipper equipperint = null;
public final static int buffersize = 5;
private Startconsole(String file) throws IOException {
AnaResTextObject object = startPreprocess(file);
startAlgorithm(object);
}
private AnaResTextObject startPreprocess(String file) throws IOException {
Preprocessor prepro = new Preprocessor(parserint, splitterint,
equipperint);
AnaResTextObject textObject = prepro.preprocessText(file);
return textObject;
}
private void startAlgorithm(AnaResTextObject object) {
AnaResAlgorithm algo = new AnaResAlgorithm(buffersize);
algo.resolveAnaphora(object);
}
public static void main(String args[]) throws FileNotFoundException,
IOException {
if(args.length > 0){
Startconsole console = new Startconsole(args[0]);
}else{
Startconsole console = new Startconsole("Text.txt");
}
}
}
As I was saying this is a bigger project and therefore contains a few .jar-files and references to other packages.
This problem only occurs on my laptop. On my other PC everything works fine, and a fellow student of mine, who works on the same project, does not have any problems either.
I already tried checking the project out again, cleaning it up and even reinstalling eclipse.
Now here's the weird part: If I comment out the whole main method, just leaving something like
public static void main(String args[]) throws FileNotFoundException,
IOException {
// if(args.length > 0){
// Startconsole console = new Startconsole(args[0]);
// }else{
// Startconsole console = new Startconsole("Text.txt");
// }
System.out.println("Hello World!");
}
I still get the exact same error message with the exact same line numbers. And no "Hello World!" in the output.
Does anyone have any ideas where the problem comes from?
Your issue seems like either there is an error in the code that I cannot see, or your Eclipse instance/compiler got into a strange state it cannot recover from.
Just some basic ideas to check
Have you tried restarting Eclipse?
Are you using the same version of Java on all computers? E.g. there might be some incompatibilities between Java 6 and Java 7.
Is automatic build turned on? Look in the Project/Build automatically menu item. It is possible that the automatic Java builder got turned off, and thus it does not recompile your code.
Have you tried to clean your project to force a rebuild? (Project/Clean menu item).
Is JDT installed in your Eclipse instance? It should be, but it might worth check for such trivial issue.
Maybe you should try to create a new workspace, and checkout the projects again.
You could also try to download Eclipse again with this new workspace idea.
If neither of these things work, I have no idea what to look for.
Look in Eclipse's Problems view (tab); any compilation problems in the project will be reported there. You can double-click on an error or warning in the Problems view and the editor will open on the specific line that is a problem.
Do one thing just remove the build path of englischPCFG.ser.gz from your project because i am sure this is not the jar file you have added in your project