Add test for Java 'this' keyword typeresolution
This commit is contained in:
@ -14,6 +14,9 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
import net.sourceforge.pmd.lang.java.ast.*;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.*;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.ThisExpression.*;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.jaxen.JaxenException;
|
||||
import org.junit.Assert;
|
||||
@ -23,36 +26,8 @@ import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||
import net.sourceforge.pmd.lang.LanguageVersionHandler;
|
||||
import net.sourceforge.pmd.lang.ast.Node;
|
||||
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTBooleanLiteral;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTExpression;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTLiteral;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTName;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTNullLiteral;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTReferenceType;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTType;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
|
||||
import net.sourceforge.pmd.lang.java.ast.TypeNode;
|
||||
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.typeresolution.ClassTypeResolver;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.AnonymousInnerClass;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.ArrayListFound;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.DefaultJavaLangImport;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.EnumWithAnonymousInnerClass;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.ExtraTopLevelClass;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.InnerClass;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.Literals;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.Operators;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.Promotion;
|
||||
|
||||
public class ClassTypeResolverTest {
|
||||
|
||||
@ -584,6 +559,41 @@ public class ClassTypeResolverTest {
|
||||
Assert.assertSame(StringTokenizer.class, id.getType());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testThisExpression() throws JaxenException {
|
||||
ASTCompilationUnit acu = parseAndTypeResolveForClass15(ThisExpression.class);
|
||||
|
||||
List<ASTPrimaryExpression> expressions = convertList(
|
||||
acu.findChildNodesWithXPath("//PrimaryExpression"),
|
||||
ASTPrimaryExpression.class);
|
||||
List<ASTPrimaryPrefix> prefixes = convertList(
|
||||
acu.findChildNodesWithXPath("//PrimaryPrefix"),
|
||||
ASTPrimaryPrefix.class);
|
||||
|
||||
int index = 0;
|
||||
|
||||
|
||||
assertEquals(ThisExpression.class, expressions.get(index).getType());
|
||||
assertEquals(ThisExpression.class, prefixes.get(index++).getType());
|
||||
assertEquals(ThisExpression.class, expressions.get(index).getType());
|
||||
assertEquals(ThisExpression.class, prefixes.get(index++).getType());
|
||||
assertEquals(ThisExpression.class, expressions.get(index).getType());
|
||||
assertEquals(ThisExpression.class, prefixes.get(index++).getType());
|
||||
assertEquals(ThisExpression.class, expressions.get(index).getType());
|
||||
assertEquals(ThisExpression.class, prefixes.get(index++).getType());
|
||||
|
||||
assertEquals(ThisExprNested.class, expressions.get(index).getType());
|
||||
assertEquals(ThisExprNested.class, prefixes.get(index++).getType());
|
||||
assertEquals(ThisExprStaticNested.class, expressions.get(index).getType());
|
||||
assertEquals(ThisExprStaticNested.class, prefixes.get(index++).getType());
|
||||
|
||||
// Make sure we got them all
|
||||
assertEquals("All expressions not tested", index, expressions.size());
|
||||
assertEquals("All expressions not tested", index, prefixes.size());
|
||||
}
|
||||
|
||||
|
||||
private ASTCompilationUnit parseAndTypeResolveForClass15(Class<?> clazz) {
|
||||
return parseAndTypeResolveForClass(clazz, "1.5");
|
||||
}
|
||||
|
22
pmd-java/src/test/java/net/sourceforge/pmd/typeresolution/testdata/ThisExpression.java
vendored
Normal file
22
pmd-java/src/test/java/net/sourceforge/pmd/typeresolution/testdata/ThisExpression.java
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
package net.sourceforge.pmd.typeresolution.testdata;
|
||||
|
||||
public class ThisExpression {
|
||||
public ThisExpression() { ThisExpression a = this; }
|
||||
|
||||
{ ThisExpression a = this; }
|
||||
|
||||
public void foo() {
|
||||
ThisExpression a = this;
|
||||
}
|
||||
|
||||
ThisExpression a = this;
|
||||
|
||||
public class ThisExprNested {
|
||||
ThisExprNested a = this;
|
||||
}
|
||||
|
||||
public static class ThisExprStaticNested {
|
||||
ThisExprStaticNested a = this;
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,14 @@ package net.sourceforge.pmd.typeresolution;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import net.sourceforge.pmd.lang.ast.Node;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.ThisExpression;
|
||||
import org.jaxen.JaxenException;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||
@ -16,6 +23,8 @@ import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.UsesJavaStreams;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.UsesRepeatableAnnotations;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ClassTypeResolverJava8Test {
|
||||
|
||||
@Test
|
||||
@ -28,6 +37,37 @@ public class ClassTypeResolverJava8Test {
|
||||
ASTCompilationUnit acu = parseAndTypeResolveForClass18(UsesRepeatableAnnotations.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThisExpression() throws JaxenException {
|
||||
ASTCompilationUnit acu = parseAndTypeResolveForClass18(ThisExpression.class);
|
||||
|
||||
List<ASTPrimaryExpression> expressions = convertList(
|
||||
acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression"),
|
||||
ASTPrimaryExpression.class);
|
||||
List<ASTPrimaryPrefix> prefixes = convertList(
|
||||
acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression/PrimaryPrefix"),
|
||||
ASTPrimaryPrefix.class);
|
||||
|
||||
int index = 0;
|
||||
|
||||
assertEquals(ThisExpression.class, expressions.get(index).getType());
|
||||
assertEquals(ThisExpression.class, prefixes.get(index++).getType());
|
||||
assertEquals(ThisExpression.PrimaryThisInterface.class, expressions.get(index).getType());
|
||||
assertEquals(ThisExpression.PrimaryThisInterface.class, prefixes.get(index++).getType());
|
||||
|
||||
// Make sure we got them all
|
||||
assertEquals("All expressions not tested", index, expressions.size());
|
||||
assertEquals("All expressions not tested", index, prefixes.size());
|
||||
}
|
||||
|
||||
private static <T> List<T> convertList(List<Node> nodes, Class<T> target) {
|
||||
List<T> converted = new ArrayList<>();
|
||||
for (Node n : nodes) {
|
||||
converted.add(target.cast(n));
|
||||
}
|
||||
return converted;
|
||||
}
|
||||
|
||||
private ASTCompilationUnit parseAndTypeResolveForClass18(Class<?> clazz) {
|
||||
return parseAndTypeResolveForClass(clazz, "1.8");
|
||||
}
|
||||
|
15
pmd-java8/src/test/java/net/sourceforge/pmd/typeresolution/testdata/ThisExpression.java
vendored
Normal file
15
pmd-java8/src/test/java/net/sourceforge/pmd/typeresolution/testdata/ThisExpression.java
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
package net.sourceforge.pmd.typeresolution.testdata;
|
||||
|
||||
public class ThisExpression {
|
||||
|
||||
public void foo() {
|
||||
((Runnable) (() -> { ThisExpression b = this; })).run();
|
||||
}
|
||||
|
||||
public interface PrimaryThisInterface {
|
||||
default void foo() {
|
||||
PrimaryThisInterface a = this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user