/** * A default program entry point (instantiates a launcher with the given * arguments and calls {@link #run()}). */ public static void main(String[] args) { new Launcher().run(args); }
public static void process(Factory factory, Collection<Processor<?>> processors) { final JDTBasedSpoonCompiler compiler = (JDTBasedSpoonCompiler) new Launcher().createCompiler(factory); compiler.process(processors); }
/** * 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(); }
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(); }
/** 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; } }
/** 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(); }
/** 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); }
private static Launcher launcher() { if (launcher == null) { launcher = new Launcher(); } return launcher; }
/** * A default program entry point (instantiates a launcher with the given * arguments and calls {@link #run()}). */ public static void main(String[] args) throws Exception { new Launcher().run(args); }
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; }
/** * this method analyze all java class in generatedTestDir and return java paths composed of packages.class * @param generatedTestFolder * @param classPath * @return List<String> list of class ({package.package.classname}) */ public static List<String> getNewTestClasses(String generatedTestFolder, String classPath){ List<String> testsClasses = new ArrayList<String>(); logger.debug("--------------------------------------------------"); logger.debug(" ##### Search tests files path ##### "); Launcher spoon = new Launcher(); spoon.getEnvironment().setAutoImports(true); spoon.addInputResource(generatedTestFolder); spoon.getEnvironment().setSourceClasspath(classPath.split(File.pathSeparator)); spoon.buildModel(); //getannotatedMethod.. could be better for(CtType<?> clazz : spoon.getFactory().Class().getAll()){ String className = clazz.getPackage().toString()+"."+clazz.getSimpleName(); logger.debug("[FOUND] "+className); testsClasses.add(className); } return testsClasses; }
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); } }
String[] classPathStrings = getClassPathStrings(); contextClasses.forEach(cxtC -> { Launcher spoon = new Launcher(); spoon.getEnvironment().setComplianceLevel(8); spoon.getEnvironment().setNoClasspath(true);
/** * this method analyze all java class in generatedTestDir and return a list of all Junit method * @param generatedTestFolder * @param classPath * @return List<CtMethod> list of methods */ public static List<CtMethod<?>> getNewTestsMethods(String generatedTestFolder, String classPath){ List<CtMethod<?>> testsMethods = new ArrayList<CtMethod<?>>(); logger.debug("--------------------------------------------------"); logger.debug(" ##### Search tests methods ##### "); Launcher spoon = new Launcher(); spoon.getEnvironment().setAutoImports(true); spoon.addInputResource(generatedTestFolder); spoon.getEnvironment().setSourceClasspath(classPath.split(File.pathSeparator)); spoon.buildModel(); //getannotatedMethod.. could be better for(CtType<?> clazz : spoon.getFactory().Class().getAll()){ methodLoop: for(CtMethod<?> method : clazz.getAllMethods()){ for(CtAnnotation<? extends Annotation> annotation : method.getAnnotations()){ if (annotation.getType().equals(method.getFactory().Annotation().createReference(org.junit.Test.class))) { logger.debug("[FOUND] "+method.getSignature()); testsMethods.add(method); continue methodLoop; } } } } return testsMethods; }
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); }
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()); }
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); }
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; }
/** * 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); }