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(); }
/** 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(); }
@Override public void run(String[] args) { this.setArgs(args); if (args.length != 0) { this.run(); // display GUI if (this.jsapActualArgs.getBoolean("gui")) { new SpoonModelTree(getFactory()); } } else { this.printUsage(); } }
public static void process(Factory factory, Collection<Processor<?>> processors) { final JDTBasedSpoonCompiler compiler = (JDTBasedSpoonCompiler) new Launcher().createCompiler(factory); compiler.process(processors); }
/** * 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); }
Launcher spoonLauncher = new Launcher(); spoonLauncher.addProcessor(new TestSelectionProcessor(keptMethods)); spoonLauncher.addProcessor(new RemoveEvosuiteEffectsProcessor()); spoonLauncher.addInputResource(newTestFolder); spoonLauncher.getEnvironment().setSourceClasspath(classPath.split(File.pathSeparator)); spoonLauncher.setSourceOutputDirectory(destSrcTestFolder); spoonLauncher.getEnvironment().setShouldCompile(true); spoonLauncher.setBinaryOutputDirectory(destCpTestFolder); spoonLauncher.getEnvironment().setComplianceLevel(7); spoonLauncher.run(); Launcher lastLauncher = new Launcher(); lastLauncher.addProcessor(new TestSelectionProcessor(keptMethods)); lastLauncher.addProcessor(new RemoveEvosuiteEffectsProcessor()); lastLauncher.addInputResource(newTestFolder); lastLauncher.getEnvironment().setSourceClasspath(classPath.split(File.pathSeparator)); lastLauncher.setSourceOutputDirectory(destSrcTestFolder); lastLauncher.getEnvironment().setShouldCompile(true); lastLauncher.setBinaryOutputDirectory(destCpTestFolder); lastLauncher.run();
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; }
private Launcher getSpoonAPIForProject() { EntryPoint.verbose = this.configuration.isVerbose(); Launcher launcher = new Launcher(); launcher.getEnvironment().setAutoImports(true); launcher.getEnvironment().setNoClasspath(true); launcher.getEnvironment().setCommentEnabled(true); launcher.getEnvironment().setSourceClasspath(this.configuration.getClasspath().split(Util.PATH_SEPARATOR)); if (this.configuration.getPathToSourceFolder() != null) { for (String path : this.configuration.getPathToSourceFolder()) { launcher.addInputResource(path); } } if (this.configuration.getPathToTestFolder() == null) { throw new RuntimeException("Test folder must be set for AssertFixer to be executed."); } for (String path : this.configuration.getPathToTestFolder()) { launcher.addInputResource(path); } launcher.getEnvironment().setShouldCompile(true); launcher.run(); return launcher; }
spoon.Launcher spoon = new spoon.Launcher(); for (File s : sourceFolders) { spoon.addInputResource(s.getAbsolutePath()); spoon.getModelBuilder().setSourceClasspath(classpath(dependencies).split(File.pathSeparatorChar + "")); spoon.buildModel();
/** 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; } }
protected void processArguments() { jsapActualArgs = getArguments(); if (getArguments().getString("generate-files") != null) { setOutputFilter(getArguments().getString("generate-files").split(":")); modelBuilder = createCompiler(); if (getArguments().getString("input") != null) { for (String s : getArguments().getString("input").split("[" + File.pathSeparatorChar + "]")) { try { modelBuilder.addInputSource(SpoonResourceHelper.createResource(new File(s))); getEnvironment().setSourceClasspath(new String[]{getEnvironment().getBinaryOutputDirectory()}); if (getArguments().getFile("output") != null) { setSourceOutputDirectory(getArguments().getFile("output")); if (getArguments().getString("template") != null) { for (String s : getArguments().getString("template").split("[" + File.pathSeparatorChar + "]")) { try { modelBuilder.addTemplateSource(SpoonResourceHelper.createResource(new File(s))); if (getArguments().getString("processors") != null) { for (String processorName : getArguments().getString("processors").split(File.pathSeparator)) { addProcessor(processorName);
public PrettyPrinter createPrettyPrinter() { return getEnvironment().createPrettyPrinter(); }
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); } }
public JavaOutputProcessor createOutputWriter() { /* * create without printer, because Launcher is not initialized yet * so we cannot setup correct printer validators at this time * The printer is created lazily using Environment */ JavaOutputProcessor outputProcessor = new JavaOutputProcessor(); outputProcessor.setFactory(this.getFactory()); return outputProcessor; }
public void run() { Environment env = modelBuilder.getFactory().getEnvironment(); env.reportProgressMessage(getVersionMessage()); env.reportProgressMessage("running Spoon..."); buildModel(); process(); prettyprint();
@Override public void process() { long tstart = System.currentTimeMillis(); modelBuilder.instantiateAndProcess(getProcessorTypes()); modelBuilder.process(getProcessors()); getEnvironment().debugMessage("model processed in " + (System.currentTimeMillis() - tstart) + " ms"); }
String fullQualifiedName, String testCaseName) { final Factory factory = spoon.getFactory(); final CtClass<?> testClass = factory.Class().get(fullQualifiedName); final CtElement addedElement = addSaveStatementInTearDownAfterClass(testClass); final SpoonModelBuilder compiler = spoon.createCompiler();
@Override public void setSourceOutputDirectory(File outputDirectory) { getEnvironment().setSourceOutputDirectory(outputDirectory); getEnvironment().setDefaultFileGenerator(createOutputWriter()); }
@Override public SpoonModelBuilder createCompiler() { return createCompiler(factory); }