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

fix(precompile): specifies and fixes precompile option #923

Merged
merged 1 commit into from
Nov 7, 2016
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
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
2 changes: 2 additions & 0 deletions src/test/java/spoon/test/comment/CommentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import spoon.reflect.factory.Factory;
import spoon.reflect.factory.FactoryImpl;
import spoon.reflect.visitor.filter.AbstractFilter;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.DefaultCoreFactory;
import spoon.support.StandardEnvironment;
Expand All @@ -47,6 +48,7 @@
import static org.apache.commons.io.IOUtils.write;
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
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();
}