Skip to content

Commit

Permalink
refactor: CompilationUnit#create renamed to getOrCreate (#1645)
Browse files Browse the repository at this point in the history
  • Loading branch information
surli authored and monperrus committed Oct 24, 2017
1 parent 501c19b commit 4c9b713
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public CompilationUnit create() {
/**
* Creates or gets a compilation unit for a given file path.
*/
public CompilationUnit create(String filePath) {
public CompilationUnit getOrCreate(String filePath) {
CompilationUnit cu = cachedCompilationUnits.get(filePath);
if (cu == null) {
if (filePath.startsWith(JDTSnippetCompiler.SNIPPET_FILENAME_PREFIX)) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/support/JavaOutputProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void createJavaFile(CtType<?> element) {
// this is a top level type (see check above)
// if the compilation unit is not set, we use a default one
if (cu == null) {
cu = element.getFactory().CompilationUnit().create(element.getQualifiedName());
cu = element.getFactory().CompilationUnit().getOrCreate(element.getQualifiedName());
cu.setDeclaredPackage(element.getPackage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class JDTCommentBuilder {
this.sourceUnit = declarationUnit.compilationResult.compilationUnit;
this.contents = sourceUnit.getContents();
this.filePath = CharOperation.charToString(sourceUnit.getFileName());
this.spoonUnit = factory.CompilationUnit().create(filePath);
this.spoonUnit = factory.CompilationUnit().getOrCreate(filePath);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class JDTImportBuilder {
this.sourceUnit = declarationUnit.compilationResult.compilationUnit;
this.filePath = CharOperation.charToString(sourceUnit.getFileName());
// get the CU: it has already been built during model building in JDTBasedSpoonCompiler
this.spoonUnit = factory.CompilationUnit().create(filePath);
this.spoonUnit = factory.CompilationUnit().getOrCreate(filePath);
this.imports = new HashSet<>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ public boolean visit(Javadoc javadoc, ClassScope scope) {
@Override
public boolean visit(CompilationUnitDeclaration compilationUnitDeclaration, CompilationUnitScope scope) {
context.compilationunitdeclaration = scope.referenceContext;
context.compilationUnitSpoon = getFactory().CompilationUnit().create(new String(context.compilationunitdeclaration.getFileName()));
context.compilationUnitSpoon = getFactory().CompilationUnit().getOrCreate(new String(context.compilationunitdeclaration.getFileName()));
context.compilationUnitSpoon.setDeclaredPackage(getFactory().Package().getOrCreate(CharOperation.toString(scope.currentPackageName)));
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ SourcePosition buildPosition(int sourceStart, int sourceEnd) {

SourcePosition buildPositionCtElement(CtElement e, ASTNode node) {
CoreFactory cf = this.jdtTreeBuilder.getFactory().Core();
CompilationUnit cu = this.jdtTreeBuilder.getFactory().CompilationUnit().create(new String(this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.getFileName()));
CompilationUnit cu = this.jdtTreeBuilder.getFactory().CompilationUnit().getOrCreate(new String(this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.getFileName()));
CompilationResult cr = this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.compilationResult;
int[] lineSeparatorPositions = cr.lineSeparatorPositions;
char[] contents = cr.compilationUnit.getContents();
Expand Down
6 changes: 1 addition & 5 deletions src/test/java/spoon/test/compilationunit/TestEncoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
import spoon.Launcher;
import spoon.reflect.cu.CompilationUnit;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.file.Files;

Expand All @@ -31,7 +27,7 @@ public void testIsoEncodingIsSupported() throws Exception {
launcher.getEnvironment().setEncoding(Charset.forName("ISO-8859-1"));
launcher.buildModel();

CompilationUnit cu = launcher.getFactory().CompilationUnit().create(resource.getPath());
CompilationUnit cu = launcher.getFactory().CompilationUnit().getOrCreate(resource.getPath());
assertEquals(content, cu.getOriginalSourceCode());
}
}
6 changes: 2 additions & 4 deletions src/test/java/spoon/test/pkg/PackageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.junit.Test;
import spoon.Launcher;
import spoon.OutputType;
import spoon.SpoonException;
import spoon.SpoonModelBuilder;
import spoon.compiler.Environment;
import spoon.compiler.SpoonResourceHelper;
Expand All @@ -13,7 +12,6 @@
import spoon.reflect.declaration.CtAnnotationType;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.visitor.DefaultJavaPrettyPrinter;
import spoon.reflect.visitor.PrettyPrinter;
Expand Down Expand Up @@ -148,7 +146,7 @@ public void testRenamePackageAndPrettyPrint() throws Exception {
assertEquals("otherName.test.pkg.testclasses.Foo", foo.getQualifiedName());

PrettyPrinter prettyPrinter = new DefaultJavaPrettyPrinter(spoon.getEnvironment());
prettyPrinter.calculate(spoon.getFactory().CompilationUnit().create("./src/test/java/spoon/test/pkg/testclasses/Foo.java"), Collections.singletonList(foo));
prettyPrinter.calculate(spoon.getFactory().CompilationUnit().getOrCreate("./src/test/java/spoon/test/pkg/testclasses/Foo.java"), Collections.singletonList(foo));
String result = prettyPrinter.getResult();

assertTrue(result.contains("package otherName.test.pkg.testclasses;"));
Expand All @@ -168,7 +166,7 @@ public void testRenamePackageAndPrettyPrintNoclasspath() throws Exception {
assertEquals("otherName.Test", foo.getQualifiedName());

PrettyPrinter prettyPrinter = new DefaultJavaPrettyPrinter(spoon.getEnvironment());
prettyPrinter.calculate(spoon.getFactory().CompilationUnit().create("./src/test/resources/noclasspath/app/Test.java"), Collections.singletonList(foo));
prettyPrinter.calculate(spoon.getFactory().CompilationUnit().getOrCreate("./src/test/resources/noclasspath/app/Test.java"), Collections.singletonList(foo));
String result = prettyPrinter.getResult();

assertTrue(result.contains("package otherName;"));
Expand Down

0 comments on commit 4c9b713

Please sign in to comment.