Skip to content
Snippets Groups Projects
Commit 37cc6846 authored by Johannes Mey's avatar Johannes Mey
Browse files

detect interface hiding, albeit only interface->class, not for sub-interfaces...

detect interface hiding, albeit only interface->class, not for sub-interfaces (but this is prohibited by java anyway and extendj collects all implemented superinterfaces automatically)
parent c958ec21
Branches
No related tags found
No related merge requests found
......@@ -35,6 +35,12 @@ aspect ProgramToScopeTree {
addInheritedScope(superDecl.asProtectedScope());
}
}
for (InterfaceDecl interfaceDecl : classDecl.implementedInterfaces()) {
System.out.println(classDecl.implementedInterfaces());
if (interfaceDecl.compilationUnit().fromSource()) {
addInheritedScope(interfaceDecl.asPackageScope());
}
}
}
super.updateInheritance();
}
......@@ -53,11 +59,11 @@ aspect ProgramToScopeTree {
EnhancedForStmt contributes scope() when !(getStmt() instanceof Block) to ASTNode.scope() for containingScope();
// collect all elements
Declarator contributes asDeclaration() when /* is NOT in interface || */ !isField() || isPrivate() to ASTNode.scope() for containingScope();
Declarator contributes asDeclaration() when !inInterface() && (!isField() || isPrivate()) to ASTNode.scope() for containingScope();
// field which is neither private, protected, nor public -> package-private scope
Declarator contributes asDeclaration() when isField() && !isPrivate() && !(isProtected() || isPublic()) to TypeDecl.packageScope() for containingScope();
// field which is either protected or public -> protected scope
Declarator contributes asDeclaration() when /* is in interface && */ isField() && !isPrivate() && (isProtected() || isPublic()) to TypeDecl.protectedScope() for containingScope();
Declarator contributes asDeclaration() when isField() && !isPrivate() && (isProtected() || isPublic() || inInterface()) to TypeDecl.protectedScope() for containingScope();
ParameterDeclaration contributes asDeclaration() to ASTNode.scope() for containingScope();
}
......@@ -156,4 +162,8 @@ aspect ScopeGenerationAttributes {
// allow host package to be called from all AST nodes
inh String ASTNode.hostPackage();
eq Program.getCompilationUnit(int i).hostPackage() = getCompilationUnit(i).getPackageDecl();
inh boolean Declarator.inInterface();
eq TypeDecl.getChild().inInterface() = false;
eq InterfaceDecl.getChild().inInterface() = true;
}
package org.extendj;
import org.extendj.ast.AbstractFinding;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Set;
public class InterfaceTest extends ScopeAnalysisTest {
@Test
void test() {
final String classA = "src/test/resources/interface/ClassA.java";
final String interfaceA = "src/test/resources/interface/InterfaceA.java";
final String interfaceB = "src/test/resources/interface/InterfaceB.java";
final String interfaceAorB = "src/test/resources/interface/Interface";
ScopeAnalysis scopeAnalysis = new ScopeAnalysis();
Set<AbstractFinding> findings = scopeAnalysis.analyze("src/test/resources/interface", true, true);
assertShadow(findings, "fieldA", classA, 2, interfaceA, 2);
assertShadow(findings, "fieldB", classA, 3, interfaceB, 3);
assertShadow(findings, "fieldC", classA, 4, interfaceAorB, 4);
Assertions.assertEquals(3, findings.size());
}
}
public class ClassA implements InterfaceA {
int fieldA = 1;
int fieldB = 1;
int fieldC = 1;
}
public interface InterfaceA extends InterfaceB {
int fieldA = 1;
int fieldC = 1;
}
public interface InterfaceB {
int fieldB = 2;
int fieldC = 2;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment