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); } }
@Override public CtModel buildModel() { long tstart = System.currentTimeMillis(); modelBuilder.build(); getEnvironment().debugMessage("model built in " + (System.currentTimeMillis() - tstart)); return modelBuilder.getFactory().getModel(); }
/** * 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(); }
/** 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; } }
/** 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); }
/** 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); }
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()); }
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()); }
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; }