Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

review: feature: CtElement#getRoleInParent() #1669

Merged
merged 2 commits into from
Nov 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 the {@link CtRole} of the parent's attribute where this element is used.
* Returns null if parent doesn't contain this element as direct children or if this element has no parent.
*/
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
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