diff --git a/scope4j/src/main/jastadd/ProgramToScopeTree.jrag b/scope4j/src/main/jastadd/ProgramToScopeTree.jrag index 4de33186757abf83e4634daefc6facaca6f0114f..8a8ef7fa514a592b83d1242062aac46d58463aca 100644 --- a/scope4j/src/main/jastadd/ProgramToScopeTree.jrag +++ b/scope4j/src/main/jastadd/ProgramToScopeTree.jrag @@ -6,7 +6,11 @@ aspect ProgramToScopeTree { for (CompilationUnit compilationUnit : getCompilationUnitList()) for (TypeDecl typeDecl : compilationUnit.getTypeDeclList()) { if (typeDecl.isClassDecl()) { - tree.addElement(((ClassDecl)typeDecl).publicScope()); + // do not add the TypeDecl, if it has a superclass from source + ClassDecl classDecl = (ClassDecl)typeDecl; + if (!classDecl.superclass().compilationUnit().fromSource()) { + tree.addElement(((ClassDecl)typeDecl).publicScope()); + } } else { tree.addElement(typeDecl.scope()); } @@ -23,7 +27,15 @@ aspect ProgramToScopeTree { // collect all scopes TypeDecl contributes scope() when !isClassDecl() to ASTNode.scope() for containingScope(); - ClassDecl contributes publicScope() to ASTNode.scope() for containingScope(); + ClassDecl contributes publicScope() when isInnerClass() to ASTNode.scope() for containingScope(); + ClassDecl contributes publicScope() + when superclass().compilationUnit().fromSource() && !isInnerClass() && accessibleFromPackage(containingScope().hostPackage()) + to ClassDecl.packageScope() + for containingScope(); + ClassDecl contributes publicScope() + when superclass().compilationUnit().fromSource() && !isInnerClass() && !accessibleFromPackage(containingScope().hostPackage()) + to ClassDecl.protectedScope() + for containingScope(); Block contributes scope() to ASTNode.scope() for containingScope(); ForStmt contributes scope() when !(getStmt() instanceof Block) to ASTNode.scope() for containingScope(); EnhancedForStmt contributes scope() when !(getStmt() instanceof Block) to ASTNode.scope() for containingScope(); @@ -128,4 +140,16 @@ aspect ScopeGenerationAttributes { eq EnhancedForStmt.getVariableDecl().containingScope() = (getStmt() instanceof Block) ? getStmt() : this; eq ForStmt.getInitStmt().containingScope() = (getStmt() instanceof Block) ? getStmt() : this; eq ForStmt.getUpdateStmt().containingScope() = (getStmt() instanceof Block) ? getStmt() : this; + // belonging to visibility scope + eq CompilationUnit.getTypeDecl(int i).containingScope() { + if (getTypeDecl(i).isClassDecl()) { + TypeDecl superclass = ((ClassDecl)getTypeDecl(i)).superclass(); + return superclass.compilationUnit().fromSource() ? superclass : this; + } + return this; + } + + // allow host package to be called from all AST nodes + inh String ASTNode.hostPackage(); + eq Program.getCompilationUnit(int i).hostPackage() = getCompilationUnit(i).getPackageDecl(); } diff --git a/scope4j/src/test/java/org/extendj/FieldsTest.java b/scope4j/src/test/java/org/extendj/FieldsTest.java index 764db42371d541d1c6ecb80a696e70b66cd2e2ea..f08b58fe3ea8e6eaf3fb972ee1ac81a3cd56e663 100644 --- a/scope4j/src/test/java/org/extendj/FieldsTest.java +++ b/scope4j/src/test/java/org/extendj/FieldsTest.java @@ -14,14 +14,6 @@ public class FieldsTest extends ScopeAnalysisTest { ScopeAnalysis scopeAnalysis = new ScopeAnalysis(); Set<AbstractFinding> findings = scopeAnalysis.analyze("src/test/resources/fields", true, false); - -// assertShadow(findings, "fieldC", 19, 3); -// assertShadow(findings, "fieldB", 21, 4); -// assertRedefinition(findings, "fieldB", 2); -// assertRedefinition(findings, "fieldB", 4); - -// Assertions.assertEquals(4, findings.size()); - } } diff --git a/scope4j/src/test/java/org/extendj/SuperclassFieldsTest.java b/scope4j/src/test/java/org/extendj/SuperclassFieldsTest.java index e32199d181284dd4a6528ac93ae15d76e04180b2..db6801480975694a6a521c223488930fd7994a18 100644 --- a/scope4j/src/test/java/org/extendj/SuperclassFieldsTest.java +++ b/scope4j/src/test/java/org/extendj/SuperclassFieldsTest.java @@ -13,15 +13,14 @@ public class SuperclassFieldsTest extends ScopeAnalysisTest { void test() { ScopeAnalysis scopeAnalysis = new ScopeAnalysis(); - Set<AbstractFinding> findings = scopeAnalysis.analyze("src/test/resources/superclassFields", true, false); + Set<AbstractFinding> findings = scopeAnalysis.analyze("src/test/resources/superclassFields", false, false); assertShadow(findings, "fieldC", 19, 3); - assertShadow(findings, "fieldB", 21, 4); - assertRedefinition(findings, "fieldB", 2); - assertRedefinition(findings, "fieldB", 4); + assertShadow(findings, "fieldB", 21, 2); + assertShadow(findings, "fieldB", 2, 4); - Assertions.assertEquals(4, findings.size()); + Assertions.assertEquals(3, findings.size()); }