Skip to content

Commit

Permalink
adds support for incremental model (INRIA#584)
Browse files Browse the repository at this point in the history
  • Loading branch information
monperrus committed Apr 18, 2016
1 parent 4f40102 commit c97e8bd
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/CtModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public interface CtModel {
Collection<CtPackage> getAllPackages();

/** process this model with the given processor */
void processWith(Processor<?> abstractProcessor);
void processWith(Processor<?> processor);

}
2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/CtModelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public Collection<CtPackage> getAllPackages() {


@Override
public void processWith(Processor<?> abstractProcessor) {
public void processWith(Processor<?> processor) {
// processing (consume all the processors)
ProcessingManager processing = new QueueProcessingManager(rootPackage.getFactory());
processing.process(getRootPackage());
Expand Down
42 changes: 36 additions & 6 deletions src/main/java/spoon/support/reflect/declaration/CtPackageImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
*/
package spoon.support.reflect.declaration;

import java.util.Set;
import java.util.TreeSet;

import spoon.reflect.cu.SourcePosition;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtType;
import spoon.reflect.reference.CtPackageReference;
import spoon.reflect.visitor.CtVisitor;

import java.util.Set;
import java.util.TreeSet;

/**
* The implementation for {@link spoon.reflect.declaration.CtPackage}.
*
Expand All @@ -33,7 +33,7 @@
public class CtPackageImpl extends CtNamedElementImpl implements CtPackage {
private static final long serialVersionUID = 1L;

private Set<CtPackage> packs = new TreeSet<CtPackage>();
protected Set<CtPackage> packs = new TreeSet<CtPackage>();

private Set<CtType<?>> types = new TreeSet<CtType<?>>();

Expand All @@ -48,11 +48,41 @@ public void accept(CtVisitor v) {

@Override
public <T extends CtPackage> T addPackage(CtPackage pack) {
pack.setParent(this);
packs.add(pack);
// they are the same
if (this.getQualifiedName().equals(pack.getQualifiedName())) {
addAllTypes(pack, this);
addAllPackages(pack, this);
return (T) this;
}

// it already exists
for (CtPackage p1 : packs) {
if (p1.getQualifiedName().equals(pack.getQualifiedName())) {
addAllTypes(pack, p1);
addAllPackages(pack, p1);
return (T) this;
}
}

this.packs.add(pack);

return (T) this;
}

/** add all types of "from" in "to" */
private void addAllTypes(CtPackage from, CtPackage to) {
for (CtType t : from.getTypes()) {
to.addType(t);
}
}

/** add all packages of "from" in "to" */
private void addAllPackages(CtPackage from, CtPackage to) {
for (CtPackage p : from.getPackages()) {
to.addPackage(p);
}
}

@Override
public boolean removePackage(CtPackage pack) {
return packs.remove(pack);
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/spoon/test/factory/FactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,39 @@ public void testCtModel() throws Exception {
// [, spoon, spoon.test, spoon.test.factory, spoon.test.factory.testclasses]
assertEquals(5, model.getAllPackages().size());
}

@Test
public void testIncrementalModel() throws Exception {

// Feed some inputResources to a spoon compiler
SpoonAPI spoon = new Launcher();
spoon.addInputResource("src/test/java/spoon/test/factory/testclasses");

// Build model
spoon.buildModel();

// Do something with that model..
CtModel model = spoon.getModel();
model.processWith(new AbstractProcessor<CtMethod>() {
@Override
public void process(CtMethod element) {
element.setDefaultMethod(false);
}
});

// Feed some new inputResources
SpoonAPI spoon2 = new Launcher();
spoon2.addInputResource("src/test/java/spoon/test/factory/testclasses2");

// Build models of newly added classes/packages
spoon2.buildModel();

// attach them to the existing model.
model.getRootPackage().addPackage(spoon2.getModel().getRootPackage());

// checking the results
assertEquals(6, model.getAllPackages().size());
assertEquals(3, model.getAllTypes().size());
}

}
4 changes: 4 additions & 0 deletions src/test/java/spoon/test/factory/testclasses2/Baz.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package spoon.test.factory.testclasses2;

public class Baz {
}

0 comments on commit c97e8bd

Please sign in to comment.