getModelBuilder().compile(SpoonModelBuilder.InputType.FILES);
/** * 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; }
/** 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(); }
/** * Utility method for testing: creates the model of the given `classesToBuild` from src/test/java and returns the factory * and allows to configure the Launcher first using `config` */ public static Factory build(Consumer<Launcher> config, Class<?>... classesToBuild) throws Exception { final Launcher launcher = new Launcher(); config.accept(launcher); SpoonModelBuilder comp = launcher.createCompiler(); for (Class<?> classToBuild : classesToBuild) { comp.addInputSources(SpoonResourceHelper.resources("./src/test/java/" + classToBuild.getName().replace('.', '/') + ".java")); } comp.build(); return comp.getFactory(); }
@Override public CtModel buildModel() { long tstart = System.currentTimeMillis(); modelBuilder.build(); getEnvironment().debugMessage("model built in " + (System.currentTimeMillis() - tstart)); return modelBuilder.getFactory().getModel(); }
public static TestListener runTest(Configuration configuration, Launcher launcher, String failingTestClass, String failingTestMethod) { final SpoonModelBuilder compiler = launcher.createCompiler(); compiler.setBinaryOutputDirectory(new File(configuration.getBinaryOutputDirectory())); compiler.compile(SpoonModelBuilder.InputType.CTTYPES); try { return EntryPoint.runTests( configuration.getBinaryOutputDirectory() + Util.PATH_SEPARATOR + configuration.getClasspath(), failingTestClass, failingTestMethod ); } catch (TimeoutException e) { throw new RuntimeException(e); } }
public static boolean compile(InputConfiguration configuration, String pathToSources, String dependencies, File binaryOutputDirectory) { Launcher launcher = new Launcher(); if (configuration.isVerbose()) { launcher.getEnvironment().setLevel("INFO"); } launcher.getEnvironment().setNoClasspath(true); launcher.getEnvironment().setCommentEnabled(true); launcher.getEnvironment().setOutputType(OutputType.CLASSES); DSpotUtils.copyPackageFromResources(); String[] sourcesArray = (pathToSources + PATH_SEPARATOR).split(PATH_SEPARATOR); Arrays.stream(sourcesArray).forEach(launcher::addInputResource); String[] dependenciesArray = dependencies.split(PATH_SEPARATOR); launcher.getModelBuilder().setSourceClasspath(dependenciesArray); launcher.buildModel(); SpoonModelBuilder modelBuilder = launcher.getModelBuilder(); modelBuilder.setBinaryOutputDirectory(binaryOutputDirectory); return modelBuilder.compile(SpoonModelBuilder.InputType.CTTYPES); }
public static Factory modelFor(Factory factory, File[] sourceFiles, URL[] classpath) { factory.getEnvironment().setLevel("OFF"); factory.getEnvironment().setCommentEnabled(false); SpoonModelBuilder compiler = launcher().createCompiler(factory); if (classpath != null) { compiler.setSourceClasspath(JavaLibrary.asFilePath(classpath)); } for (int i = 0; i < sourceFiles.length; i++) { File sourceFile = sourceFiles[i]; compiler.addInputSource(sourceFile); } compiler.build(); return factory; }
/** * Compile the original code * * @param properties */ protected void compileProject(ProjectConfiguration properties) { final Launcher launcher = new Launcher(); for (String path_src : properties.getOriginalDirSrc()) { log.debug("Add folder to compile: " + path_src); launcher.addInputResource(path_src); } for (String path_test : properties.getTestDirSrc()) { log.debug("Add folder to compile: " + path_test); launcher.addInputResource(path_test); } String binoutput = properties.getWorkingDirForBytecode() + File.separator + (ProgramVariant.DEFAULT_ORIGINAL_VARIANT); launcher.setBinaryOutputDirectory(binoutput); log.info("Compiling original code from " + launcher.getModelBuilder().getInputSources() + "\n bytecode saved in " + launcher.getModelBuilder().getBinaryOutputDirectory()); launcher.getEnvironment() .setPreserveLineNumbers(ConfigurationProperties.getPropertyBool("preservelinenumbers")); launcher.getEnvironment().setComplianceLevel(ConfigurationProperties.getPropertyInt("javacompliancelevel")); launcher.getEnvironment().setShouldCompile(true); launcher.getEnvironment().setSourceClasspath(properties.getDependenciesString().split(File.pathSeparator)); launcher.buildModel(); launcher.getModelBuilder().generateProcessedSourceFiles(OutputType.COMPILATION_UNITS); launcher.getModelBuilder().compile(InputType.FILES); // launcher.getModelBuilder().generateProcessedSourceFiles(OutputType.CLASSES); }
for (String s : getArguments().getString("input").split("[" + File.pathSeparatorChar + "]")) { try { modelBuilder.addInputSource(SpoonResourceHelper.createResource(new File(s))); } catch (FileNotFoundException e) { throw new SpoonException(e); modelBuilder.compile(InputType.FILES); getEnvironment().setSourceClasspath(new String[]{getEnvironment().getBinaryOutputDirectory()}); for (String s : getArguments().getString("template").split("[" + File.pathSeparatorChar + "]")) { try { modelBuilder.addTemplateSource(SpoonResourceHelper.createResource(new File(s))); } catch (FileNotFoundException e) { environment.report(null, Level.ERROR, "Unable to add template file: " + e.getMessage());
this.getModelBuilder().setSourceClasspath(classpath);
/** checks that the file `outputDirectoryFile` can be parsed with Spoon , given a compliance level and the noclasspath option. */ public static void canBeBuilt(File outputDirectoryFile, int complianceLevel, boolean noClasspath) { final Launcher launcher = new Launcher(); final Factory factory = launcher.getFactory(); factory.getEnvironment().setComplianceLevel(complianceLevel); factory.getEnvironment().setNoClasspath(noClasspath); final SpoonModelBuilder compiler = launcher.createCompiler(factory); compiler.addInputSource(outputDirectoryFile); try { compiler.build(); } catch (Exception e) { final AssertionError error = new AssertionError("Can't compile " + outputDirectoryFile.getName() + " because " + e.getMessage()); error.initCause(e); throw error; } }
Environment env = modelBuilder.getFactory().getEnvironment(); env.reportProgressMessage(getVersionMessage()); env.reportProgressMessage("running Spoon..."); modelBuilder.compile(InputType.CTTYPES);
@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"); }
/** adds a resource to be parsed to build the spoon model */ public void addInputResource(SpoonResource resource) { modelBuilder.addInputSource(resource); }
private static void build(Factory f, String contents) { // Build contents SpoonModelBuilder builder = new JDTSnippetCompiler(f, contents); try { builder.build(); } catch (Exception e) { throw new ModelBuildingException("snippet compilation error while compiling: " + contents, e); } }
addedMethod.add(clone); }); compiler.setBinaryOutputDirectory(new File(configuration.getBinaryOutputDirectory())); compiler.compile(SpoonModelBuilder.InputType.CTTYPES);
/** Utility method for testing: creates the model of `packageName` and the factory from src/test/java and returns the CtType corresponding to `className` */ public static <T extends CtType<?>> T build(String packageName, String className, final Factory f) throws Exception { Launcher launcher = new Launcher() { @Override public Factory createFactory() { return f; } }; 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); }
public static Launcher getSpoonModelOf(String pathToSources, String pathToDependencies) { Launcher launcher = new Launcher(); launcher.getEnvironment().setNoClasspath(true); launcher.getEnvironment().setCommentEnabled(true); launcher.getEnvironment().setOutputType(OutputType.CLASSES); DSpotUtils.copyPackageFromResources(); String[] sourcesArray = (pathToSources + PATH_SEPARATOR + DSpotUtils.getAbsolutePathToDSpotDependencies()).split(PATH_SEPARATOR); Arrays.stream(sourcesArray).forEach(launcher::addInputResource); if (!pathToDependencies.isEmpty()) { String[] dependenciesArray = pathToDependencies.split(PATH_SEPARATOR); launcher.getModelBuilder().setSourceClasspath(dependenciesArray); } launcher.buildModel(); return launcher; }
public CtType getCtType(SpoonResource resource) { Factory factory = createFactory(); factory.getModel().setBuildModelIsFinished(false); SpoonModelBuilder compiler = new JDTBasedSpoonCompiler(factory); compiler.getFactory().getEnvironment().setLevel("OFF"); compiler.addInputSource(resource); compiler.build(); if (factory.Type().getAll().size() == 0) { return null; } // let's first take the first type. CtType type = factory.Type().getAll().get(0); // Now, let's ask to the factory the type (which it will set up the // corresponding // package) return factory.Type().get(type.getQualifiedName()); }