diff --git a/scope4j/src/main/jastadd/ProgramToScopeTree.jrag b/scope4j/src/main/jastadd/ProgramToScopeTree.jrag index a8c82ac90ad153228774a67d97b81e3b8ad9084b..cb3828343ebf6e843d2b85365e73129e4dffe28b 100644 --- a/scope4j/src/main/jastadd/ProgramToScopeTree.jrag +++ b/scope4j/src/main/jastadd/ProgramToScopeTree.jrag @@ -18,7 +18,9 @@ aspect ProgramToScopeTree { Block contributes scope() to ASTNode.scope() for containingScope(); // collect all elements + // TODO exclude declarators not in statements (for, enhancedfor, try) Declarator contributes asDeclaration() to ASTNode.scope() for containingScope(); + ResourceDeclaration contributes asDeclaration() to ASTNode.scope() for succeedingScope(); ParameterDeclaration contributes asDeclaration() to ASTNode.scope() for succeedingScope(); /** fallback attribute to ensure every AST element could pontentially be a scope */ @@ -66,4 +68,8 @@ aspect ScopeGenerationAttributes { eq BasicCatch.getParameter().succeedingScope() = getBlock(); eq MethodDecl.getParameter().succeedingScope() = getBlock(); eq ConstructorDecl.getParameter().succeedingScope() = getBlock(); + inh ASTNode ResourceDeclaration.succeedingScope(); + eq TryWithResources.getResource().succeedingScope() = getBlock(); + eq EnhancedForStmt.getVariableDecl().succeedingScope() = (getStmt() instanceof Block) ? getStmt() : null; + eq VarDeclStmt.getDeclarator().succeedingScope() = null; } diff --git a/testprograms/simpleScope/A.java b/testprograms/simpleScope/A.java index ae1d74f32209d04750c5ad89d888b95d7ef69817..4a67af48b44a952c603201ef2ff4aeb597b55cde 100644 --- a/testprograms/simpleScope/A.java +++ b/testprograms/simpleScope/A.java @@ -2,26 +2,43 @@ package p1; public abstract class ClassA { - A fieldA; + int fieldA; + int fieldB; public ClassA(int constructorParameterA) { - A localConstructorVarA = new A(); + int localConstructorVarA = 0; } public void methodNameA(int parameterA) { - A localVarA = new A(); + int localVarA = 1; + int localVarB = 1; { - A localVarInBlockA = new A(); + int localVarInBlockA = 2; - // this is shadowed: - A localVarA = new A(); + // this is shadowed (and forbidden) + int localVarA = 3; } - // this is shadowed (over two levels) - A fieldA; + // this is shadowed (over two levels, not forbidden) + int fieldA; + + try ( + // this is forbidden + java.util.zip.ZipFile localVarB = new java.util.zip.ZipFile("zipFileName"); + + // this is okay + java.io.BufferedWriter fieldB = java.nio.file.Files.newBufferedWriter(null) + ) { /* do stuff */ } catch (java.io.IOException e) {/* do stuff */} + } + + int i; + class Local { + { + for (int i = 0; i < 10; i++) System.out.println(i); + } } // this does not appear as a scope (and, more importantly, the parameters are not added anywhere else) - public void methodNameB(int parameterForAbstractMethodB); + public abstract void methodNameB(int parameterForAbstractMethodB); }