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 20, 2016
1 parent d9904df commit da6e269
Show file tree
Hide file tree
Showing 17 changed files with 411 additions and 100 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();
}
42 changes: 42 additions & 0 deletions src/main/java/spoon/reflect/CtModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (C) 2006-2015 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and
* abiding by the rules of distribution of free software. You can use, modify
* and/or redistribute the software under the terms of the CeCILL-C license as
* circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
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 top-level 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<?> processor);

}
115 changes: 115 additions & 0 deletions src/main/java/spoon/reflect/CtModelImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* Copyright (C) 2006-2015 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and
* abiding by the rules of distribution of free software. You can use, modify
* and/or redistribute the software under the terms of the CeCILL-C license as
* circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package spoon.reflect;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

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() {
List<CtType<?>> types = new ArrayList<CtType<?>>();
for (CtPackage pack : getAllPackages()) {
types.addAll(pack.getTypes());
}
return types;
}


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


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


}
2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/declaration/CtPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public interface CtPackage extends CtNamedElement {
String TOP_LEVEL_PACKAGE_NAME = "unnamed package";

/**
* Gets the declaring package of the current one.
* Gets the declaring package of the current one. Returns null if the package is not yet in another one.
*/
CtPackage getDeclaringPackage();

Expand Down
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 da6e269

Please sign in to comment.