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

fix(noclasspath): superclass of enclosing class of static class was null #434

Merged
merged 1 commit into from
Dec 3, 2015
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
14 changes: 10 additions & 4 deletions src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,12 @@ public <T> CtTypeReference<T> getTypeReference(TypeBinding binding) {
packageBinding = (PackageBinding) someBinding;
} else {
packageBinding = context.compilationunitdeclaration.scope.environment.createPackage(chars);
if (packageBinding == null) {
// Big crisis here. We are already in noclasspath mode since the check `binding instance MissingTypeBinding`
// but JDT doesn't support always creation of a package in this mode. So, if we are in this brace, we make
// the job of JDT...
packageBinding = new PackageBinding(chars, null, context.compilationunitdeclaration.scope.environment);
}
}
ref.setPackage(getPackageReference(packageBinding));
break;
Expand Down Expand Up @@ -1005,10 +1011,10 @@ CtType<?> createType(TypeDeclaration typeDeclaration) {
// superclasses aren't in the same package and when their visibilities are "default".
List<ModifierKind> modifiers = Arrays.asList(ModifierKind.PUBLIC, ModifierKind.PROTECTED);
final TypeBinding resolvedType = typeDeclaration.superclass.resolvedType;
if (resolvedType instanceof MemberTypeBinding && resolvedType.enclosingType() != null && !getModifiers(resolvedType.enclosingType().modifiers).containsAll(modifiers)) {
typeDeclaration.superclass.resolvedType = new SpoonReferenceBinding(typeDeclaration.superclass.resolvedType.sourceName(),
(ReferenceBinding) typeDeclaration.enclosingType.superclass.resolvedType);
} else if (resolvedType instanceof BinaryTypeBinding && resolvedType.enclosingType() != null && !getModifiers(resolvedType.enclosingType().modifiers).containsAll(modifiers)) {
if ((resolvedType instanceof MemberTypeBinding || resolvedType instanceof BinaryTypeBinding)
&& resolvedType.enclosingType() != null
&& typeDeclaration.enclosingType.superclass != null
&& !getModifiers(resolvedType.enclosingType().modifiers).containsAll(modifiers)) {
typeDeclaration.superclass.resolvedType = new SpoonReferenceBinding(typeDeclaration.superclass.resolvedType.sourceName(),
(ReferenceBinding) typeDeclaration.enclosingType.superclass.resolvedType);
}
Expand Down
41 changes: 36 additions & 5 deletions src/test/java/spoon/test/ctClass/CtClassTest.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,67 @@
package spoon.test.ctClass;

import org.junit.Assert;
import org.junit.Test;
import spoon.Launcher;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtArrayTypeReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.test.TestUtils;
import spoon.test.ctClass.testclasses.Foo;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

public class CtClassTest {

@Test
public void getConstructor() throws Exception {
final Factory build = TestUtils.build(Foo.class);
final CtClass<?> foo = (CtClass<?>) build.Type().get(Foo.class);

Assert.assertEquals(3, foo.getConstructors().size());
assertEquals(3, foo.getConstructors().size());

CtTypeReference<Object> typeString = build.Code().createCtTypeReference(String.class);
CtConstructor<?> constructor = foo.getConstructor(typeString);
Assert.assertEquals(typeString, constructor.getParameters().get(0).getType());
assertEquals(typeString, constructor.getParameters().get(0).getType());

CtArrayTypeReference<Object> typeStringArray = build.Core().createArrayTypeReference();
typeStringArray.setComponentType(typeString);
constructor = foo.getConstructor(typeStringArray);
Assert.assertEquals(typeStringArray, constructor.getParameters().get(0).getType());
assertEquals(typeStringArray, constructor.getParameters().get(0).getType());

CtArrayTypeReference<Object> typeStringArrayArray = build.Core().createArrayTypeReference();
typeStringArrayArray.setComponentType(typeStringArray);
constructor = foo.getConstructor(typeStringArrayArray);
Assert.assertEquals(typeStringArrayArray, constructor.getParameters().get(0).getType());
assertEquals(typeStringArrayArray, constructor.getParameters().get(0).getType());
}

@Test
public void testParentOfTheEnclosingClassOfStaticClass() throws Exception {
// contract: When we have a static class which extends a superclass in the classpath,
// the enclosing class don't have a superclass. This is probably a bug in JDT but good
// luck to report a bug about noclasspath in their bugtracker. :)

final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/noclasspath/InvariantChecker.java");
launcher.addInputResource("./src/test/resources/noclasspath/FileIO.java");
launcher.addInputResource("./src/test/resources/noclasspath/Daikon.java");
launcher.setSourceOutputDirectory("./target/class");
launcher.getEnvironment().setNoClasspath(true);
launcher.run();

final CtClass<Object> aClass = launcher.getFactory().Class().get("daikon.tools.InvariantChecker");

final CtType<?> staticClass = aClass.getNestedType("InvariantCheckProcessor");
assertNotNull(staticClass);
assertEquals("InvariantCheckProcessor", staticClass.getSimpleName());
assertNotNull(staticClass.getSuperclass());
assertEquals("daikon.FileIO$Processor", staticClass.getSuperclass().getQualifiedName());
assertNull(aClass.getSuperclass());

TestUtils.canBeBuilt("./target/class", 8, true);
}
}
2 changes: 1 addition & 1 deletion src/test/java/spoon/test/literal/LiteralTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class LiteralTest {
@Test
public void testCharLiteralInNoClasspath() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/noclasspath/");
launcher.addInputResource("./src/test/resources/noclasspath/SecondaryIndexManager.java");
launcher.setSourceOutputDirectory("./target/literal");
launcher.getEnvironment().setNoClasspath(true);
launcher.run();
Expand Down
Loading