Eclipse internal compiler error - java

When using this code in Eclipse:
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Test {
public static void main(String[] args) {
List<Object> objs = Arrays.asList(new Object(), new Object());
Set<String> s = objs.stream().collect(HashSet::new, HashSet::add, Object::toString);
System.out.println(s);
}
}
I get:
Internal compiler error: java.lang.ArrayIndexOutOfBoundsException: 0 at
org.eclipse.jdt.internal.compiler.lookup.ConstraintExpressionFormula.reduceReferenceExpressionCompatibility(ConstraintExpressionFormula
.java:273)
I know that this is this line which is producing the error:
Set<String> s = objs.stream().collect(HashSet::new, HashSet::add, Object::toString);
Not sure if it's relevant but I'm using:
Eclipse Kepler 4.3.2
Plugins: Eclipse Java Development Tools Patch with Java 8 support (for Kepler SR2) and Eclipse Plug-in Development Environment Patch with Java 8 support (for Kepler SR2)
java.runtime.version=1.8.0-b132
Here's the screenshot:
I know that the collect method is not correct but why I don't have a compiler error telling something like:
- The method collect(Supplier<R>, BiConsumer<R,? super Object>, BiConsumer<R,R>) in the type Stream<Object> is not applicable for the arguments etc.

This looks like Eclipse bug 433085 a duplicate of bug 430766. This is targeted to be fixed in Eclipse 4.4 Luna M7.

Related

inference variable R has incompatible bounds

Today i experienced a strange error. The Strange was if i add the below code block in eclipse IDE it shows no error , but the same piece of code i compile it from cmd its showing error as
inference variable R has incompatible bounds
The Code Piece as follows:
import java.util.*;
import java.util.stream.*;
import java.util.function.*;
public class Test{
public static void main(String[] args){
int[] x = {1,2,3,4,5,6,1,2,3,1,4,65,3,56,24};
System.out.println(Arrays.stream(x).boxed().collect(Collectors.groupingBy(Function.identity(),LinkedHashMap::new,Collectors.counting())));
}
}
I Used same JDK versions(11.0.15 2022-04-19 LTS) both in eclipse as well as cmd prompt to compile it. Is Eclipse is doing some magic here or it is a bug at eclipse IDE.

Encountered "=" error in Jmeter when List is used in jmeter beanshell code

Below is my code.
package findDuplicatre;
import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class FindDuplicate {
public static void main(String[] args) {
String[] ContentNameCount = {"name1","name1","name2","name2", "name2"};
List<String> sampleList = (List<String>) Arrays.asList(ContentNameCount);
for(String inpt:ContentNameCount){
int frequency=Collections.frequency(sampleList,inpt);
log.error(inpt+" "+frequency); //System.out.println(inpt+" "+frequency);
}
}
}
In eclipse i could get results:
name1 2
name1 2
name2 3
name2 3
name2 3
When i could run the same code in Eclipse without any error.But when i run the same code in jmeter beanshell sampler i'm getting below errors.
Response message: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval In file: inline evaluation of: ``package findDuplicatre; import java.util.*; import java.util.Collections; impor . . . '' Encountered "=" at line 13, column 41.
Can anyone kindly explain me what am i missing and how would it make difference when i run in beanshell script.
Beanshell is not Java, you need to stick to Java SE 1.5 language features in general. In particular your case you need to remove diamond operators from your code as they were introduced in Java 7 and Beanshell doesn't support them like:
List sampleList = Arrays.asList(ContentNameCount);
A better option would be going for JSR223 Test Elements and Groovy language. Groovy is compatible with all modern Java language features, moreover it has much better performance comparing to Beanshell. See Apache Groovy - Why and How You Should Use It for more details.
#Dmitri T answer is valid, you can still overcome the exception (remove diamond operators) in Beanshell:
String[] sampleList = {"name1","name1","name2","name2", "name2"};
for(String inpt:sampleList){
int frequency=Collections.frequency(Arrays.asList(sampleList),inpt);
log.error(inpt+" "+frequency); //System.out.println(inpt+" "+frequency);
}

How to interact with Fuseki using Java in Eclipse

