Skip to content

Commit

Permalink
fix: visibility detection issue in CtElement #1099 (#1102)
Browse files Browse the repository at this point in the history
* reproduce ElasticSearch access path problem #1099

* fix access path problem

* fix other tests

* Add one more assert on tests to check behaviour when overriding inner class. Replace some assertTrue by assertEquals to help debug test.
  • Loading branch information
pvojtechovsky authored and surli committed Jan 13, 2017
1 parent 7beb903 commit 551f0fb
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ public boolean canAccess(CtTypeReference<?> type) {
}
if (modifiers.contains(ModifierKind.PRIVATE)) {
//it is visible in scope of the same class only
return type.getTopLevelType().getQualifiedName().equals(this.getQualifiedName());
return type.getTopLevelType().getQualifiedName().equals(this.getTopLevelType().getQualifiedName());
}
//package protected
if (type.getTopLevelType().getPackage().getSimpleName().equals(this.getTopLevelType().getPackage().getSimpleName())) {
Expand Down Expand Up @@ -674,6 +674,9 @@ public CtTypeReference<?> getAccessType() {
}
declType = visibleDeclType;
}
if (declType == null) {
throw new SpoonException("Cannot compute access path to type: " + this.getQualifiedName() + " in context of type: " + contextType.getQualifiedName());
}
return declType;
}

Expand Down
37 changes: 32 additions & 5 deletions src/test/java/spoon/test/imports/ImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,11 @@ public void testNestedAccessPathWithTypedParameter() throws Exception {
}
CtClass<?> mm = launcher.getFactory().Class().get("spoon.test.imports.testclasses2.AbstractMapBasedMultimap");
CtClass<?> mmwli = launcher.getFactory().Class().get("spoon.test.imports.testclasses2.AbstractMapBasedMultimap$WrappedList$WrappedListIterator");
assertTrue(mmwli.toString().indexOf("AbstractMapBasedMultimap<K, V>.WrappedList.WrappedIterator")>=0);
assertTrue(mm.toString().indexOf("AbstractMapBasedMultimap<K, V>.WrappedList.WrappedIterator")>=0);
assertEquals("private class WrappedListIterator extends spoon.test.imports.testclasses2.AbstractMapBasedMultimap<K, V>.WrappedCollection.WrappedIterator {}",mmwli.toString());
assertTrue(mm.toString().indexOf("AbstractMapBasedMultimap<K, V>.WrappedCollection.WrappedIterator")>=0);

CtClass<?> mmwliother = launcher.getFactory().Class().get("spoon.test.imports.testclasses2.AbstractMapBasedMultimap$OtherWrappedList$WrappedListIterator");
assertEquals("private class WrappedListIterator extends spoon.test.imports.testclasses2.AbstractMapBasedMultimap<K, V>.OtherWrappedList.WrappedIterator {}",mmwliother.toString());

}

Expand All @@ -424,9 +427,12 @@ public void testNestedAccessPathWithTypedParameterWithImports() throws Exception

CtClass<?> mm = launcher.getFactory().Class().get("spoon.test.imports.testclasses2.AbstractMapBasedMultimap");
CtClass<?> mmwli = launcher.getFactory().Class().get("spoon.test.imports.testclasses2.AbstractMapBasedMultimap$WrappedList$WrappedListIterator");
assertTrue(mmwli.toString().indexOf("AbstractMapBasedMultimap<K, V>.WrappedList.WrappedIterator")>=0);
assertTrue(mm.toString().indexOf("AbstractMapBasedMultimap<K, V>.WrappedList.WrappedIterator")>=0);

assertEquals("private class WrappedListIterator extends AbstractMapBasedMultimap<K, V>.WrappedCollection.WrappedIterator {}",mmwli.toString());
assertTrue(mm.toString().indexOf("AbstractMapBasedMultimap<K, V>.WrappedCollection.WrappedIterator")>=0);

CtClass<?> mmwliother = launcher.getFactory().Class().get("spoon.test.imports.testclasses2.AbstractMapBasedMultimap$OtherWrappedList$WrappedListIterator");
assertEquals("private class WrappedListIterator extends AbstractMapBasedMultimap<K, V>.OtherWrappedList.WrappedIterator {}",mmwliother.toString());

}

@Test
Expand Down Expand Up @@ -603,4 +609,25 @@ public void testShouldNotCreateAutoreference() {
assertTrue("The file should not contain a static import for NOFOLLOW_LINKS",!output.contains("import static java.nio.file.LinkOption.NOFOLLOW_LINKS;"));
canBeBuilt(outputDir, 7);
}

@Test
public void testAccessPath() {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/TransportIndicesShardStoresAction.java");
String outputDir = "./target/spooned-accessPath";
launcher.setSourceOutputDirectory(outputDir);
launcher.run();
CtType element = launcher.getFactory().Class().getAll().get(0);

PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();

List<CtType<?>> toPrint = new ArrayList<>();
toPrint.add(element);

prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
String output = prettyPrinter.getResult();

canBeBuilt(outputDir, 7);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package spoon.test.imports.testclasses;

import java.util.Queue;

public class TransportIndicesShardStoresAction {

private class AsyncShardStoresInfoFetches {

private Queue<InternalAsyncFetch.Response> fetchResponses;

private class InternalAsyncFetch {

public class Response {
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ private class WrappedList extends WrappedCollection {
private class WrappedListIterator extends WrappedIterator {
}
}

private class OtherWrappedList extends WrappedCollection {
private class WrappedListIterator extends WrappedIterator {
}

class WrappedIterator {

}
}
}

0 comments on commit 551f0fb

Please sign in to comment.