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 refactor: CompilationUnit should return unmodifiable collection #1781

Merged
merged 3 commits into from
Dec 13, 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
5 changes: 5 additions & 0 deletions src/main/java/spoon/reflect/cu/CompilationUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ enum UNIT_TYPE {
*/
void setDeclaredTypes(List<CtType<?>> types);

/**
* Add a type to the list of declared types
*/
void addDeclaredType(CtType type);

/**
* Gets the declared module if the compilationUnit is "module-info.java"
*/
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/spoon/support/compiler/jdt/ParentExiter.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import spoon.reflect.code.BinaryOperatorKind;
import spoon.reflect.code.CtThisAccess;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtElement;
Expand Down Expand Up @@ -776,7 +777,10 @@ public void visitCtPackage(CtPackage ctPackage) {
}
ctPackage.addType((CtType<?>) child);
if (child.getPosition() != null && child.getPosition().getCompilationUnit() != null) {
child.getPosition().getCompilationUnit().getDeclaredTypes().add((CtType<?>) child);
CompilationUnit cu = child.getPosition().getCompilationUnit();
List<CtType<?>> declaredTypes = new ArrayList<>(cu.getDeclaredTypes());
declaredTypes.add((CtType<?>) child);
cu.setDeclaredTypes(declaredTypes);
}
return;
}
Expand Down
22 changes: 16 additions & 6 deletions src/main/java/spoon/support/reflect/cu/CompilationUnitImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

Expand All @@ -47,11 +48,6 @@ public class CompilationUnitImpl implements CompilationUnit, FactoryAccessor {

CtModule ctModule;


public List<CtType<?>> getDeclaredTypes() {
return declaredTypes;
}

File file;

@Override
Expand Down Expand Up @@ -81,10 +77,12 @@ public UNIT_TYPE getUnitType() {
}
}

@Override
public File getFile() {
return file;
}

@Override
public CtType<?> getMainType() {
if (getFile() == null) {
return getDeclaredTypes().get(0);
Expand All @@ -103,8 +101,20 @@ public CtType<?> getMainType() {
+ getDeclaredTypes());
}

@Override
public List<CtType<?>> getDeclaredTypes() {
return Collections.unmodifiableList(declaredTypes);
}

@Override
public void setDeclaredTypes(List<CtType<?>> types) {
this.declaredTypes = types;
this.declaredTypes.clear();
this.declaredTypes.addAll(types);
}

@Override
public void addDeclaredType(CtType type) {
this.declaredTypes.add(type);
}

@Override
Expand Down
56 changes: 56 additions & 0 deletions src/test/java/spoon/test/compilationunit/TestCompilationUnit.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package spoon.test.compilationunit;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import spoon.Launcher;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtType;
import spoon.support.JavaOutputProcessor;
import spoon.test.api.testclasses.Bar;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
* Created by urli on 18/08/2017.
Expand Down Expand Up @@ -71,4 +77,54 @@ public void testGetUnitTypeWorksWithCreatedObjects() {
cu.setDeclaredTypes(Collections.singletonList(launcher.getFactory().createClass()));
assertEquals(CompilationUnit.UNIT_TYPE.TYPE_DECLARATION, cu.getUnitType());
}

@Test
public void testCompilationUnitDeclaredTypes() throws IOException {
// contract: the list of declared types should be unmodifiable
File resource = new File("./src/test/java/spoon/test/model/Foo.java");
final Launcher launcher = new Launcher();
launcher.addInputResource(resource.getPath());
launcher.buildModel();

CompilationUnit cu = launcher.getFactory().CompilationUnit().getOrCreate(resource.getCanonicalPath());
assertEquals(3, cu.getDeclaredTypes().size());

List<CtType<?>> typeList = cu.getDeclaredTypes();
try {
typeList.remove(0);
fail();
} catch (UnsupportedOperationException e) {
// do nothing
}
}

@Test
public void testAddDeclaredTypeInCU() throws IOException {
// contract: when a type is added to a CU, it should also be pretty printed in cu mode
File resource = new File("./src/test/java/spoon/test/model/Foo.java");
final Launcher launcher = new Launcher();
launcher.setArgs(new String[]{ "--output-type", "compilationunits"});
launcher.addInputResource(resource.getPath());
launcher.setSourceOutputDirectory("./target/cu-onemoretype");
launcher.buildModel();

CompilationUnit cu = launcher.getFactory().CompilationUnit().getOrCreate(resource.getCanonicalPath());
assertEquals(3, cu.getDeclaredTypes().size());

CtType typeBla = launcher.getFactory().Class().create("spoon.test.model.Bla");
cu.addDeclaredType(typeBla);

assertEquals(4, cu.getDeclaredTypes().size());

launcher.prettyprint();

File output = new File("./target/cu-onemoretype/spoon/test/model/Foo.java");
List<String> lines = Files.readAllLines(output.toPath());

String fullContent = StringUtils.join(lines, "\n");
assertTrue(fullContent.contains("public class Foo"));
assertTrue(fullContent.contains("class Bar"));
assertTrue(fullContent.contains("class Baz"));
assertTrue(fullContent.contains("class Bla"));
}
}