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

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

Related

IBM Watson Visual Recognition in Java training classifier error

So I want to make a java application in eclipse which the user i will be able to import .zip files. Each .zip file will represent a cat breed. I will click on a "train" button and my program will contact IBM Watson services and create a classifier. Then from a different window, i will import random cat images and the program will show what cat breed is in the image. Everything with the SDKs is fine since I ran some examples from the official Watson site and everything ran smoothly. Problem comes when I try to create my own classifiers. The code you are about to see is also from their site. For some reason the createClassifier method won't take the CreateClassifierOptions object as an argument.
import java.io.File;
import com.ibm.watson.developer_cloud.http.ServiceCall;
import com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognitionCallback;
import com.ibm.watson.developer_cloud.visual_recognition.v3.*;
import com.ibm.watson.developer_cloud.visual_recognition.v3.model.*;
public class TrainningClassifier{
public static void main(String[] args) {
VisualRecognition service = new VisualRecognition(
VisualRecognition.VERSION_DATE_2016_05_20
);
service.setApiKey("aca4433597018de62edafdeebceb2bdc1482496a");
CreateClassifierOptions createClassifierOptions = new CreateClassifierOptions.Builder()
.name("dogs")
.addClass("beagle", new File("./beagle.zip"))
.addClass("goldenretriever",new File("./golden-retriever.zip"))
.addClass("husky", new File("./husky.zip"))
.negativeExamples(new File("./cats.zip"))
.build();
Classifier dogs = service.createClassifier(createClassifierOptions).execute();
System.out.println(dogs); /*error is in the above line.
the createClassifier method.*/
}
}
Error: Exception in thread "main" java.lang.Error: Unresolved
compilation problem: The method createClassifier(ClassifierOptions)
in the type VisualRecognition is not applicable for the arguments
(CreateClassifierOptions)
at testVisualRec.ForAssignment.main(ForAssignment.java:31)
Any ideas?
Found the solution. For some reason eclipse wouldn't recommend this solution I had to experiment. I just added throws IOException in main method. I also put inside the main method System.out.println(new File(".").getAbsoluteFile()); to make sure the path was correct, and it was. (SDK used for this project is 4.0.0, not the newest one. SDK found here: https://github.com/watson-developer-cloud/java-sdk/releases)

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.

Minecraft forge not loading textures

I'm trying to teach myself java syntax and using minecraft as a platform for diving in. I'm having a problem though because none of my textures are being loaded. For that matter neither are my localizations. Here is the code for my block
package net.richbaird.testtutorial.blocks;
import cpw.mods.fml.common.registry.GameRegistry;
//import cpw.mods.fml.common.registry.LanguageRegistry;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
import net.richbaird.testtutorial.lib.constants;
public class OrangeBlock extends Block {
private String blockName = "orangeBlock";
public OrangeBlock() {
super(Material.rock);
this.setBlockName(constants.MODID + "_" + blockName);
this.setCreativeTab(CreativeTabs.tabBlock);
GameRegistry.registerBlock(this,blockName);
this.setBlockTextureName(constants.MODID + ":" + blockName);
//LanguageRegistry.addName(this,"tutorial block");
}
}
here is my constants class
package net.richbaird.testtutorial.lib;
public class constants {
public static final String MODID = "testtutorial";
public static final String MODNAME = "Test Tutorial";
public static final String VERSION = "1.0";
}
I have my texture saved at
~/IdeaProjects/testmod/src/main/resources/assets/testtutorial/textures/blocks/orangeBlock.png
According to the log it is unable to find my texture. Here's the message I'm getting
[08:08:14] [Client thread/ERROR]:
Using missing texture, unable to load
testtutorial:textures/blocks/orangeBlock.png
java.io.FileNotFoundException: testtutorial:textures/blocks/orangeBlock.png
The client loads and my item shows up but with a default black and purple texture. What have I done wrong? I'm thinking it might have to do with my naming conventions, since the .lang file never gets read either, and the only way I can give my block a friendly name is with the now depreciated LanguageRegistry.addName() method
For those who are curious, it's a bug with intellij 14 looks like. Adding this line to the bottom of the build.gradle that comes with forge
sourceSets {
main { output.resourcesDir = output.classesDir }
}
And running gradle setupDecompWorkspace idea --refresh-dependencies
fixed the problem.
I recently ran into this bug after updating IntelliJ, and while richbai90's solution did fix the immediate issue, it also broke compiling the mod into a jar (the assets folder gets included twice). After some digging around, I eventually found the root of the issue: IntelliJ was delegating the build task to Gradle, which put the assets and classes in separate folders, and Forge didn't know they belong to the same mod. The solution that worked for me was to build and run using the IDE, which is in the Settings dialog under Build, Execution, Deployment | Build Tools | Gradle (the help page has more detailed instructions). On older versions of IntelliJ, this was called "Delegate IDE build/run actions to gradle" (see the help page).

Eclipse internal compiler error

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.

Weird error with Locale.getISOCountries()

I'm using this code:
for (final String code : Locale.getISOCountries())
{
//stuff here
}
But on compile I get this error:
[ERROR] Line 21: No source code is available for type java.util.Locale; did you forget to inherit a required module?
And then a stack trace of compiler errors.
I'm doing both of these imports at the beginning of the class:
package com.me.example;
import java.util.Locale;
import java.util.*;
What can be wrong?
In Netbeans i see the autocomplete options and no syntax error for the Locale object...
Something screwy with your setup, the folllowing program works fine for me.
import java.util.*;
import java.util.Locale;
public class Donors {
public static void main (String [] args) {
for (final String code : Locale.getISOCountries()) {
System.out.println (code);
}
}
}
The fact that it's asking for source code leads me to believe that it's trying to compile or run it in some sort of debugging mode. You shouldn't need the source code for java.util.* to compile, that's just bizarre.
See if my simple test program works in your environment, then try looking for something along those lines (debugging options). Final step: compile your code with the baseline javac (not NetBeans).
UPDATE:
Actually, I have found something. If you are creating GWT applications, I don't think java.util.Locale is available on the client side (only the server side). All of the references on the web to this error message point to GWT and its limitations on the client side which are, after all, converted to Javascript goodies, so cannot be expected to support the entire set of Java libraries.
This page here shows how to do i18n on GWT apps and there's no mention of java.util.Locale except on the server side.
Looks like there might be something fishy in your build environment, as Locale.getISOCountries() should work just fine. Try compiling a small test program manually and see if you get the same error.
Definitely try to boil this down to a minimum, three-line program (or so), compile from the command-line, then put that class into your IDE and see if you still get the error, and if not, then change/add one line at a time until you have the original failing program, looking for what causes the problem. I'm thinking maybe some other import in your code is importing a Locale class? Why in the world would it be looking for source code?
See what happens when you compile this from the command-line:
import java.util.*;
public class LocaleTest {
public static void main(String[] args) {
Locale.getISOCountries();
}
}

Categories

Resources