Skip to content

Commit

Permalink
introduces CtModel
Browse files Browse the repository at this point in the history
  • Loading branch information
monperrus committed Apr 18, 2016
1 parent 7659714 commit 4f40102
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 86 deletions.
8 changes: 8 additions & 0 deletions src/main/java/spoon/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@
import com.martiansoftware.jsap.JSAPResult;
import com.martiansoftware.jsap.Switch;
import com.martiansoftware.jsap.stringparsers.FileStringParser;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import spoon.compiler.Environment;
import spoon.compiler.SpoonCompiler;
import spoon.compiler.SpoonResource;
import spoon.compiler.SpoonResourceHelper;
import spoon.processing.Processor;
import spoon.reflect.CtModel;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
Expand Down Expand Up @@ -767,4 +770,9 @@ public void setBinaryOutputDirectory(File outputDirectory) {
modelBuilder.setBinaryOutputDirectory(outputDirectory);
}

@Override
public CtModel getModel() {
return factory.getModel();
}

}
8 changes: 6 additions & 2 deletions src/main/java/spoon/SpoonAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
*/
package spoon;

import java.io.File;

import spoon.compiler.Environment;
import spoon.compiler.SpoonCompiler;
import spoon.processing.Processor;
import spoon.reflect.CtModel;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.visitor.Filter;

import java.io.File;

/**
* Is the core entry point of Spoon. Implemented by Launcher.
*/
Expand Down Expand Up @@ -144,4 +145,7 @@ public interface SpoonAPI {
* Creates a new Spoon compiler (for building the model)
*/
SpoonCompiler createCompiler();

/** Returns the model built from the sources given via {@link #addInputResource(String)} */
CtModel getModel();
}
26 changes: 26 additions & 0 deletions src/main/java/spoon/reflect/CtModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package spoon.reflect;

import java.util.Collection;

import spoon.processing.Processor;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtType;

/** represents a Java program, modeled by a set of compile-time (Ct) objects
* where each objects is a program element (for instance, a CtClass represents a class).
*/
public interface CtModel {

/** returns the root package */
CtPackage getRootPackage();

/** returns all types of the model */
Collection<CtType<?>> getAllTypes();

/** returns all packages of the model */
Collection<CtPackage> getAllPackages();

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

}
93 changes: 93 additions & 0 deletions src/main/java/spoon/reflect/CtModelImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package spoon.reflect;

import java.util.Collection;
import java.util.Collections;

import spoon.processing.ProcessingManager;
import spoon.processing.Processor;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.ParentNotInitializedException;
import spoon.reflect.factory.Factory;
import spoon.reflect.visitor.CtVisitor;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.QueueProcessingManager;
import spoon.support.reflect.declaration.CtElementImpl;
import spoon.support.reflect.declaration.CtPackageImpl;

public class CtModelImpl implements CtModel {

private static class CtRootPackage extends CtPackageImpl {
{
this.setSimpleName(CtPackage.TOP_LEVEL_PACKAGE_NAME);
this.setParent(new CtElementImpl() {
@Override
public void accept(CtVisitor visitor) {

}

@Override
public CtElement getParent() throws ParentNotInitializedException {
return null;
}
});
}

// @Override
// public
// <T extends CtPackage> T addPackage(CtPackage pack) {
// packs.add(pack);
// return (T)this;
// }

@Override
public String getSimpleName() {
return super.getSimpleName();
}

@Override
public String getQualifiedName() {
return "";
}

@Override
public String toString() {
return packs.size() + " packages";
}

}

private CtPackage rootPackage = new CtRootPackage();

public CtModelImpl(Factory f) {
rootPackage.setFactory(f);
}

@Override
public CtPackage getRootPackage() {
return rootPackage;
}


@Override
public Collection<CtType<?>> getAllTypes() {
return Collections.unmodifiableCollection(rootPackage.getElements(new TypeFilter<CtType<?>>(CtType.class)));
}


@Override
public Collection<CtPackage> getAllPackages() {
return Collections.unmodifiableCollection(rootPackage.getElements(new TypeFilter<>(CtPackage.class)));
}


@Override
public void processWith(Processor<?> abstractProcessor) {
// processing (consume all the processors)
ProcessingManager processing = new QueueProcessingManager(rootPackage.getFactory());
processing.process(getRootPackage());
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public CompilationUnitFactory(Factory factory) {
super(factory);
}

Map<String, CompilationUnit> compilationUnits = new TreeMap<String, CompilationUnit>();
private transient Map<String, CompilationUnit> cachedCompilationUnits = new TreeMap<String, CompilationUnit>();

/**
* Gets the compilation unit map.
*
* @return a map (path -&gt; {@link CompilationUnit})
*/
public Map<String, CompilationUnit> getMap() {
return compilationUnits;
return cachedCompilationUnits;
}

/**
Expand All @@ -62,15 +62,15 @@ public CompilationUnit create() {
* Creates or gets a compilation unit for a given file path.
*/
public CompilationUnit create(String filePath) {
CompilationUnit cu = compilationUnits.get(filePath);
CompilationUnit cu = cachedCompilationUnits.get(filePath);
if (cu == null) {
if ("".equals(filePath)) {
cu = factory.Core().createVirtualCompilationUnit();
return cu;
}
cu = factory.Core().createCompilationUnit();
cu.setFile(new File(filePath));
compilationUnits.put(filePath, cu);
cachedCompilationUnits.put(filePath, cu);
}
return cu;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/spoon/reflect/factory/Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package spoon.reflect.factory;

import spoon.compiler.Environment;
import spoon.reflect.CtModel;

/**
* Provides the sub-factories required by Spoon.
Expand All @@ -27,6 +28,9 @@
*/
public interface Factory {

/** returns the Spoon model that has been built with this factory or one of its subfactories */
CtModel getModel();

CoreFactory Core(); // used 238 times

TypeFactory Type(); // used 107 times
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/spoon/reflect/factory/FactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@
*/
package spoon.reflect.factory;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import spoon.compiler.Environment;
import spoon.reflect.CtModel;
import spoon.reflect.CtModelImpl;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.declaration.CtAnnotationType;
import spoon.reflect.declaration.CtClass;
Expand All @@ -32,11 +39,6 @@
import spoon.support.DefaultInternalFactory;
import spoon.support.StandardEnvironment;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
* Implements {@link Factory}
*/
Expand Down Expand Up @@ -214,7 +216,7 @@ public MethodFactory Method() {
return methodF;
}

private PackageFactory packageF;
private transient PackageFactory packageF;

/**
* The {@link CtPackage} sub-factory.
Expand All @@ -227,7 +229,7 @@ public PackageFactory Package() {
return packageF;
}

private CompilationUnitFactory compilationUnit;
private transient CompilationUnitFactory compilationUnit;

/**
* The {@link CompilationUnit} sub-factory.
Expand Down Expand Up @@ -320,4 +322,11 @@ public String dedup(String symbol) {
return symbol;
}
}

private final CtModel model = new CtModelImpl(this);

@Override
public CtModel getModel() {
return model;
}
}
Loading

0 comments on commit 4f40102

Please sign in to comment.