Skip to content

Commit

Permalink
fix(precompile): specifies and fixes precompile option
Browse files Browse the repository at this point in the history
  • Loading branch information
monperrus committed Nov 1, 2016
1 parent 7e6f111 commit 3e7299c
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 29 deletions.
15 changes: 0 additions & 15 deletions doc/command_line.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,6 @@ Options :
[--compile]
Enable compilation and output class files.

[--precompile]
Enable pre-compilation of input source files before processing. Compiled
classes will be added to the classpath so that they are accessible to
the processing manager (typically, processors, annotations, and
templates should be pre-compiled most of the time).

[--buildOnlyOutdatedFiles]
Set Spoon to build only the source files that have been modified since
the latest source code generation, for performance purpose. Note that
this option requires to have the --ouput-type option not set to none.
This option is not appropriate to all kinds of processing. In particular
processings that implement or rely on a global analysis should avoid
this option because the processor will only have access to the outdated
source code (the files modified since the latest processing).

[--lines]
Set Spoon to try to preserve the original line numbers when generating
the source code (may lead to human-unfriendly formatting).
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/spoon/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,7 @@ protected static JSAP defineArgs() {
// Enable pre-compilation
sw1 = new Switch("precompile");
sw1.setLongFlag("precompile");
sw1.setHelp("Enable pre-compilation of input source files " + "before processing. Compiled classes " + "will be added to the classpath so that " + "they are accessible to the processing "
+ "manager (typically, processors, " + "annotations, and templates should be " + "pre-compiled most of the time).");
sw1.setHelp("[experimental] Enable pre-compilation of input source files " + "before processing. The compiled classes " + "will be added to the classpath.");
sw1.setDefault("false");
jsap.registerParameter(sw1);

Expand Down Expand Up @@ -468,6 +467,11 @@ protected void processArguments() {
}
}

if (jsapActualArgs.getBoolean("precompile")) {
modelBuilder.compile(InputType.FILES);
getEnvironment().setSourceClasspath(new String[]{getEnvironment().getBinaryOutputDirectory()});
}

