use of net.sourceforge.pmd.lang.java.ast.AbstractJavaNode in project pmd by pmd.
the class PrematureDeclarationRule method blocksAfter.
/**
* Returns all the blocks found right after the node supplied within the its
* current scope.
*
* @param block
* SimpleJavaNode
* @param node
* SimpleNode
* @return List
*/
private static List<ASTBlockStatement> blocksAfter(AbstractJavaNode block, AbstractJavaNode node) {
int count = block.jjtGetNumChildren();
int start = indexOf(block, node.jjtGetParent()) + 1;
List<ASTBlockStatement> nextBlocks = new ArrayList<>(count);
for (int i = start; i < count; i++) {
Node maybeBlock = block.jjtGetChild(i);
if (maybeBlock instanceof ASTBlockStatement) {
nextBlocks.add((ASTBlockStatement) maybeBlock);
}
}
return nextBlocks;
}
use of net.sourceforge.pmd.lang.java.ast.AbstractJavaNode in project pmd by pmd.
the class UnusedPrivateFieldRule method usedInOuter.
private boolean usedInOuter(NameDeclaration decl, JavaNode body) {
List<ASTClassOrInterfaceBodyDeclaration> classOrInterfaceBodyDeclarations = body.findChildrenOfType(ASTClassOrInterfaceBodyDeclaration.class);
List<ASTEnumConstant> enumConstants = body.findChildrenOfType(ASTEnumConstant.class);
List<AbstractJavaNode> nodes = new ArrayList<>();
nodes.addAll(classOrInterfaceBodyDeclarations);
nodes.addAll(enumConstants);
for (AbstractJavaNode node : nodes) {
for (ASTPrimarySuffix primarySuffix : node.findDescendantsOfType(ASTPrimarySuffix.class, true)) {
if (decl.getImage().equals(primarySuffix.getImage())) {
// No violation
return true;
}
}
for (ASTPrimaryPrefix primaryPrefix : node.findDescendantsOfType(ASTPrimaryPrefix.class, true)) {
ASTName name = primaryPrefix.getFirstDescendantOfType(ASTName.class);
if (name != null) {
for (String id : name.getImage().split("\\.")) {
if (id.equals(decl.getImage())) {
// No violation
return true;
}
}
}
}
}
return false;
}
use of net.sourceforge.pmd.lang.java.ast.AbstractJavaNode in project pmd by pmd.
the class PrematureDeclarationRule method visit.
/**
* @param node
* ASTLocalVariableDeclaration
* @param data
* Object
* @return Object
* @see net.sourceforge.pmd.lang.java.ast.JavaParserVisitor#visit(ASTLocalVariableDeclaration,
* Object)
*/
public Object visit(ASTLocalVariableDeclaration node, Object data) {
// is it part of a for-loop declaration?
if (node.jjtGetParent() instanceof ASTForInit) {
// yes, those don't count
return visit((AbstractJavaNode) node, data);
}
String varName = varNameIn(node);
AbstractJavaNode grandparent = (AbstractJavaNode) node.jjtGetParent().jjtGetParent();
List<ASTBlockStatement> nextBlocks = blocksAfter(grandparent, node);
for (ASTBlockStatement block : nextBlocks) {
if (hasReferencesIn(block, varName) || isLambda(block)) {
break;
}
if (hasExit(block)) {
addViolation(data, node, varName);
break;
}
}
return visit((AbstractJavaNode) node, data);
}
use of net.sourceforge.pmd.lang.java.ast.AbstractJavaNode in project pmd by pmd.
the class ClassTypeResolverTest method testMethodParameterization.
@Test
public void testMethodParameterization() throws JaxenException, NoSuchMethodException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(GenericMethodsImplicit.class);
List<AbstractJavaNode> expressions = convertList(acu.findChildNodesWithXPath("//ArgumentList"), AbstractJavaNode.class);
JavaTypeDefinition context = forClass(GenericMethodsImplicit.class, forClass(Thread.class));
Method method = GenericMethodsImplicit.class.getMethod("bar", Object.class, Object.class, Integer.class, Object.class);
ASTArgumentList argList = (ASTArgumentList) expressions.get(0);
MethodType inferedMethod = MethodTypeResolution.parameterizeInvocation(context, method, argList);
assertEquals(inferedMethod.getParameterTypes().get(0), forClass(SuperClassA2.class));
assertEquals(inferedMethod.getParameterTypes().get(1), forClass(SuperClassA2.class));
assertEquals(inferedMethod.getParameterTypes().get(2), forClass(Integer.class));
assertEquals(inferedMethod.getParameterTypes().get(3), forClass(SuperClassAOther2.class));
}
use of net.sourceforge.pmd.lang.java.ast.AbstractJavaNode in project pmd by pmd.
the class ClassTypeResolverTest method testMethodInitialConstraints.
@Test
public void testMethodInitialConstraints() throws NoSuchMethodException, JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(GenericMethodsImplicit.class);
List<AbstractJavaNode> expressions = convertList(acu.findChildNodesWithXPath("//ArgumentList"), AbstractJavaNode.class);
List<Variable> variables = new ArrayList<>();
for (int i = 0; i < 2; ++i) {
variables.add(new Variable());
}
Method method = GenericMethodsImplicit.class.getMethod("bar", Object.class, Object.class, Integer.class, Object.class);
ASTArgumentList argList = (ASTArgumentList) expressions.get(0);
List<Constraint> constraints = MethodTypeResolution.produceInitialConstraints(method, argList, variables);
assertEquals(constraints.size(), 3);
// A
assertTrue(constraints.contains(new Constraint(forClass(SuperClassA.class), variables.get(0), LOOSE_INVOCATION)));
assertTrue(constraints.contains(new Constraint(forClass(SuperClassAOther.class), variables.get(0), LOOSE_INVOCATION)));
// B
assertTrue(constraints.contains(new Constraint(forClass(SuperClassAOther2.class), variables.get(1), LOOSE_INVOCATION)));
}
Aggregations