use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.
the class UselessStringValueOfRule method isPrimitive.
private static boolean isPrimitive(Node parent) {
boolean result = false;
if (parent instanceof ASTPrimaryExpression && parent.jjtGetNumChildren() == 1) {
Node child = parent.jjtGetChild(0);
if (child instanceof ASTPrimaryPrefix && child.jjtGetNumChildren() == 1) {
Node gc = child.jjtGetChild(0);
if (gc instanceof ASTName) {
ASTName name = (ASTName) gc;
NameDeclaration nd = name.getNameDeclaration();
if (nd instanceof VariableNameDeclaration && ((VariableNameDeclaration) nd).isPrimitiveType()) {
result = true;
}
} else if (gc instanceof ASTLiteral) {
result = !((ASTLiteral) gc).isStringLiteral();
}
}
}
return result;
}
use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.
the class ClassScope method addNameOccurrence.
public Set<NameDeclaration> addNameOccurrence(NameOccurrence occurrence) {
JavaNameOccurrence javaOccurrence = (JavaNameOccurrence) occurrence;
Set<NameDeclaration> declarations = findVariableHere(javaOccurrence);
if (!declarations.isEmpty() && (javaOccurrence.isMethodOrConstructorInvocation() || javaOccurrence.isMethodReference())) {
for (NameDeclaration decl : declarations) {
List<NameOccurrence> nameOccurrences = getMethodDeclarations().get(decl);
if (nameOccurrences == null) {
// search inner classes
for (ClassNameDeclaration innerClass : getClassDeclarations().keySet()) {
Scope innerClassScope = innerClass.getScope();
if (innerClassScope.contains(javaOccurrence)) {
innerClassScope.addNameOccurrence(javaOccurrence);
}
}
} else {
nameOccurrences.add(javaOccurrence);
Node n = javaOccurrence.getLocation();
if (n instanceof ASTName) {
((ASTName) n).setNameDeclaration(decl);
}
// TODO what to do with PrimarySuffix case?
}
}
} else if (!declarations.isEmpty() && !javaOccurrence.isThisOrSuper()) {
for (NameDeclaration decl : declarations) {
List<NameOccurrence> nameOccurrences = getVariableDeclarations().get(decl);
if (nameOccurrences == null) {
// search inner classes
for (ClassNameDeclaration innerClass : getClassDeclarations().keySet()) {
Scope innerClassScope = innerClass.getScope();
if (innerClassScope.contains(javaOccurrence)) {
innerClassScope.addNameOccurrence(javaOccurrence);
}
}
} else {
nameOccurrences.add(javaOccurrence);
Node n = javaOccurrence.getLocation();
if (n instanceof ASTName) {
((ASTName) n).setNameDeclaration(decl);
}
// TODO what to do with PrimarySuffix case?
}
}
}
return declarations;
}
use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.
the class ClassScope method findVariableHere.
protected Set<NameDeclaration> findVariableHere(JavaNameOccurrence occurrence) {
if (occurrence.isThisOrSuper() || className.equals(occurrence.getImage())) {
// Reference to ourselves!
return Collections.<NameDeclaration>singleton(classDeclaration);
}
Map<MethodNameDeclaration, List<NameOccurrence>> methodDeclarations = getMethodDeclarations();
Set<NameDeclaration> result = new HashSet<>();
if (occurrence.isMethodOrConstructorInvocation()) {
final boolean hasAuxclasspath = getEnclosingScope(SourceFileScope.class).hasAuxclasspath();
matchMethodDeclaration(occurrence, methodDeclarations.keySet(), hasAuxclasspath, result);
if (isEnum && "valueOf".equals(occurrence.getImage())) {
result.add(createBuiltInMethodDeclaration("valueOf", "String"));
}
if (result.isEmpty()) {
for (ClassNameDeclaration innerClass : getClassDeclarations().keySet()) {
matchMethodDeclaration(occurrence, innerClass.getScope().getDeclarations(MethodNameDeclaration.class).keySet(), hasAuxclasspath, result);
}
}
return result;
}
if (occurrence.isMethodReference()) {
for (MethodNameDeclaration mnd : methodDeclarations.keySet()) {
if (mnd.getImage().equals(occurrence.getImage())) {
result.add(mnd);
}
}
return result;
}
List<String> images = new ArrayList<>();
if (occurrence.getImage() != null) {
images.add(occurrence.getImage());
if (occurrence.getImage().startsWith(className)) {
images.add(clipClassName(occurrence.getImage()));
}
}
Map<VariableNameDeclaration, List<NameOccurrence>> variableDeclarations = getVariableDeclarations();
ImageFinderFunction finder = new ImageFinderFunction(images);
Applier.apply(finder, variableDeclarations.keySet().iterator());
if (finder.getDecl() != null) {
result.add(finder.getDecl());
}
// search inner classes
Map<ClassNameDeclaration, List<NameOccurrence>> classDeclarations = getClassDeclarations();
if (result.isEmpty() && !classDeclarations.isEmpty()) {
for (ClassNameDeclaration innerClass : getClassDeclarations().keySet()) {
Applier.apply(finder, innerClass.getScope().getDeclarations(VariableNameDeclaration.class).keySet().iterator());
if (finder.getDecl() != null) {
result.add(finder.getDecl());
}
}
}
return result;
}
use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.
the class LocalScope method addNameOccurrence.
public Set<NameDeclaration> addNameOccurrence(NameOccurrence occurrence) {
JavaNameOccurrence javaOccurrence = (JavaNameOccurrence) occurrence;
Set<NameDeclaration> declarations = findVariableHere(javaOccurrence);
if (!declarations.isEmpty() && !javaOccurrence.isThisOrSuper()) {
for (NameDeclaration decl : declarations) {
List<NameOccurrence> nameOccurrences = getVariableDeclarations().get(decl);
nameOccurrences.add(javaOccurrence);
Node n = javaOccurrence.getLocation();
if (n instanceof ASTName) {
((ASTName) n).setNameDeclaration(decl);
}
// TODO what to do with PrimarySuffix case?
}
}
return declarations;
}
use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.
the class UseStringBufferLengthRule method visit.
@Override
public Object visit(ASTName decl, Object data) {
if (!decl.getImage().endsWith("toString")) {
return data;
}
NameDeclaration nd = decl.getNameDeclaration();
if (nd == null) {
return data;
}
if (alreadySeen.contains(nd) || !(nd instanceof TypedNameDeclaration) || nd instanceof TypedNameDeclaration && TypeHelper.isNeither((TypedNameDeclaration) nd, StringBuffer.class, StringBuilder.class)) {
return data;
}
alreadySeen.add(nd);
if (isViolation(decl)) {
addViolation(data, decl);
}
return data;
}
Aggregations