From 8c33468297c4dd2ab9f5b3b1e715835aacf1d5cc Mon Sep 17 00:00:00 2001 From: Johannes Mey <johannes.mey@tu-dresden.de> Date: Mon, 23 Dec 2019 19:40:57 +0100 Subject: [PATCH] work in progress --- .../src/main/jastadd/ProgramToScopeTree.jrag | 6 ++++ testprograms/simpleScope/A.java | 35 ++++++++++++++----- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/scope4j/src/main/jastadd/ProgramToScopeTree.jrag b/scope4j/src/main/jastadd/ProgramToScopeTree.jrag index a8c82ac..cb38283 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 ae1d74f..4a67af4 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); } -- GitLab