if (getArguments().getFile("output") != null) {
setSourceOutputDirectory(getArguments().getFile("output"));
}
Expand Down Expand Up @@ -570,10 +574,6 @@ public SpoonCompiler createCompiler(Factory factory) {
env.debugMessage("source classpath: " + Arrays.toString(comp.getSourceClasspath()));
env.debugMessage("template classpath: " + Arrays.toString(comp.getTemplateClasspath()));

if (jsapActualArgs.getBoolean("precompile")) {
comp.compile(InputType.FILES);
}

return comp;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/spoon/support/QueueProcessingManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public boolean addProcessor(Processor<?> p) {
@SuppressWarnings("unchecked")
public void addProcessor(String qualifiedName) {
try {
addProcessor((Class<? extends Processor<?>>) Thread.currentThread().getContextClassLoader().loadClass(qualifiedName));
addProcessor((Class<? extends Processor<?>>) getFactory().getEnvironment().getClassLoader().loadClass(qualifiedName));
} catch (ClassNotFoundException e) {
throw new SpoonException("Unable to load processor \"" + qualifiedName + "\" - Check your classpath. Did you use the --precompile option?", e);
throw new SpoonException("Unable to load processor \"" + qualifiedName + "\" - Check your classpath.", e);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/spoon/support/RuntimeProcessingManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public boolean addProcessor(Processor<?> p) {
@SuppressWarnings("unchecked")
public void addProcessor(String qualifiedName) {
try {
addProcessor((Class<? extends Processor<?>>) Thread.currentThread().getContextClassLoader().loadClass(qualifiedName));
addProcessor((Class<? extends Processor<?>>) getFactory().getEnvironment().getClassLoader().loadClass(qualifiedName));
} catch (ClassNotFoundException e) {
factory.getEnvironment().report(null, Level.ERROR, "Unable to load processor \"" + qualifiedName + "\" - Check your classpath. Did you use the --precompile option?");
factory.getEnvironment().report(null, Level.ERROR, "Unable to load processor \"" + qualifiedName + "\" - Check your classpath.");
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/spoon/support/compiler/jdt/FileCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
*/
package spoon.support.compiler.jdt;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.eclipse.jdt.internal.compiler.batch.CompilationUnit;

import spoon.SpoonException;
import spoon.compiler.SpoonFile;

import java.util.ArrayList;
import java.util.List;

public class FileCompiler extends JDTBatchCompiler {

public FileCompiler(JDTBasedSpoonCompiler jdtCompiler) {
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/spoon/test/comment/CommentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.test.comment.testclasses.BlockComment;
import spoon.test.comment.testclasses.InlineComment;
Expand All @@ -31,6 +32,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class CommentTest {
Expand Down Expand Up @@ -472,4 +474,5 @@ public void testCodeFactory() {
assertEquals("// comment", comment.toString());
assertEquals(CtComment.CommentType.INLINE, comment.getCommentType());
}

}
33 changes: 33 additions & 0 deletions src/test/java/spoon/test/compilation/CompilationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
import spoon.reflect.factory.CodeFactory;
import spoon.reflect.factory.CoreFactory;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.compiler.jdt.FileCompiler;
import spoon.support.compiler.jdt.JDTBasedSpoonCompiler;
import spoon.support.reflect.reference.SpoonClassNotFoundException;
import spoon.test.compilation.testclasses.Bar;
import spoon.test.compilation.testclasses.IBar;
import spoon.testing.utils.ModelUtils;
Expand All @@ -31,6 +33,7 @@
import java.util.List;

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

Expand Down Expand Up @@ -229,4 +232,34 @@ public CompilationUnit[] getCompilationUnits() {

}

@Test
public void testPrecompile() {
// without precompile
Launcher l = new Launcher();
l.setArgs(new String[] {"--noclasspath", "-i", "src/test/resources/compilation/"});
l.buildModel();
CtClass klass = l.getFactory().Class().get("compilation.Bar");
// without precompile, actualClass does not exist (an exception is thrown)
try {
klass.getSuperInterfaces().toArray(new CtTypeReference[0])[0].getActualClass();
fail();
} catch (SpoonClassNotFoundException ignore) {}

// with precompile
Launcher l2 = new Launcher();
l2.setArgs(new String[] {"--precompile", "--noclasspath", "-i", "src/test/resources/compilation/"});
l2.buildModel();
CtClass klass2 = l2.getFactory().Class().get("compilation.Bar");
// with precompile, actualClass is not null
Class actualClass = klass2.getSuperInterfaces().toArray(new CtTypeReference[0])[0].getActualClass();
assertNotNull(actualClass);
assertEquals("IBar", actualClass.getSimpleName());

// precompile can be used to compile processors on the fly
Launcher l3 = new Launcher();
l3.setArgs(new String[] {"--precompile", "--noclasspath", "-i", "src/test/resources/compilation/", "-p", "compilation.SimpleProcessor"});
l3.run();
}


}
18 changes: 18 additions & 0 deletions src/test/resources/compilation/SimpleProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package compilation;

import spoon.processing.AbstractProcessor;
import spoon.reflect.declaration.CtType;

/**
* Simple Processor to demonstrate <code>--precompile</code> issue
*
* @author Michael Stocker
* @since 0.1.0
*/
public class SimpleProcessor extends AbstractProcessor<CtType<?>> {

@Override
public void process(CtType<?> element) {
System.out.println(">> Hello: " + element.getSimpleName() + " <<");
}
}
11 changes: 11 additions & 0 deletions src/test/resources/compilation/compilation-tests/Bar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package compilation;

public class Bar implements IBar {

@Override
public int m() {
return 1;
}
}

class FooEx extends RuntimeException {}
8 changes: 8 additions & 0 deletions src/test/resources/compilation/compilation-tests/IBar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package compilation;

/**
* Created by thomas on 28/10/16.
*/
public interface IBar {
int m();
}

0 comments on commit 3e7299c

Please sign in to comment.