spoon.compiler.Environment Java Examples

The following examples show how to use spoon.compiler.Environment. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpoonMojoGenerate.java    From spoon-maven-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void initSpoonProperties(Launcher launcher) throws MojoExecutionException {
	Environment environment = launcher.getEnvironment();

	for (ProcessorProperties processorProperties : this.getProcessorProperties()) {
		spoon.processing.ProcessorProperties properties = new ProcessorPropertiesImpl();

		Properties xmlProperties = processorProperties.getProperties();

		for (Object key : xmlProperties.keySet()) {
			String sKey = (String) key;
			String value = (String) xmlProperties.get(key);

			properties.set(sKey, value);
		}

		environment.setProcessorProperties(processorProperties.getName(), properties);
	}
}
 
Example #2
Source File: SpoonedFile.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
public void generateOutputFile(File destinationFolder) {
    Environment env = factory.getEnvironment();
    env.setSourceOutputDirectory(destinationFolder);
    JavaOutputProcessor javaOutputProcessor = new JavaOutputProcessor(prettyPrinter);
    javaOutputProcessor.setFactory(factory);
    Processor<?> writer = javaOutputProcessor;
    process(writer);
}
 
Example #3
Source File: Spoonerism.java    From spoon-examples with GNU General Public License v2.0 5 votes vote down vote up
Spoonerism writeTransformedClasses () {
    // And now just write the transformed classes
    Environment env = spoonUniverse.getEnvironment();
    // replace FQN with imports and short names
    env.setAutoImports(true);
    // include comments - source is for human consumption
    env.setCommentEnabled(true);

    // where to write
    spoonUniverse.setSourceOutputDirectory(OUT_DIR);
    // default printing, use a different printer for even fancier formatting
    spoonUniverse.prettyprint();
    return this;
}
 
Example #4
Source File: ProcessorMainTest.java    From spoon-examples with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void main() {
    String projectPath = ".";

    MavenLauncher launcher = new MavenLauncher(projectPath, MavenLauncher.SOURCE_TYPE.APP_SOURCE);

    Environment environment = launcher.getEnvironment();
    environment.setCommentEnabled(true);
    environment.setAutoImports(true);

    launcher.addProcessor(new APICheckingProcessor());
    launcher.run();
}
 
Example #5
Source File: MutationSupporter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public static Environment getEnvironment() {
	StandardEnvironment env = new StandardEnvironment();

	Integer complianceLevel = ConfigurationProperties.getPropertyInt("javacompliancelevel");
	env.setComplianceLevel((complianceLevel > 2) ? complianceLevel : 3);
	env.setVerbose(false);
	env.setDebug(true);
	env.setTabulationSize(5);
	env.useTabulations(true);
	return env;
}
 
Example #6
Source File: SpoonedFile.java    From nopol with GNU General Public License v2.0 4 votes vote down vote up
protected Environment spoonEnvironment() {
    return spoonFactory().getEnvironment();
}
 
Example #7
Source File: ReferenceProcessor.java    From spoon-examples with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void init() {
	ignoredTypes.add(getFactory().Type().createReference(Environment.class));
	ignoredTypes.add(getFactory().Type().createReference(Factory.class));
	ignoredTypes.add(getFactory().Type().createReference(FactoryAccessor.class));
}
 
Example #8
Source File: BigTransfoScenarioTest.java    From spoon-examples with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("all")
@Test
public void main() {
    MavenLauncher launcher = new MavenLauncher(
            "./src/test/resources/project/",
            MavenLauncher.SOURCE_TYPE.APP_SOURCE);

    CtModel model = launcher.buildModel();
    List<CtMethod> methodList = model.
            filterChildren(new NamedElementFilter<CtPackage>(CtPackage.class, "ow2con")).
            filterChildren(new NamedElementFilter<CtPackage>(CtPackage.class, "publicapi")).
            filterChildren(new TypeFilter<CtMethod>(CtMethod.class)).
            filterChildren(new Filter<CtMethod>() {
                @Override
                public boolean matches(CtMethod element) {
                    boolean isPublic = element.isPublic();
                    CtTypeReference returnType = element.getType();
                    String privateApiPackage = "ow2con.privateapi";
                    boolean isTypeFromPrivateApi = returnType.getQualifiedName().contains(privateApiPackage);
                    return isPublic && isTypeFromPrivateApi;
                }
            }).list();

    Factory factory = launcher.getFactory();
    CtClass<? extends Throwable> exceptionClass = factory.createClass("ow2con.PrivateAPIException");
    CtConstructorCall<? extends Throwable> exceptionInstance = factory.createConstructorCall(exceptionClass.getReference());

    for (CtMethod method : methodList) {
        CtBlock methodBody = method.getBody();
        List<CtComment> bodyComments = new ArrayList<>();

        ArrayList<CtStatement> ctStatements = new ArrayList<>(methodBody.getStatements());

        for (CtStatement ctStatement : ctStatements) {
            String statement = ctStatement.toString();
            CtComment statementAsComment = factory.createInlineComment(statement);
            bodyComments.add(statementAsComment);
            methodBody.removeStatement(ctStatement);
        }

        CtThrow throwMyException = factory.createThrow();
        CtConstructorCall<? extends Throwable> constructorCall = exceptionInstance.clone();
        throwMyException.setThrownExpression(constructorCall);
        methodBody.addStatement(throwMyException);

        bodyComments.add(
                factory.createInlineComment(
                "FIXME: The private API type should never be return in a public API."
                )
        );

        for (CtComment bodyComment : bodyComments) {
            throwMyException.addComment(bodyComment);
        }
    }

    Environment environment = launcher.getEnvironment();
    environment.setCommentEnabled(true);
    environment.setAutoImports(true);
    // the transformation must produce compilable code
    environment.setShouldCompile(true);
    launcher.prettyprint();

    // look in folder spooned/ow2con/publicapi/ the transformed code
}
 
Example #9
Source File: OutputWritter.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public Environment getEnvironment() {
	return this.getFactory().getEnvironment();
}
 
Example #10
Source File: MutationSupporter.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public MutationSupporter(Factory factory, Environment environment) {
	this.factory = factory;
	this.currentSupporter = this;
	this.output = new OutputWritter(factory);
}
 
Example #11
Source File: MutationSupporter.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private static Factory createFactory() {
	Environment env = getEnvironment();
	Factory factory = new FactoryImpl(new DefaultCoreFactory(), env);

	return factory;
}