Skip to content

Commit

Permalink
fix(import): Skip findImport and createPackage if necessary. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
msteinbeck authored and GerardPaligot committed Jul 25, 2016
1 parent 4a89309 commit 76b3648
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/main/java/spoon/support/compiler/jdt/ReferenceBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,19 +249,24 @@ CtReference getDeclaringReferenceFromImports(char[] expectedName) {
this.jdtTreeBuilder.getContextBuilder().ignoreComputeImports = false;
return typeReference;
} else {
PackageBinding packageBinding = null;
char[][] chars = CharOperation.subarray(anImport.getImportName(), 0, anImport.getImportName().length - 1);
Binding someBinding = this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.scope.findImport(chars, false, false);
PackageBinding packageBinding;
if (someBinding != null && someBinding.isValidBinding() && someBinding instanceof PackageBinding) {
packageBinding = (PackageBinding) someBinding;
} else {
packageBinding = this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.scope.environment.createPackage(chars);
if (packageBinding == null) {
// Big crisis here. We are already in noclasspath mode 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, this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.scope.environment);
// `findImport(chars, false, false);` and `createPackage(chars)` require
// an array with a minimum length of 1 and throw an
// ArrayIndexOutOfBoundsException if `chars.length == 0`. Fixes #759.
if (chars.length > 0) {
Binding someBinding = this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.scope.findImport(chars, false, false);
if (someBinding != null && someBinding.isValidBinding() && someBinding instanceof PackageBinding) {
packageBinding = (PackageBinding) someBinding;
} else {
packageBinding = this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.scope.environment.createPackage(chars);
}
}
if (packageBinding == null) {
// Big crisis here. We are already in noclasspath mode 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, this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.scope.environment);
}
return getPackageReference(packageBinding);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package spoon.test.imports;

import org.junit.Assert;
import org.junit.Test;
import spoon.Launcher;
import spoon.reflect.declaration.CtType;
import spoon.reflect.reference.CtTypeReference;

import java.util.Collection;

public class ImportAndExtendWithPackageNameTest {

private static final String inputResource =
"./src/test/resources/import-resources/ImportAndExtendWithPackageName.java";

@Test
public void testBuildModel() {
final Launcher runLaunch = new Launcher();
runLaunch.getEnvironment().setNoClasspath(true);
runLaunch.addInputResource(inputResource);
runLaunch.buildModel();

final Collection<CtType<?>> types = runLaunch.getModel().getAllTypes();
Assert.assertSame(1, types.size());

final CtType type = types.iterator().next();
Assert.assertEquals("ImportAndExtendWithPackageName", type.getSimpleName());

final CtTypeReference superClass = type.getSuperclass();
Assert.assertEquals("LLkParser", superClass.getSimpleName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import antlr.*;

public class ImportAndExtendWithPackageName extends antlr.LLkParser {
public static void main(final String[] args) {}
}

0 comments on commit 76b3648

Please sign in to comment.