use of net.sourceforge.pmd.lang.symboltable.Scope in project pmd by pmd.
the class UnnecessaryLocalBeforeReturnRule method isInitDataModifiedAfterInit.
private boolean isInitDataModifiedAfterInit(final VariableNameDeclaration variableDeclaration, final ASTReturnStatement rtn) {
final ASTVariableInitializer initializer = variableDeclaration.getAccessNodeParent().getFirstDescendantOfType(ASTVariableInitializer.class);
if (initializer != null) {
final List<ASTName> referencedNames = initializer.findDescendantsOfType(ASTName.class);
for (final ASTName refName : referencedNames) {
// TODO : Shouldn't the scope allow us to search for a var name occurrences directly, moving up through parent scopes?
Scope scope = refName.getScope();
do {
final Map<VariableNameDeclaration, List<NameOccurrence>> declarations = scope.getDeclarations(VariableNameDeclaration.class);
for (final Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : declarations.entrySet()) {
if (entry.getKey().getName().equals(refName.getImage())) {
// Variable found! Check usage locations
for (final NameOccurrence occ : entry.getValue()) {
final ScopedNode location = occ.getLocation();
// Is it used after initializing our "unnecessary" local but before the return statement?
if (isAfter(location, initializer) && isAfter(rtn, location)) {
return true;
}
}
return false;
}
}
scope = scope.getParent();
} while (scope != null);
}
}
return false;
}
use of net.sourceforge.pmd.lang.symboltable.Scope in project pmd by pmd.
the class JUnitTestsShouldIncludeAssertRule method visit.
@Override
public Object visit(ASTMethodDeclaration method, Object data) {
if (isJUnitMethod(method, data)) {
if (!isExpectAnnotated(method.jjtGetParent())) {
Scope classScope = method.getScope().getParent();
Map<String, List<NameOccurrence>> expectables = getRuleAnnotatedExpectedExceptions(classScope);
if (!containsExpectOrAssert(method.getBlock(), expectables)) {
addViolation(data, method);
}
}
}
return data;
}
use of net.sourceforge.pmd.lang.symboltable.Scope 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.Scope in project pmd by pmd.
the class OccurrenceFinder method visit.
public Object visit(ASTPrimaryExpression node, Object data) {
NameFinder nameFinder = new NameFinder(node);
declarations.clear();
additionalDeclarations.clear();
List<JavaNameOccurrence> names = nameFinder.getNames();
for (JavaNameOccurrence occ : names) {
Search search = new Search(occ);
if (declarations.isEmpty()) {
// doing the first name lookup
search.execute();
declarations.addAll(search.getResult());
if (declarations.isEmpty()) {
// SymbolNotFoundException
break;
}
} else {
for (NameDeclaration decl : declarations) {
// now we've got a scope we're starting with, so work from
// there
Scope startingScope = decl.getScope();
// outerClassRef.member. See also bug #1302
if (decl instanceof VariableNameDeclaration) {
String typeImage = ((VariableNameDeclaration) decl).getTypeImage();
ClassNameDeclaration clazzDeclaration = startingScope.getEnclosingScope(SourceFileScope.class).findClassNameDeclaration(typeImage);
if (clazzDeclaration != null) {
startingScope = clazzDeclaration.getScope();
}
}
search.execute(startingScope);
Set<NameDeclaration> result = search.getResult();
additionalDeclarations.addAll(result);
if (result.isEmpty()) {
// SymbolNotFoundException
break;
}
}
declarations.addAll(additionalDeclarations);
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.symboltable.Scope in project pmd by pmd.
the class ScopeAndDeclarationFinder method createClassScope.
/**
* Creates a new class scope for an AST node. The scope on top of the stack
* is set as the parent of the new scope, which is then also stored on the
* scope stack.
*
* @param node
* the AST node for which the scope has to be created.
* @throws java.util.EmptyStackException
* if the scope stack is empty.
*/
private void createClassScope(JavaNode node) {
Scope s = ((JavaNode) node.jjtGetParent()).getScope();
ClassNameDeclaration classNameDeclaration = new ClassNameDeclaration(node);
s.addDeclaration(classNameDeclaration);
if (node instanceof ASTClassOrInterfaceBody) {
addScope(new ClassScope(classNameDeclaration), node);
} else {
addScope(new ClassScope(node.getImage(), classNameDeclaration), node);
}
}
Aggregations