use of net.sourceforge.pmd.lang.java.symboltable.ClassScope in project pmd by pmd.
the class ClassTypeResolver method getTypeDefinitionOfVariableFromScope.
/**
* Search for a field by it's image stating from a scope and taking into account if it's visible from the
* accessingClass Class. The method takes into account that Nested inherited fields shadow outer scope fields.
*
* @param scope The scope to start the search from.
* @param image The name of the field, local variable or method parameter.
* @param accessingClass The Class (which is defined in the current ACU) that is trying to access the field.
* @return Type def. of the field, or null if it could not be resolved.
*/
private JavaTypeDefinition getTypeDefinitionOfVariableFromScope(Scope scope, String image, Class<?> accessingClass) {
if (accessingClass == null) {
return null;
}
for (; /* empty */
scope != null; scope = scope.getParent()) {
// search each enclosing scope one by one
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : scope.getDeclarations(VariableNameDeclaration.class).entrySet()) {
if (entry.getKey().getImage().equals(image)) {
ASTType typeNode = entry.getKey().getDeclaratorId().getTypeNode();
if (typeNode == null) {
// TODO : Type is infered, ie, this is a lambda such as (var) -> var.equals(other)
return null;
}
if (typeNode.jjtGetChild(0) instanceof ASTReferenceType) {
return ((TypeNode) typeNode.jjtGetChild(0)).getTypeDefinition();
} else {
// primitive type
return JavaTypeDefinition.forClass(typeNode.getType());
}
}
}
// Nested class' inherited fields shadow enclosing variables
if (scope instanceof ClassScope) {
try {
// get the superclass type def. ot the Class the ClassScope belongs to
JavaTypeDefinition superClass = getSuperClassTypeDefinition(((ClassScope) scope).getClassDeclaration().getNode(), null);
// TODO: check if anonymous classes are class scope
// try searching this type def.
JavaTypeDefinition foundTypeDef = getFieldType(superClass, image, accessingClass);
if (foundTypeDef != null) {
// if null, then it's not an inherited field
return foundTypeDef;
}
} catch (ClassCastException ignored) {
// if there is an anonymous class, getClassDeclaration().getType() will throw
// TODO: maybe there is a better way to handle this, maybe this hides bugs
}
}
}
// will return null if not found
return searchImportedStaticFields(image);
}
use of net.sourceforge.pmd.lang.java.symboltable.ClassScope in project pmd by pmd.
the class UseCollectionIsEmptyRule method getTypeOfMethodCall.
private ASTClassOrInterfaceType getTypeOfMethodCall(ASTPrimarySuffix node) {
ASTClassOrInterfaceType type = null;
ASTName methodName = node.jjtGetParent().getFirstChildOfType(ASTPrimaryPrefix.class).getFirstChildOfType(ASTName.class);
if (methodName != null) {
ClassScope classScope = node.getScope().getEnclosingScope(ClassScope.class);
Map<MethodNameDeclaration, List<NameOccurrence>> methods = classScope.getMethodDeclarations();
for (Map.Entry<MethodNameDeclaration, List<NameOccurrence>> e : methods.entrySet()) {
if (e.getKey().getName().equals(methodName.getImage())) {
type = e.getKey().getNode().getFirstParentOfType(ASTMethodDeclaration.class).getFirstChildOfType(ASTResultType.class).getFirstDescendantOfType(ASTClassOrInterfaceType.class);
break;
}
}
}
return type;
}
use of net.sourceforge.pmd.lang.java.symboltable.ClassScope in project pmd by pmd.
the class AccessorMethodGenerationRule method analyzeScope.
private void analyzeScope(final AbstractJavaScope file, final Object data) {
for (final ClassNameDeclaration classDecl : file.getDeclarations(ClassNameDeclaration.class).keySet()) {
final ClassScope classScope = (ClassScope) classDecl.getScope();
// Check fields
for (final Map.Entry<VariableNameDeclaration, List<NameOccurrence>> varDecl : classScope.getVariableDeclarations().entrySet()) {
final ASTFieldDeclaration field = varDecl.getKey().getNode().getFirstParentOfType(ASTFieldDeclaration.class);
analyzeMember(field, varDecl.getValue(), classScope, data);
}
// Check methods
for (final Map.Entry<MethodNameDeclaration, List<NameOccurrence>> methodDecl : classScope.getMethodDeclarations().entrySet()) {
final ASTMethodDeclaration method = methodDecl.getKey().getNode().getFirstParentOfType(ASTMethodDeclaration.class);
analyzeMember(method, methodDecl.getValue(), classScope, data);
}
// Check inner classes
analyzeScope(classScope, data);
}
}
use of net.sourceforge.pmd.lang.java.symboltable.ClassScope in project pmd by pmd.
the class CouplingBetweenObjectsRule method checkVariableType.
/**
* performs a check on the variable and updates the counter. Counter is
* instance for a class and is reset upon new class scan.
*
* @param variableType
* The variable type.
*/
private void checkVariableType(Node nameNode, String variableType) {
// TODO - move this into the symbol table somehow?
if (nameNode.getParentsOfType(ASTClassOrInterfaceDeclaration.class).isEmpty()) {
return;
}
// if the field is of any type other than the class type
// increment the count
ClassScope clzScope = ((JavaNode) nameNode).getScope().getEnclosingScope(ClassScope.class);
if (!clzScope.getClassName().equals(variableType) && !this.filterTypes(variableType) && !this.typesFoundSoFar.contains(variableType)) {
couplingCount++;
typesFoundSoFar.add(variableType);
}
}
use of net.sourceforge.pmd.lang.java.symboltable.ClassScope in project pmd by pmd.
the class AccessorClassGenerationRule method visit.
@Override
public Object visit(final ASTAllocationExpression node, final Object data) {
if (node.jjtGetChild(0) instanceof ASTClassOrInterfaceType) {
// Ignore primitives
final ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) node.jjtGetChild(0);
final List<ASTConstructorDeclaration> constructors = privateConstructors.get(type.getImage());
if (constructors != null) {
final ASTArguments callArguments = node.getFirstChildOfType(ASTArguments.class);
// Is this really a constructor call and not an array?
if (callArguments != null) {
final ClassScope enclosingScope = node.getScope().getEnclosingScope(ClassScope.class);
for (final ASTConstructorDeclaration cd : constructors) {
// Are we within the same class scope?
if (cd.getScope().getEnclosingScope(ClassScope.class) == enclosingScope) {
break;
}
if (cd.getParameterCount() == callArguments.getArgumentCount()) {
// TODO : Check types
addViolation(data, node);
break;
}
}
}
}
}
return data;
}
Aggregations