I'm following this tutorial here to insert a new resource into Fuseki's dataset, but I'm getting this error:
the method format(String, Object[]) in the type String is not applicable for the arguments (String, String)
This is the code:
import java.util.UUID;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.update.UpdateExecutionFactory;
import com.hp.hpl.jena.update.UpdateFactory;
import com.hp.hpl.jena.update.UpdateProcessor;
/**
* Example connection to Fuseki. For this to work, you need to start a local
* Fuseki server like this: ./fuseki-server --update --mem /ds
*/
public class FusekiTest {
/** A template for creating a nice SPARUL query */
private static final String UPDATE_TEMPLATE =
"PREFIX dc: <http://purl.org/dc/elements/1.1/>"
+ "INSERT DATA"
+ "{ <http://example/%s> dc:title \"A new book\" ;"
+ " dc:creator \"A.N.Other\" ." + "} ";
public static void main(String[] args) {
//Add a new book to the collection
String id = UUID.randomUUID().toString();
System.out.println(String.format("Adding %s", id));
UpdateProcessor upp = UpdateExecutionFactory.createRemote(
UpdateFactory.create(String.format(UPDATE_TEMPLATE, id)),
"http://localhost:3030/ds/update");
upp.execute();
}
}
How can I fix that error?
This issue is common when the java project version is 1.4.
It's a common problem that the project Java version is set to 1.4 or 1.6 by default in the template by the IDE. You should make sure that you have the correct Java version set on your project.
How to change your Java version
Eclipse:
Right click project -> Properties -> Java Build Path -> select JRE System Library click Edit and select JDK or JRE after then click Java Compiler and select Compiler compliance level to 1.8
IntelliJ
Menu -> File -> Project structure -> project SDK
Netbeans
This assumes that you installed JDK 1.6 and NetBeans knows about this.
Right-click on the Project and select Properties.
Under Library, select Java Platform JDK 1.8.
Select Source/Binary Format JDK8 in the Source category.
The JDK 1.8 must already have been supplied to NetBeans. To do that you got to menu -> Tools-> Java Platform Manager.

Eclipse cannot infer correct type from java 8 stream

The following code works fine in all online java compilers but eclipse throws compiler error. Is it a bug in eclipse or am I missing some setting somewhere? A simple fix to silence eclipse? online: https://ideone.com/l0bbhz. Note: This is a simplified cooked-up example to just point to the problem. I understand flatMap is not necessary in this case. In the actual case, I really need flatMap
package dummy;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static java.util.stream.Collectors.toList;
public class LearnJava {
public static void main(String[] args) {
String[] sa = {"ne", "two", "three"};
List<String> l = Arrays.stream(sa)
.flatMap(s -> Collections.singleton(s).stream().map(c -> c.toUpperCase()))
.collect(toList());
System.out.println(l.get(0));
}
}
Error in eclipse console.
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from List<Object> to List<String>
at dummy.LearnJava.main(LearnJava.java:13)
My eclipse version:
Eclipse Java EE IDE for Web Developers.
Version: Luna Service Release 2 (4.4.2)
Build id: 20150219-0600
Update: I went with this minor workaround. It works without major refactoring!
.flatMap(s -> Collections.singleton(s).stream().map(c -> c.toUpperCase()))
To
.<String>flatMap(s -> Collections.singleton(s).stream().map(c -> c.toUpperCase()))
The Eclipse compiler is not perfect. Sometimes you'll hit issues such as this. For example, there are currently two bugs open related to flatMap and type interference - 482664 and 502158.
If you believe the code is legit, which is strongly the case when javac compiles it without issues, then you should open a bug and post a snippet there in order to let them know about it. This helps improving the compiler.

Intellij IDEA cannot resolve 'andThen' functional interface method when referenced Function::andThen

How can I get IntellijIDEA to find the 'andThen' method of the core java 8 'Function' interface when using the notation Funciton::andThen? I've tried many things unsuccessfully.
My intellijIDEA module is configured to java 8, the sdk used is the oracle java 8, I've invalidated the caches, and tried several other things, but still the editor marks and then as: "cannot resolve method 'andThen'".
I can launch and build this sample, so I think it's something to do with the static code analyzer. Maybe a bug?
package foo.bar;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public class Meh {
public static void main(String... args) {
final List<Function<String, String>> fs = new ArrayList<>();
fs.add(s -> s + "1");
fs.add(s -> s + "2");
final Function<String, String> f =
fs.stream()
//copmiles from cli and project->make, but editor shows: Cannot resolve method 'andThen'
.reduce(Function::andThen)
.get();
System.out.println(f.apply(""));//succesfully prints 12
final Function<String,String> f2 = f.andThen(s-> s+"a");
//succesfully prints 12a
System.out.println(f2.apply(""));
}
}
Something interesting is that when I reference f.andThen, the static code analyzer doesn't complain. It only happens when I reference Function::andThen.
This is not a problem when using eclipse. Or again, when compiling from the command line, or going to project -> make
This seems to be fixed in IntelliJ 15.0.2, with 15.0.1 I could reproduce this error marker.
Quite some bugs which sound like your kind of problem are mentioned in the release notes, section "Java.Error Highlighting", e.g.:
IDEA-146604 (Bug) Valid code highlighted as error (Enum::compareTo)
IDEA-147873 (Bug) Good code marked red with lambdas/method references

Categories

Resources