#1317 RuntimeException when parsing class with multiple lambdas

Removing Java8MultipleLambdasTest from java8 integration, as it can
be tested with java < 8 in pmd-java
This commit is contained in:
Andreas Dangel
2015-03-07 21:12:04 +01:00
parent 59b14bd009
commit c74b43baf2
6 changed files with 56 additions and 61 deletions

View File

@ -40,14 +40,7 @@ public class LocalScope extends AbstractJavaScope {
throw new IllegalArgumentException("A LocalScope can contain only VariableNameDeclarations or ClassNameDeclarations. "
+ "Tried to add " + nameDecl.getClass() + "(" + nameDecl + ")");
}
if (nameDecl.getNode().jjtGetParent() instanceof ASTLambdaExpression
|| nameDecl.getNode().jjtGetParent().jjtGetParent().jjtGetParent() instanceof ASTLambdaExpression) {
// don't add the variable declaration, but verify, that there is no other local variable declaration
// with the same name - this would be a compiler error
checkForDuplicatedNameDeclaration(nameDecl);
} else {
super.addDeclaration(nameDecl);
}
super.addDeclaration(nameDecl);
}
public NameDeclaration findVariableHere(JavaNameOccurrence occurrence) {

View File

@ -20,6 +20,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTForStatement;
import net.sourceforge.pmd.lang.java.ast.ASTFormalParameters;
import net.sourceforge.pmd.lang.java.ast.ASTIfStatement;
import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTLambdaExpression;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator;
import net.sourceforge.pmd.lang.java.ast.ASTPackageDeclaration;
@ -266,6 +267,13 @@ public class ScopeAndDeclarationFinder extends JavaParserVisitorAdapter {
return data;
}
@Override
public Object visit(ASTLambdaExpression node, Object data) {
createLocalScope(node);
cont(node);
return data;
}
@Override
public Object visit(ASTTryStatement node, Object data) {
createLocalScope(node);

View File

@ -15,7 +15,7 @@ public abstract class STBBaseTst {
protected SymbolFacade stb;
protected void parseCode(String code) {
parseCode(code, LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.4"));
parseCode(code, LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion());
}
protected void parseCode15(String code) {
@ -23,7 +23,7 @@ public abstract class STBBaseTst {
}
protected void parseCode(String code, LanguageVersion languageVersion) {
LanguageVersionHandler languageVersionHandler = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler();
LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler();
acu = (ASTCompilationUnit)languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(code));
stb = new SymbolFacade();
stb.initializeWith(acu);

View File

@ -0,0 +1,44 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.symboltable;
import java.util.List;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
import net.sourceforge.pmd.lang.java.ast.ASTLambdaExpression;
import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
import org.junit.Assert;
import org.junit.Test;
public class ScopeAndDeclarationFinderTest extends STBBaseTst {
/**
* Unit test for https://sourceforge.net/p/pmd/bugs/1317/
*/
@Test
public void testJava8LambdaScoping() {
String source = "public class MultipleLambdas {\n" +
" Observer a = (o, arg) -> System.out.println(\"a:\" + arg);\n" +
" Observer b = (o, arg) -> System.out.println(\"b:\" + arg);\n" +
"}";
parseCode(source, LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.8"));
List<ASTLambdaExpression> lambdas = acu.findDescendantsOfType(ASTLambdaExpression.class);
Assert.assertEquals(2, lambdas.size());
LocalScope scope1 = (LocalScope)lambdas.get(0).getScope();
LocalScope scope2 = (LocalScope)lambdas.get(1).getScope();
Assert.assertNotSame(scope1, scope2);
for (ASTLambdaExpression l : lambdas) {
LocalScope scope = (LocalScope)l.getScope();
Assert.assertEquals(2, scope.getVariableDeclarations().size());
Assert.assertTrue(scope.contains(new JavaNameOccurrence(null, "o")));
Assert.assertTrue(scope.contains(new JavaNameOccurrence(null, "arg")));
NameDeclaration decl = scope.findVariableHere(new JavaNameOccurrence(null, "arg"));
Assert.assertEquals(1, scope.getVariableDeclarations().get(decl).size());
}
}
}

View File

@ -1,51 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.bugs;
import java.io.StringReader;
import org.junit.Ignore;
import org.junit.Test;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersionHandler;
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import net.sourceforge.pmd.lang.java.symboltable.SymbolFacade;
@Ignore
public class Java8MultipleLambdasTest {
private static final String MULTIPLE_JAVA_8_LAMBDAS =
"public class MultipleLambdas {" + PMD.EOL +
" Observer a = (o, arg) -> System.out.println(\"a_\" + arg);" + PMD.EOL +
" Observer b = (o, arg) -> System.out.println(\"b_\" + arg);" + PMD.EOL +
"}";
@Test
public void should_not_fail() {
parseCode(MULTIPLE_JAVA_8_LAMBDAS);
}
private void parseCode(String code) {
LanguageVersionHandler languageVersionHandler = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler();
ASTCompilationUnit acu = (ASTCompilationUnit) languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(code));
SymbolFacade stb = new SymbolFacade();
stb.initializeWith(acu);
}
// Was failing with :
// java.lang.RuntimeException: Variable: image = 'i', line = 3 is already in the symbol table
// at net.AbstractJavaScope.checkForDuplicatedNameDeclaration(AbstractJavaScope.java:27)
// at net.sourceforge.pmd.lang.java.symboltable.AbstractJavaScope.addDeclaration(AbstractJavaScope.java:21)
// at net.sourceforge.pmd.lang.java.symboltable.ScopeAndDeclarationFinder.visit(ScopeAndDeclarationFinder.java:294)
// at net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId.jjtAccept(ASTVariableDeclaratorId.java:30)
// at net.AbstractJavaNode.childrenAccept(AbstractJavaNode.java:55)
// at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:9)
// at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:455)
// at net.sourceforge.pmd.lang.java.ast.ASTLambdaExpression.jjtAccept(ASTLambdaExpression.java:21)
// at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.childrenAccept(AbstractJavaNode.java:55)
// at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:9)
// at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:312)
}

View File

@ -47,6 +47,7 @@
* [#1312](https://sourceforge.net/p/pmd/bugs/1312/): Rule reference must not override rule name of referenced rule
* [#1313](https://sourceforge.net/p/pmd/bugs/1313/): Missing assertion message in assertEquals with delta not detected
* [#1316](https://sourceforge.net/p/pmd/bugs/1316/): Multi Rule Properties with delimiter not possible
* [#1317](https://sourceforge.net/p/pmd/bugs/1317/): RuntimeException when parsing class with multiple lambdas
**API Changes:**