In the following code I get a warning from the squid:RedundantThrowsDeclarationCheck rule on the Foo1Exception (behind the throws keyword): Remove the redundant '!unknownSymbol!' thrown exception declaration(s).
Foo.java:
public class Foo {
public static boolean bar(final String test) throws Foo1Exception, Foo2Exception {
if (test.startsWith("a")) {
throw new Foo1Exception();
} else if (test.startsWith("b")) {
throw new Foo2Exception();
} else if (test.startsWith("c")) {
return true;
}
return false;
}
}
Both exceptions are decrlared in seperate files:
Foo1Exception.java:
class Foo1Exception extends Exception {}
Foo2Exception.java:
class Foo2Exception extends Exception {}
I think this is a false positive, isn't it?
Also interesting: I don't get this message directly in SonarQube (web interface) only in the SonarLint plugin in IntelliJ IDEA.
Any Ideas?
I'm using: IntelliJ IDEA 2016.2.2; SonarLint 2.3 (with working server binding); SonarQube 5.6; SonarQube Java Plugin 4.0; Java 8
This seems to be fixed in SonarLint 2.3.1.
Related
I followed the instructions from this video https://www.codenameone.com/how-do-i---access-native-device-functionality-invoke-native-interfaces.html, but my code is throwing java.lang.ClassNotFoundException: interfaces.MyNativeImpl on the look up line. The code I'm using is almost exactly that of the video.
package interfaces;
import com.codename1.system.NativeInterface;
public interface MyNative extends NativeInterface{
public String sayHi();
}
in the android native directory
package interfaces;
public class MyNativeImpl {
public String sayHi() {
return "hi";
}
public boolean isSupported() {
return true;
}
}
and in the java code:
MyNative my = (MyNative)NativeLookup.create(MyNative.class);
if(my != null && my.isSupported()){
System.out.println("Hello!");
}
Where did I go wrong now?
That is perfectly fine code and will work on the device.
You are seeing the exception in the simulator because the native/internal_tmp directory is missing from your runtime classpath but it shouldn't cause a problem other than no native interfaces on the desktop:
I am trying to follow the sparkjava exception handling example located here, without success: http://sparkjava.com/documentation.html#exception-mapping. It appears the code they posted isn't quite right? I was able to fix one of the posted methods so that it compiles. The method on the documentation page which doesn't compile was:
get("/throwexception", (request, response) -> {
throw new NotFoundException();
});
I changed to this code and it compiles:
get(new Route("/throwexception") {
#Override
public Object handle(Request request, Response response) {
throw new IllegalArgumentException();
}
});
However, I am unable to get this method to compile. What is wrong? I am using java 8 and IntelliJ community edition 15.0.2 to compile. My java module is set to language level 8. Here is the suspect method:
exception(Exception.class, (e, request, response) -> {
//TODO: implement this after it compiles.
});
Here is the error I get from the compiler:
Error:(83, 9) java: cannot find symbol
symbol: method exception(java.lang.Class<java.lang.Exception>,(e,request[...]->{ })
location: class org.me.JournalController
To confirm that I really am using java 8, this example using a lambda expression does compile:
public class Lambdas {
public static void main(String[] args) throws Exception {
new Lambdas().start();
Thread.sleep(1000);
}
public void start(){
Interface f = () -> System.out.println("test");
}
}
The original code uses lambda expressions, you should use Java 8 to compile it.
My maven file had sparkjava dependency listed twice. I think my IDE automatically imported 1.1.1 for me at some point and I didn't realize it. I had 1.1.1 and 2.3. When I removed the outdated 1.1.1 sparkjava dependency then everything works as expected.
You can extract your throw statement into a function:
get("/throwexception", () -> BlowUp());
(snip)
public static void BlowUp()
{
throw new NotFoundException();
});
I have a class that take a ArrayList as parameter:
public class Foo {
private ArrayList<Bar> bars;
public Foo(ArrayList barList) {
bars = barList;
}
}
there is a bug that I can pass any ArrayList into the constructor:
// should compile error with this line
Foo foo = new Foo(new ArrayList<String>());
the problem is if I add this case to test suite, when the bug fixed, I can not compile it.
is there anyway to test this case?
I don't know of any language/unit test framework that will let you "test" for code that should not compile. If you can't compile it, there is nothing to test. You can however turn on all compiler warnings when building. I'm pretty sure passing an unparameterized collection is a big warning in any JVM after JDK5.
I feel it is bad practise and I don't really see a use for it, but see this example for how to test for compilations errors:
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
#RunWith(JUnit4.class)
public class CompileTest {
//A very naive way checking for any error
#Test(expected=java.lang.Error.class)
public void testForError() throws Exception {
this.is.not.a.statement;
}
//An example which check that we only find errors for regarding compilation errors.
#Test
public void expectNotCompilableMethod() {
try {
uncompilableMethod();
fail("No compile error detected");
} catch (Error e) {
assertTrue("Check for compile error message", e.getMessage().startsWith("Unresolved compilation problems"));
}
}
private void uncompilableMethod() {
do.bad.things;
}
}
Edit:
1) I am not sure how this may behave together with build-tools like maven. As far as I know maven breaks the build at compile-errors, so probably the tests will not even be executed.
Fix your method signature:
public Foo(ArrayList<Bar> barList) {
bars = barList;
}
Problem solved. (You probably also want to check for null.)
When I add a keyword system to my grammar, the generated InternalParser shows errors, when the keyword is a java class. For Instance I use system, which is also used a class used in the following method.
public static final int System=98;
public final boolean synpred196_InternalDummyParser() {
state.backtracking++;
int start = input.mark();
try {
synpred196_InternalFoStructuresParser_fragment(); // can never throw exception
} catch (RecognitionException re) {
System.err.println("impossible: "+re);
}
boolean success = !state.failed;
input.rewind(start);
state.backtracking--;
state.failed=false;
return success;
}
Is there a way to fix this. With XText 2.6.2 was everything working fine, but I have this error using XText 2.7.2.
What comes to my mind is a post-processing step on the grammar that replaces System.err.println by some serious exception. Meanwhile you may want to file a ticket against Xtext.
it's my first time trying akka - and I'm so stuck.
I found some tutorial code on the official akka website which sets up a supervisor strategy but I keep getting the following 4 compilation errors:
Incompatible types.
Required: akka.actor.SupervisorStrategy.Directive
Found: akka.actor.SupervisorStrategy.Resume <--same for Restart, Stop, Escalate
My gut feeling tells me that I got something really basic wrong, below are the libriaries I used and a minimum failing example.
Cheers, Seb
libraries: scala-library:2.11.2, akka-actor_2.11:2.3.5, jdk 1.7.0_45
import akka.actor.OneForOneStrategy;
import akka.actor.SupervisorStrategy;
import akka.actor.UntypedActor;
import akka.japi.Function;
import scala.concurrent.duration.Duration;
import static akka.actor.SupervisorStrategy.*;
class MyWorker extends UntypedActor {
private static SupervisorStrategy supervisorStrategy =
new OneForOneStrategy(10, Duration.create("1 minute"),
new Function<Throwable, Directive>() {
#Override
public Directive apply(Throwable t) {
if (t instanceof ArithmeticException) {
return SupervisorStrategy.resume();
} else if (t instanceof NullPointerException) {
return restart();
} else if (t instanceof IllegalArgumentException) {
return stop();
} else {
return escalate();
}
}
});
#Override
public void onReceive(Object message) throws Exception {
}
}
Solved. It was two issues:
a) turned out that IntelliJ IDEA 12 had seems to have had problems with Scala. Version 13.1 works like a charm out of the box.
b) maven stopped complaining about a missing SupervisorStrategy$1.class after I made the static imports explicit (e.g. import static akka.actor.SupervisorStrategy.resume).