/** * Convenience method for the common case, that only one file is generated. */ public String getSingleGeneratedCode() { doGenerate(); Set<GeneratedFile> generatedFiles = access.getGeneratedFiles(); if (generatedFiles.size() == 1) return generatedFiles.iterator().next().getContents().toString(); else if (generatedFiles.isEmpty()) return "NO FILE WAS GENERATED"; String separator = System.getProperty("line.separator"); if (separator == null) separator = "\n"; List<GeneratedFile> files = newArrayList(generatedFiles); Collections.sort(files, new Comparator<GeneratedFile>() { @Override public int compare(GeneratedFile o1, GeneratedFile o2) { return o1.getPath().toString().compareTo(o2.getPath().toString()); } }); StringBuilder result = new StringBuilder("MULTIPLE FILES WERE GENERATED"+separator+separator); int i = 1; for (GeneratedFile file: files) { result.append("File "+i+" : "+file.getPath().toString()+separator+separator); result.append(file.getContents()).append(separator); i++; } return result.toString(); }
protected void doValidation() { if (allErrorsAndWarnings == null) { doLinking(); allErrorsAndWarnings = newArrayList(); // validation for (Resource resource : sources) { if (resource instanceof XtextResource) { XtextResource xtextResource = (XtextResource) resource; List<Issue> issues = xtextResource.getResourceServiceProvider().getResourceValidator().validate(xtextResource, checkMode, CancelIndicator.NullImpl); for (Issue issue : issues) { allErrorsAndWarnings.add(issue); } } } } }
/** * Convenience method for single generated Java classes */ public Class<?> getCompiledClass() { return IterableExtensions.head(getCompiledClasses().values()); }
result.setJavaCompiler(javaCompiler); result.setCheckMode(getCheckMode()); result.setResources(resourcesToCheck); result.setResourceSet(resourceSet); result.setOutputConfigurations(getOutputConfigurations()); result.doGenerate(); acceptor.accept(result); } catch (Exception e) {
protected void doGenerate() { if (access == null) { doValidation(); access = fileSystemAccessProvider.get(); access.setOutputConfigurations(outputConfigurations); for (Resource resource : sources) { if (resource instanceof XtextResource) { access.setProjectName(PROJECT_NAME); XtextResource xtextResource = (XtextResource) resource; IResourceServiceProvider resourceServiceProvider = xtextResource.getResourceServiceProvider(); GeneratorDelegate generator = resourceServiceProvider.get(GeneratorDelegate.class); if (generator != null) { GeneratorContext context = new GeneratorContext(); context.setCancelIndicator(CancelIndicator.NullImpl); generator.generate(xtextResource, access, context); } } } generatedCode = newHashMap(); for (final GeneratedFile e : access.getGeneratedFiles()) { if (e.getJavaClassName() != null) { generatedCode.put(e.getJavaClassName(), e.getContents().toString()); } } } }
protected void doCompile() { if (compiledClasses == null || classLoader==null) { doGenerate(); try { Map<String, Class<?>> compilationResult = javaCompiler.compileToClasses(getGeneratedCode()); Iterator<Class<?>> values = compilationResult.values().iterator(); this.classLoader = values.hasNext() ? values.next().getClassLoader() : null; this.compiledClasses = compilationResult; } catch (IllegalArgumentException e) { throw new AssertionError(e); } } } }
/** * convenience method. Same as getGeneratedCode().get(typeName) */ public String getGeneratedCode(String typeName) { return getGeneratedCode().get(typeName); }
/** * Ensures compilation has happened and returns any generated and compiled Java classes. * * @return the compiled Java classes */ public Map<String,Class<?>> getCompiledClasses() { doCompile(); return compiledClasses; }
@Override public void accept(Result r) { Assert.assertEquals(expected.toString(), r.getSingleGeneratedCode()); called[0] = true; } });
/** * Ensures generation happened and returns a map of the generated Java source files. * * @return a map of the generated Java source files, where the key is the qualified class name and the value the generated Java code. */ public Map<String,String> getGeneratedCode() { doGenerate(); return generatedCode; }
/** * Ensures validation has happened and returns any errors and warnings * * @return errors and warnings contained in the currently processed sources */ public List<Issue> getErrorsAndWarnings() { doValidation(); return allErrorsAndWarnings; }
/** * @return all generated resources. the key is the file path and the value denotes the generated text. */ public Map<String, CharSequence> getAllGeneratedResources() { doGenerate(); Map<String,CharSequence> result = newHashMap(); for (GeneratedFile f: access.getGeneratedFiles()) { result.put(f.getPath(), f.getContents()); } return result; }
/** * Convenience method for single generated Java classes */ public Class<?> getCompiledClass(String className) { return getCompiledClasses().get(className); }
/** * Ensures compilation has happened and returns the class loader including compiled classes. * * @return the class loader after the compilation happend */ public ClassLoader getClassLoader() { doCompile(); return classLoader; }
protected void doLinking() { doIndex(); for (Resource resource : sources) { EcoreUtil2.resolveLazyCrossReferences(resource, CancelIndicator.NullImpl); } }