Skip to content

Commit

Permalink
feature: CtElement#getRoleInParent()
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky committed Oct 31, 2017
1 parent eb73422 commit 800ce51
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 24 deletions.
7 changes: 7 additions & 0 deletions src/main/java/spoon/reflect/declaration/CtElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import spoon.processing.FactoryAccessor;
import spoon.reflect.code.CtComment;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.path.CtRole;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtVisitable;
import spoon.reflect.visitor.Filter;
Expand Down Expand Up @@ -255,6 +256,12 @@ <E extends CtElement> List<E> getAnnotatedChildren(
*/
void updateAllParentsBelow();

/**
* @return {@link CtRole} of the parent's attribute where this element is used.
* returns null if parent doesn't links this element or if isParentInitialized()==false.
*/
CtRole getRoleInParent();

/*
* Deletes the element. For instance, delete a statement from its containing block. Warning: it may result in an incorrect AST, use at your own risk.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,11 @@ public abstract class CtVariableAccessImpl<T> extends CtExpressionImpl<T> implem

@Override
public CtVariableReference<T> getVariable() {
if (variable != null) {
return (CtVariableReference<T>) variable;
}
if (getFactory() != null) {
CtVariableReference<Object> ref = getFactory().Core().createLocalVariableReference();
ref.setParent(this);
return (CtVariableReference<T>) ref;
if (variable == null && getFactory() != null) {
variable = getFactory().Core().createLocalVariableReference();
variable.setParent(this);
}
return null;
return (CtVariableReference<T>) variable;
}

@Override
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/spoon/support/reflect/declaration/CtElementImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtScanner;
import spoon.reflect.visitor.DefaultJavaPrettyPrinter;
import spoon.reflect.visitor.EarlyTerminatingScanner;
import spoon.reflect.visitor.Filter;
import spoon.reflect.visitor.ModelConsistencyChecker;
import spoon.reflect.visitor.Query;
Expand Down Expand Up @@ -375,6 +376,25 @@ public boolean hasParent(CtElement candidate) {
}
}

@Override
public CtRole getRoleInParent() {
if (isParentInitialized()) {
EarlyTerminatingScanner<CtRole> ets = new EarlyTerminatingScanner<CtRole>() {
@Override
public void scan(CtRole role, CtElement element) {
if (element == CtElementImpl.this) {
setResult(role);
terminate();
}
//do not call super.scan, because we do not want scan children
}
};
getParent().accept(ets);
return ets.getResult();
}
return null;
}

@Override
public void updateAllParentsBelow() {
new ModelConsistencyChecker(getFactory().getEnvironment(), true, true).scan(this);
Expand Down
60 changes: 44 additions & 16 deletions src/test/java/spoon/test/main/MainTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package spoon.test.main;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.Assertion;
Expand All @@ -22,6 +23,7 @@
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtTypeParameter;
import spoon.reflect.declaration.ParentNotInitializedException;
import spoon.reflect.path.CtRole;
import spoon.reflect.reference.CtArrayTypeReference;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtFieldReference;
Expand All @@ -48,13 +50,19 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

public class MainTest {

@Test
public void testMain() throws Exception {


static Launcher launcher;
static CtPackage rootPackage;

/**
* load model once into static variable and use it for more read-only tests
*/
@BeforeClass
public static void loadModel() {
// we have to remove the test-classes folder
// so that the precondition of --source-classpath is not violated
// (target/test-classes contains src/test/resources which itself contains Java files)
Expand All @@ -67,7 +75,7 @@ public void testMain() throws Exception {
}
String systemClassPath = classpath.substring(0, classpath.length() - 1);

Launcher launcher = new Launcher();
launcher = new Launcher();

launcher.run(new String[] {
"-i", "src/main/java",
Expand All @@ -78,14 +86,23 @@ public void testMain() throws Exception {
"--compliance", "8",
"--level", "OFF"
});

rootPackage = launcher.getFactory().Package().getRootPackage();
}

@Test
public void testMain_checkGenericContracts() {
checkGenericContracts(rootPackage);
}

@Test
public void testMain_checkShadow() {
checkShadow(rootPackage);
}

checkGenericContracts(launcher.getFactory().Package().getRootPackage());

checkShadow(launcher.getFactory().Package().getRootPackage());

checkParentConsistency(launcher.getFactory().Package().getRootPackage());

checkModelIsTree(launcher.getFactory().Package().getRootPackage());
@Test
public void testMain_checkParentConsistency() {
checkParentConsistency(rootPackage);
}

public void checkGenericContracts(CtPackage pack) {
Expand Down Expand Up @@ -347,13 +364,12 @@ protected void exit(CtElement e) {
assertEquals("All parents have to be consistent", 0, inconsistentParents.size());
}



/*
* contract: each element is used only once
* For example this is always true: field.getType() != field.getDeclaringType()
*/
private void checkModelIsTree(CtPackage rootPackage) {
@Test
public void checkModelIsTree() {
Exception dummyException = new Exception("STACK");
PrinterHelper problems = new PrinterHelper(rootPackage.getFactory().getEnvironment());
Map<CtElement, Exception> allElements = new IdentityHashMap<>();
Expand Down Expand Up @@ -390,7 +406,19 @@ private String getStackTrace(Exception e) {
return sw.toString();
}


@Test
public void testMyRoleInParent() {
rootPackage.accept(new CtScanner() {
@Override
public void scan(CtRole role, CtElement element) {
if (element != null) {
//contract: getMyRoleInParent returns the expected parent
assertSame(role, element.getRoleInParent());
}
super.scan(role, element);
}
});
}

@Test
public void testTest() throws Exception {
Expand Down

0 comments on commit 800ce51

Please sign in to comment.