public PrettyPrinter createPrettyPrinter() { return getEnvironment().createPrettyPrinter(); }
/** Utility method for testing: creates the model of the given `classesToBuild` from src/test/java and returns the factory */ public static Factory build(Class<?>... classesToBuild) throws Exception { return build(launcher -> { launcher.getEnvironment().setNoClasspath(false); launcher.getEnvironment().setCommentEnabled(false); }, classesToBuild); }
@Override public void setSourceOutputDirectory(File outputDirectory) { getEnvironment().setSourceOutputDirectory(outputDirectory); getEnvironment().setDefaultFileGenerator(createOutputWriter()); }
/** Utility method for testing: creates the noclasspath model of the given `classesToBuild` from src/test/java and returns the factory */ public static Factory buildNoClasspath(Class<?>... classesToBuild) throws Exception { return build(launcher -> launcher.getEnvironment().setNoClasspath(true), classesToBuild); }
@Override public CtModel buildModel() { long tstart = System.currentTimeMillis(); modelBuilder.build(); getEnvironment().debugMessage("model built in " + (System.currentTimeMillis() - tstart)); return modelBuilder.getFactory().getModel(); }
private static Factory createFactory(File spoonJavaSourcesDirectory) { final Launcher launcher = new Launcher(); launcher.getEnvironment().setNoClasspath(true); launcher.getEnvironment().setCommentEnabled(true); // // Spoon model interfaces Arrays.asList("spoon/reflect/code", "spoon/reflect/declaration", "spoon/reflect/reference", "spoon/support/reflect/declaration", "spoon/support/reflect/code", "spoon/support/reflect/reference").forEach(path -> launcher.addInputResource(new FileSystemFolder(new File(spoonJavaSourcesDirectory, path)))); launcher.buildModel(); return launcher.getFactory(); }
@Override public void prettyprint() { long tstart = System.currentTimeMillis(); try { modelBuilder.generateProcessedSourceFiles(getEnvironment().getOutputType(), typeFilter); } catch (Exception e) { throw new SpoonException(e); } if (!getEnvironment().getOutputType().equals(OutputType.NO_OUTPUT) && getEnvironment().isCopyResources()) { for (File dirInputSource : modelBuilder.getInputSources()) { if (dirInputSource.isDirectory()) { final Collection<?> resources = FileUtils.listFiles(dirInputSource, RESOURCES_FILE_FILTER, ALL_DIR_FILTER); for (Object resource : resources) { final String resourceParentPath = ((File) resource).getParent(); final String packageDir = resourceParentPath.substring(dirInputSource.getPath().length()); final String targetDirectory = getEnvironment().getDefaultFileGenerator().getOutputDirectory() + packageDir; try { FileUtils.copyFileToDirectory((File) resource, new File(targetDirectory)); } catch (IOException e) { throw new SpoonException(e); } } } } } getEnvironment().debugMessage("pretty-printed in " + (System.currentTimeMillis() - tstart) + " ms"); }
@Override public void process() { long tstart = System.currentTimeMillis(); modelBuilder.instantiateAndProcess(getProcessorTypes()); modelBuilder.process(getProcessors()); getEnvironment().debugMessage("model processed in " + (System.currentTimeMillis() - tstart) + " ms"); }
/** returns the AST of an inline class */ public static CtClass<?> parseClass(String code) { Launcher launcher = new Launcher(); launcher.addInputResource(new VirtualFile(code)); launcher.getEnvironment().setNoClasspath(true); launcher.getEnvironment().setAutoImports(true); Collection<CtType<?>> allTypes = launcher.buildModel().getAllTypes(); if (allTypes.size() != 1) { throw new SpoonException("parseClass only considers one class. Please consider using a Launcher object for more advanced usage."); } try { return (CtClass<?>) allTypes.stream().findFirst().get(); } catch (ClassCastException e) { throw new SpoonException("parseClass only considers classes (and not interfaces and enums). Please consider using a Launcher object for more advanced usage."); } } }
/** Builds the Spoon mode of the `filesToBuild` given as parameter */ public static Factory build(File... filesToBuild) { final Launcher launcher = new Launcher(); launcher.getEnvironment().setNoClasspath(true); SpoonModelBuilder comp = launcher.createCompiler(); for (File fileToBuild : filesToBuild) { try { comp.addInputSource(SpoonResourceHelper.createResource(fileToBuild)); } catch (FileNotFoundException e) { throw new RuntimeException("File not found", e); } } comp.build(); return comp.getFactory(); }
/** * Creates a new Spoon Java compiler in order to process and compile Java * source code. * * @param factory * the factory this compiler works on */ public SpoonModelBuilder createCompiler(Factory factory) { SpoonModelBuilder comp = new JDTBasedSpoonCompiler(factory); Environment env = getEnvironment(); // building comp.setBinaryOutputDirectory(jsapActualArgs.getFile("destination")); // backward compatibility // we don't have to set the source classpath if (jsapActualArgs.contains("source-classpath")) { comp.setSourceClasspath(jsapActualArgs.getString("source-classpath").split(System.getProperty("path.separator"))); } env.debugMessage("destination: " + comp.getBinaryOutputDirectory()); env.debugMessage("source classpath: " + Arrays.toString(comp.getSourceClasspath())); env.debugMessage("template classpath: " + Arrays.toString(comp.getTemplateClasspath())); return comp; }
/** Utility method for testing: creates the model of `packageName` from src/test/java and returns the CtType corresponding to `className` */ public static <T extends CtType<?>> T build(String packageName, String className) throws Exception { Launcher launcher = new Launcher(); launcher.getEnvironment().setCommentEnabled(false); // we don't want to parse the comments for equals SpoonModelBuilder comp = launcher.createCompiler(); comp.addInputSources(SpoonResourceHelper.resources("./src/test/java/" + packageName.replace('.', '/') + "/" + className + ".java")); comp.build(); return comp.getFactory().Package().get(packageName).getType(className); }
getEnvironment().setSourceClasspath(new String[]{getEnvironment().getBinaryOutputDirectory()});
public PrettyPrinter createPrettyPrinter() { return new DefaultJavaPrettyPrinter(getEnvironment()); }
@Override public void setSourceOutputDirectory(File outputDirectory) { modelBuilder.setSourceOutputDirectory(outputDirectory); getEnvironment().setDefaultFileGenerator(createOutputWriter(outputDirectory, getEnvironment())); }
@Override public void buildModel() { long tstart = System.currentTimeMillis(); modelBuilder.build(); getEnvironment().debugMessage("model built in " + (System.currentTimeMillis() - tstart)); }
@Override public void process() { long tstart = System.currentTimeMillis(); modelBuilder.process(getProcessorTypes()); modelBuilder.process(getProcessors()); getEnvironment().debugMessage("model processed in " + (System.currentTimeMillis() - tstart) + " ms"); }
private static CtClass<?> getNewModelCtClass(String pathToSrcFolder, String fullQualifiedName) { Launcher launcher = new Launcher(); launcher.getEnvironment().setNoClasspath(true); launcher.getEnvironment().setCommentEnabled(true); launcher.addInputResource(pathToSrcFolder); launcher.buildModel(); return launcher.getFactory().Class().get(fullQualifiedName); }
public static void main(String[] args) throws Exception { Launcher main = new Launcher(); JDTBasedSpoonCompiler comp = new JDTBasedSpoonCompiler(main.createFactory()); comp.createBatchCompiler().printUsage(); SpoonFile file = new FileSystemFile(new File("./src/main/java/spoon/support/compiler/JDTCompiler.java")); comp.addInputSource(file); try { comp.build(); final Set<CtType<?>> types = comp.getFactory().Package().get("spoon.support.compiler").getTypes(); for (CtType<?> type : types) { main.getEnvironment().debugMessage(type.toString()); } } catch (Exception e) { throw new RuntimeException(e); } }
private static CtClass<?> getExistingClass(CtType<?> type, String pathname) { Launcher launcher = new Launcher(); launcher.getEnvironment().setNoClasspath(true); launcher.addInputResource(pathname); launcher.buildModel(); return launcher.getFactory().Class().get(type.getQualifiedName()); }