Skip to content
Snippets Groups Projects
Commit b5e41085 authored by René Schöne's avatar René Schöne
Browse files

Added test for inner. Small cleanup in tests.

- anonymous inner classes defined in another source file are not reported yet
parent 5cf253a7
No related branches found
No related tags found
No related merge requests found
...@@ -137,7 +137,7 @@ public class ScopeAnalysis extends Frontend { ...@@ -137,7 +137,7 @@ public class ScopeAnalysis extends Frontend {
} }
private Program readProgram(Collection<String> files) throws IOException { private Program readProgram(Collection<String> files) throws IOException {
System.out.println("Reading " + (files.size() > 10 ? files.size() + " files" : files.toString()));
Program program = new Program(); Program program = new Program();
program.resetStatistics(); program.resetStatistics();
......
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 InnerTest extends ScopeAnalysisTest {
@Test
void test() {
ScopeAnalysis scopeAnalysis = new ScopeAnalysis();
Set<AbstractFinding> findings = scopeAnalysis.analyze("src/test/resources/inner", true, true);
System.out.println(findings);
// anonymous class
assertShadow(findings, "fieldA", 11, 13);
assertShadow(findings, "fieldA", 13, 3);
// local inner class
assertShadow(findings, "fieldA", 27, 29);
assertShadow(findings, "fieldA", 29, 3);
assertShadow(findings, "changingVar", 25, 19);
// static member class
assertShadow(findings, "fieldA", 37, 35);
assertShadow(findings, "fieldA", 35, 3);
// member class
assertShadow(findings, "fieldA", 44, 42);
assertShadow(findings, "fieldA", 42, 3);
// anonymous class defined in other class
assertShadow(findings, "fieldB", 5, 10);
// this finding is currently not found
// assertShadow(findings, "fieldB", 10, 4);
Assertions.assertEquals(10, findings.size());
}
}
public abstract class ClassA {
int fieldA;
int fieldB;
abstract void toBeDefined();
void method1() {
ClassA anonymous = new ClassA() {
void toBeDefined() {
int fieldA = 11;
}
int fieldA = 1;
};
}
void method2() {
final int finalVar = 1;
int changingVar = 0;
changingVar = 1; // changingVar is not-final and not-effective-final, thus can not be used in InnerA
class InnerA extends ClassA {
/* This variable shares the name, but actually could never reference the outer scope
We include it anyway, because a) it would obscure analysis for this edge-case, and b) warns for potentially
unwanted effects (as all shadowing-warnings do) */
int changingVar = 4;
void toBeDefined() {
int fieldA = 21 + changingVar + finalVar;
}
int fieldA = 2;
}
ClassA inner = new InnerA();
}
static class StaticMemberClass extends ClassA {
int fieldA = 3;
void toBeDefined() {
int fieldA = 31;
}
}
class MemberClass extends ClassA {
int fieldA = 4;
void toBeDefined() {
int fieldA = 41;
}
}
}
public class ClassB {
void anonymousClassFromOtherSourceFile() {
ClassA anonymous = new ClassA() {
void toBeDefined() {
int fieldB = 11;
}
/* false-negative. there should be two scopes: ClassA and this method of ClassB
* But there is only the method, thus, not shadowing of ClassA.fieldB is detected. */
int fieldB = 1;
}
}
}
...@@ -37,6 +37,7 @@ public abstract class ClassA { ...@@ -37,6 +37,7 @@ public abstract class ClassA {
) { /* do stuff */ } catch (java.io.IOException e) {/* do stuff */} ) { /* do stuff */ } catch (java.io.IOException e) {/* do stuff */}
} }
// this does not appear as a scope (and, more importantly, the parameters are not added anywhere else) // these do not appear as a scope (and, more importantly, the parameters are not added anywhere else)
public abstract void methodNameB(int parameterForAbstractMethodB); public abstract void methodNameB(int parameterForAbstractMethodB);
public abstract void methodNameC(int fieldA);
} }
public abstract class ClassA { public abstract class ClassA {
int fieldA; int fieldA;
int fieldB; int fieldB;
void m(); abstract void m();
void n() { void n() {
//... //...
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment