Java, typeres: fix checkstyle and mvn tests
This commit is contained in:
@ -74,7 +74,6 @@ import net.sourceforge.pmd.lang.java.ast.ASTUnaryExpression;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTUnaryExpressionNotPlusMinus;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
|
||||
import net.sourceforge.pmd.lang.java.ast.AbstractJavaNode;
|
||||
import net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode;
|
||||
import net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter;
|
||||
import net.sourceforge.pmd.lang.java.ast.Token;
|
||||
@ -338,56 +337,70 @@ public class ClassTypeResolver extends JavaParserVisitorAdapter {
|
||||
// make sure it handles same name classes and packages
|
||||
// TODO: handle generic static field cases
|
||||
|
||||
// handles cases where first part is a fully qualified name
|
||||
populateType(node, node.getImage());
|
||||
Class<?> accessingClass = getEnclosingTypeDeclarationClass(node);
|
||||
|
||||
if (node.getType() == null) {
|
||||
Class<?> accessingClass = getEnclosingTypeDeclarationClass(node);
|
||||
String[] dotSplitImage = node.getImage().split("\\.");
|
||||
ASTArguments astArguments = getSuffixMethodArgs(node);
|
||||
ASTArgumentList astArgumentList = null;
|
||||
int methodArgsArity = 0;
|
||||
|
||||
String[] dotSplitImage = node.getImage().split("\\.");
|
||||
ASTArguments astArguments = getSuffixMethodArgs(node);
|
||||
ASTArgumentList astArgumentList = null;
|
||||
int methodArgsArity = 0;
|
||||
if (astArguments != null) {
|
||||
astArgumentList = astArguments.getFirstChildOfType(ASTArgumentList.class);
|
||||
}
|
||||
|
||||
if (astArguments != null) {
|
||||
astArgumentList = astArguments.getFirstChildOfType(ASTArgumentList.class);
|
||||
}
|
||||
if (astArgumentList != null) {
|
||||
methodArgsArity = astArgumentList.jjtGetNumChildren();
|
||||
}
|
||||
|
||||
if (astArgumentList != null) {
|
||||
methodArgsArity = astArgumentList.jjtGetNumChildren();
|
||||
}
|
||||
JavaTypeDefinition previousType = null;
|
||||
if (dotSplitImage.length == 1 && astArguments != null) { // method
|
||||
|
||||
JavaTypeDefinition previousType = null;
|
||||
if (dotSplitImage.length == 1 && astArguments != null) { // method
|
||||
List<MethodType> methods = getLocalApplicableMethods(node, dotSplitImage[0], null,
|
||||
methodArgsArity, accessingClass);
|
||||
|
||||
List<MethodType> methods = getLocalApplicableMethods(node, dotSplitImage[0], null,
|
||||
methodArgsArity, accessingClass);
|
||||
previousType = getBestMethodReturnType(methods, astArgumentList, null);
|
||||
} else {
|
||||
previousType = getTypeDefinitionOfVariableFromScope(node.getScope(), dotSplitImage[0], accessingClass);
|
||||
}
|
||||
|
||||
previousType = getBestMethodReturnType(methods, astArgumentList, null);
|
||||
} else {
|
||||
previousType = getTypeDefinitionOfVariableFromScope(node.getScope(), dotSplitImage[0], accessingClass);
|
||||
}
|
||||
|
||||
for (int i = 1; i < dotSplitImage.length; ++i) {
|
||||
if (previousType == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (i == dotSplitImage.length - 1 && astArguments != null) { // method
|
||||
List<MethodType> methods = getApplicableMethods(previousType, dotSplitImage[i], null,
|
||||
methodArgsArity, accessingClass);
|
||||
|
||||
previousType = getBestMethodReturnType(methods, astArgumentList, null);
|
||||
} else { // field
|
||||
previousType = getFieldType(previousType, dotSplitImage[i], accessingClass);
|
||||
}
|
||||
}
|
||||
|
||||
if (previousType != null) {
|
||||
node.setTypeDefinition(previousType);
|
||||
// TODO: remove this if branch, it's only purpose is to make JUnitAssertionsShouldIncludeMessage's tests pass
|
||||
// as the code is not compiled there and symbol table works on uncompiled code
|
||||
if (node.getNameDeclaration() != null
|
||||
&& previousType == null // if it's not null, then let other code handle things
|
||||
&& node.getNameDeclaration().getNode() instanceof TypeNode) {
|
||||
// Carry over the type from the declaration
|
||||
Class<?> nodeType = ((TypeNode) node.getNameDeclaration().getNode()).getType();
|
||||
// FIXME : generic classes and class with generic super types could have the wrong type assigned here
|
||||
if (nodeType != null) {
|
||||
node.setType(nodeType);
|
||||
}
|
||||
}
|
||||
|
||||
if (node.getType() == null) {
|
||||
// handles cases where first part is a fully qualified name
|
||||
populateType(node, node.getImage());
|
||||
|
||||
if (node.getType() == null) {
|
||||
for (int i = 1; i < dotSplitImage.length; ++i) {
|
||||
if (previousType == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (i == dotSplitImage.length - 1 && astArguments != null) { // method
|
||||
List<MethodType> methods = getApplicableMethods(previousType, dotSplitImage[i], null,
|
||||
methodArgsArity, accessingClass);
|
||||
|
||||
previousType = getBestMethodReturnType(methods, astArgumentList, null);
|
||||
} else { // field
|
||||
previousType = getFieldType(previousType, dotSplitImage[i], accessingClass);
|
||||
}
|
||||
}
|
||||
|
||||
if (previousType != null) {
|
||||
node.setTypeDefinition(previousType);
|
||||
}
|
||||
}
|
||||
}
|
||||
return super.visit(node, data);
|
||||
}
|
||||
|
||||
@ -530,7 +543,7 @@ public class ClassTypeResolver extends JavaParserVisitorAdapter {
|
||||
&& prefix.jjtGetParent().jjtGetNumChildren() >= 2) {
|
||||
ASTArguments args = prefix.jjtGetParent().jjtGetChild(1).getFirstChildOfType(ASTArguments.class);
|
||||
// TODO: investigate if this will cause double visitation
|
||||
if(args != null) {
|
||||
if (args != null) {
|
||||
super.visit(args, null);
|
||||
}
|
||||
|
||||
@ -874,8 +887,8 @@ public class ClassTypeResolver extends JavaParserVisitorAdapter {
|
||||
|
||||
for (int childIndex = 0; childIndex < primaryNode.jjtGetNumChildren(); ++childIndex) {
|
||||
AbstractJavaTypeNode currentChild = (AbstractJavaTypeNode) primaryNode.jjtGetChild(childIndex);
|
||||
nextChild = childIndex + 1 < primaryNode.jjtGetNumChildren() ?
|
||||
(AbstractJavaTypeNode) primaryNode.jjtGetChild(childIndex + 1) : null;
|
||||
nextChild = childIndex + 1 < primaryNode.jjtGetNumChildren()
|
||||
? (AbstractJavaTypeNode) primaryNode.jjtGetChild(childIndex + 1) : null;
|
||||
|
||||
// skip children which already have their type assigned
|
||||
if (currentChild.getType() == null) {
|
||||
|
@ -1,11 +1,14 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.java.typeresolution;
|
||||
|
||||
|
||||
import net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition;
|
||||
|
||||
/**
|
||||
* This is really just a POJO.
|
||||
*/
|
||||
|
@ -16,8 +16,6 @@ import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
import net.sourceforge.pmd.typeresolution.testdata.MethodAccessibility;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.MethodPotentialApplicability;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.jaxen.JaxenException;
|
||||
import org.junit.Assert;
|
||||
@ -69,6 +67,8 @@ import net.sourceforge.pmd.typeresolution.testdata.FieldAccessShadow;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.FieldAccessSuper;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.InnerClass;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.Literals;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.MethodAccessibility;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.MethodPotentialApplicability;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.NestedAnonymousClass;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.Operators;
|
||||
import net.sourceforge.pmd.typeresolution.testdata.Promotion;
|
||||
@ -184,12 +184,13 @@ public class ClassTypeResolverTest {
|
||||
Assert.assertTrue(Comparator.class.isAssignableFrom(child.getType()));
|
||||
Assert.assertSame(Integer.class, child.getTypeDefinition().getGenericType(0).getType());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testNestedAnonymousClass() throws Exception {
|
||||
Node acu = parseAndTypeResolveForClass(NestedAnonymousClass.class, "1.8");
|
||||
ASTAllocationExpression allocationExpression = acu.getFirstDescendantOfType(ASTAllocationExpression.class);
|
||||
ASTAllocationExpression nestedAllocation = allocationExpression.getFirstDescendantOfType(ASTAllocationExpression.class);
|
||||
ASTAllocationExpression nestedAllocation
|
||||
= allocationExpression.getFirstDescendantOfType(ASTAllocationExpression.class);
|
||||
TypeNode child = (TypeNode) nestedAllocation.jjtGetChild(0);
|
||||
Assert.assertTrue(Converter.class.isAssignableFrom(child.getType()));
|
||||
Assert.assertSame(String.class, child.getTypeDefinition().getGenericType(0).getType());
|
||||
@ -1128,7 +1129,8 @@ public class ClassTypeResolverTest {
|
||||
assertEquals(String.class, getChildType(expressions.get(index++), 3));
|
||||
|
||||
// this.field.first = "";
|
||||
assertEquals(String.class, expressions.get(index).getType()); assertEquals(String.class, getChildType(expressions.get(index++), 2));
|
||||
assertEquals(String.class, expressions.get(index).getType());
|
||||
assertEquals(String.class, getChildType(expressions.get(index++), 2));
|
||||
|
||||
// Make sure we got them all
|
||||
assertEquals("All expressions not tested", index, expressions.size());
|
||||
@ -1149,7 +1151,7 @@ public class ClassTypeResolverTest {
|
||||
assertEquals(int.class, expressions.get(index).getType());
|
||||
assertEquals(int.class, getChildType(expressions.get(index), 0));
|
||||
assertEquals(int.class, getChildType(expressions.get(index++), 1));
|
||||
|
||||
|
||||
// int b = vararg("", 10);
|
||||
assertEquals(int.class, expressions.get(index).getType());
|
||||
assertEquals(int.class, getChildType(expressions.get(index), 0));
|
||||
@ -1181,6 +1183,7 @@ public class ClassTypeResolverTest {
|
||||
|
||||
@Test
|
||||
public void testMethodAccessibility() throws JaxenException {
|
||||
// ASTCompilationUnit acu = parseAndTypeResolveForClass15(Dummy.class);
|
||||
ASTCompilationUnit acu = parseAndTypeResolveForClass15(MethodAccessibility.class);
|
||||
|
||||
List<AbstractJavaTypeNode> expressions = convertList(
|
||||
|
@ -1,3 +1,7 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.typeresolution.testdata;
|
||||
|
||||
import net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassA;
|
||||
|
@ -1,3 +1,7 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.typeresolution.testdata;
|
||||
|
||||
public class MethodPotentialApplicability {
|
||||
@ -24,15 +28,25 @@ public class MethodPotentialApplicability {
|
||||
}
|
||||
|
||||
// test if variable arity with arity n -> then call arity >= n-1
|
||||
int vararg(String b, int... a) { return 0;}
|
||||
int vararg(String b, int... a) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
Exception vararg(String a, String b, String c, int... d) {return null;}
|
||||
Exception vararg(String a, String b, String c, int... d) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// test no arguments
|
||||
Number noArguments() {return null;}
|
||||
Number noArguments() {
|
||||
return null;
|
||||
}
|
||||
|
||||
// test not vararg mathching arity
|
||||
String notVararg(int a, int b) {return null;}
|
||||
String notVararg(int a, int b) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Exception notVararg(int a) {return null;}
|
||||
Exception notVararg(int a) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -8,5 +8,7 @@ package net.sourceforge.pmd.typeresolution.testdata.dummytypes;
|
||||
public class SuperClassA extends SuperClassA2 {
|
||||
public SuperClassA s;
|
||||
|
||||
public SuperClassA inheritedA() { return null; }
|
||||
public SuperClassA inheritedA() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -9,5 +9,7 @@ public class SuperClassB extends SuperClassB2 {
|
||||
protected SuperClassB bs;
|
||||
private String privateShadow;
|
||||
|
||||
public SuperClassB inheritedB() {return null;}
|
||||
public SuperClassB inheritedB() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user