private CompilationUnit getCompilationUnit(SourceFile sourceFile) { CompilationUnit compilationUnit; if (Files.exists(sourceFile.getOutputPath())) { try { compilationUnit = JavaParser.parse(sourceFile.getOutputPath().toFile()); } catch (Throwable t) { throw new RuntimeException(t); } } else { compilationUnit = new CompilationUnit(); compilationUnit.setComment( new LineComment(" Generated by GraphWalker (http://www.graphwalker.org)")); if (!"".equals(sourceFile.getPackageName())) { compilationUnit.setPackage(createPackageDeclaration(sourceFile)); } compilationUnit.setImports( Arrays.asList( new ImportDeclaration( new NameExpr("org.graphwalker.java.annotation.Model"), false, false), new ImportDeclaration( new NameExpr("org.graphwalker.java.annotation.Vertex"), false, false), new ImportDeclaration( new NameExpr("org.graphwalker.java.annotation.Edge"), false, false))); ASTHelper.addTypeDeclaration(compilationUnit, getInterfaceName(sourceFile)); } return compilationUnit; }
protected void testExternChanges(String extern, String input, String expectedExtern) { Compiler compiler = createCompiler(); CompilerOptions options = getOptions(); compiler.init( ImmutableList.of(SourceFile.fromCode("extern", extern)), ImmutableList.of(SourceFile.fromCode("input", input)), options); compiler.parseInputs(); assertFalse(compiler.hasErrors()); Node externsAndJs = compiler.getRoot(); Node root = externsAndJs.getLastChild(); Node externs = externsAndJs.getFirstChild(); Node expected = compiler.parseTestCode(expectedExtern); assertFalse(compiler.hasErrors()); (getProcessor(compiler)).process(externs, root); String externsCode = compiler.toSource(externs); String expectedCode = compiler.toSource(expected); assertEquals(expectedCode, externsCode); }
protected void compile( SourceFile[] units, SourceFile[] additionalUnits, boolean compilingFirstGroup) { if (compilingFirstGroup && additionalUnits != null) { // add any source file from additionalUnits to units if it defines secondary types // otherwise its possible during testing with MAX_AT_ONCE == 1 that a secondary type // can cause an infinite loop as it alternates between not found and defined, see bug 146324 ArrayList extras = null; for (int i = 0, l = additionalUnits.length; i < l; i++) { SourceFile unit = additionalUnits[i]; if (unit != null && this.newState.getDefinedTypeNamesFor(unit.typeLocator()) != null) { if (JavaBuilder.DEBUG) System.out.println( "About to compile file with secondary types " + unit.typeLocator()); // $NON-NLS-1$ if (extras == null) extras = new ArrayList(3); extras.add(unit); } } if (extras != null) { int oldLength = units.length; int toAdd = extras.size(); System.arraycopy(units, 0, units = new SourceFile[oldLength + toAdd], 0, oldLength); for (int i = 0; i < toAdd; i++) units[oldLength++] = (SourceFile) extras.get(i); } } super.compile(units, additionalUnits, compilingFirstGroup); }
public void testPackagePrivateAccessForProperties3() { testSame( ImmutableList.of( SourceFile.fromCode( "foo/bar.js", "/** @constructor */ function Foo() {}" + "/** @package */ Foo.prototype.bar = function() {}; (new Foo).bar();"), SourceFile.fromCode("foo/baz.js", "Foo.prototype.baz = function() { this.bar(); };"))); }
public void testSortInputs() throws Exception { SourceFile a = SourceFile.fromCode("a.js", "require('b');require('c')"); SourceFile b = SourceFile.fromCode("b.js", "require('d')"); SourceFile c = SourceFile.fromCode("c.js", "require('d')"); SourceFile d = SourceFile.fromCode("d.js", "1;"); assertSortedInputs(ImmutableList.of(d, b, c, a), ImmutableList.of(a, b, c, d)); assertSortedInputs(ImmutableList.of(d, b, c, a), ImmutableList.of(d, b, c, a)); assertSortedInputs(ImmutableList.of(d, c, b, a), ImmutableList.of(d, c, b, a)); assertSortedInputs(ImmutableList.of(d, b, c, a), ImmutableList.of(d, a, b, c)); }
private SourceFile parseJavaFile(final JavaFile javaFile) { SourceFile sourceFile = new SourceFile(); ArrayList<Clazz> clazzes = new ArrayList<Clazz>(); for (JavaClazz javaClazz : javaFile.getClasses()) { clazzes.add(parseJavaClazz(javaClazz)); } sourceFile.setClasses(clazzes); sourceFile.setPath(javaFile.getFilePath()); sourceFile.setSourcePackage(javaFile.getPackageName()); return sourceFile; }
@Override public Invokable invoke(Invokation invokation, Object... parameters) { String packageName = parameters[0].toString(); String simpleName = parameters[1].toString(); Invokable body = (Invokable) parameters[2]; ResourceKey key = new ResourceKey(packageName, simpleName); SourceFile javaFile = getFiles().sourceFiles.get(key); body.invoke(new Invokation(javaFile.consumer)); javaFile.complete(); return null; }
/** * Verifies that the compiler pass's JS output matches the expected output and (optionally) that * an expected warning is issued. Or, if an error is expected, this method just verifies that the * error is encountered. * * @param externs Externs input * @param js Input * @param expected Expected output, or null if an error is expected * @param error Expected error, or null if no error is expected * @param warning Expected warning, or null if no warning is expected * @param description The description of the expected warning, or null if no warning is expected * or if the warning's description should not be examined */ public void test( String externs, String js, String expected, DiagnosticType error, DiagnosticType warning, String description) { SourceFile externsFile = SourceFile.fromCode("externs", externs); externsFile.setIsExtern(true); List<SourceFile> externsInputs = ImmutableList.of(externsFile); test(externsInputs, js, expected, error, warning, description); }
public void testNoPackagePrivateAccessForProperties3() { test( ImmutableList.of( SourceFile.fromCode( "foo/bar.js", "/** @constructor */ function Foo() {} " + "/** @package */ Foo.prototype.bar = function() {};"), SourceFile.fromCode( "baz/quux.js", "/** @constructor */ function OtherFoo() { (new Foo).bar(); }")), null, BAD_PACKAGE_PROPERTY_ACCESS); }
protected void processDir( File srcRoot, File destRoot, List<String> srcIncludes, List<String> srcExcludes, boolean destAsSource) throws Exception { if ((srcRoot == null) || (!srcRoot.exists())) { return; } if (destRoot == null) { throw new MojoFailureException("destination directory for " + srcRoot + " is null"); } DirectoryScanner scanner = new DirectoryScanner(); scanner.setBasedir(srcRoot); if ((srcIncludes != null) && !srcIncludes.isEmpty()) { scanner.setIncludes(srcIncludes.toArray(EMPTY_STRING_ARRAY)); } if ((includes != null) && !includes.isEmpty()) { scanner.setIncludes(includes.toArray(EMPTY_STRING_ARRAY)); } else { scanner.setIncludes(getDefaultIncludes()); } if ((srcExcludes != null) && !srcExcludes.isEmpty()) { scanner.setExcludes(srcExcludes.toArray(EMPTY_STRING_ARRAY)); } if ((excludes != null) && !excludes.isEmpty()) { scanner.setExcludes(excludes.toArray(EMPTY_STRING_ARRAY)); } scanner.addDefaultExcludes(); scanner.scan(); for (String name : scanner.getIncludedFiles()) { SourceFile src = new SourceFile(srcRoot, destRoot, name, destAsSource); jsErrorReporter_.setDefaultFileName( "..." + src.toFile() .getAbsolutePath() .substring(project.getBasedir().getAbsolutePath().length())); processFile(src); } }
/** Get a line of a source file by its location. */ public String getSourceLine(File file, int line, String sep) { SourceFile source = sourceFiles.get(file); if (source == null) { try { source = new SourceFile(file); sourceFiles.put(file, source); } catch (IOException e) { return "Cannot open source file: " + file; } } return line + sep + source.getLine(line); }
/** * @param srcFolder * @throws CoreException */ protected void createTestSourceFiles(IFolder srcFolder) throws CoreException { SourceFile sourceFile = createSourceFile(); IPath path = new Path(sourceFile.getFileName()); for (int i = path.segmentCount() - 1; i > 0; i--) { IFolder newFolder = srcFolder.getFolder(path.removeLastSegments(i)); newFolder.create(true, true, null); } IFile file = srcFolder.getFile(path); file.create(new StringBufferInputStream(sourceFile.getContent()), true, null); }
private static void write(ContextFactory factory, SourceFile file) { try { RuntimeModel model = factory.create(file.getInputPath()).getModel(); String source = generator.generate(file, model); Files.createDirectories(file.getOutputPath().getParent()); Files.write( file.getOutputPath(), source.getBytes(Charset.forName("UTF-8")), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); } catch (Throwable t) { throw new CodeGeneratorException(t); } }
private ClassOrInterfaceDeclaration getInterfaceName(SourceFile sourceFile) { ClassOrInterfaceDeclaration classOrInterfaceDeclaration = new ClassOrInterfaceDeclaration(ModifierSet.PUBLIC, false, sourceFile.getFileName()); List<MemberValuePair> memberValuePairs = new ArrayList<>(); memberValuePairs.add( new MemberValuePair( "file", new StringLiteralExpr( sourceFile.getRelativePath().toString().replace(File.separator, "/")))); List<AnnotationExpr> annotations = new ArrayList<>(); annotations.add(new NormalAnnotationExpr(ASTHelper.createNameExpr("Model"), memberValuePairs)); classOrInterfaceDeclaration.setAnnotations(annotations); classOrInterfaceDeclaration.setInterface(true); return classOrInterfaceDeclaration; }
public void testNoPackagePrivateAccessForProperties5() { test( ImmutableList.of( SourceFile.fromCode( "foo/bar.js", "/** @constructor */ function Foo() {} " + "/** @package */ Foo.prototype.bar = function() {};"), SourceFile.fromCode( "baz/quux.js", "/** @constructor \n * @extends {Foo} */ " + "function SubFoo() {};" + "SubFoo.prototype.baz = function() { this.bar(); }")), null, BAD_PACKAGE_PROPERTY_ACCESS); }
/** * Sets the string in the IDE * * @param value The new file contained in a string */ protected void setStringInIDE(String value) { if (sourceFile == null) { return; } sourceFile.setText(value); sourceFile = null; }
/** * @return a mutable list * @throws IOException */ public static List<SourceFile> getDefaultExterns() throws IOException { InputStream input = CommandLineRunner.class.getResourceAsStream("/externs.zip"); if (input == null) { // In some environments, the externs.zip is relative to this class. input = CommandLineRunner.class.getResourceAsStream("externs.zip"); } Preconditions.checkNotNull(input); ZipInputStream zip = new ZipInputStream(input); Map<String, SourceFile> externsMap = Maps.newHashMap(); for (ZipEntry entry = null; (entry = zip.getNextEntry()) != null; ) { BufferedInputStream entryStream = new BufferedInputStream(ByteStreams.limit(zip, entry.getSize())); externsMap.put( entry.getName(), SourceFile.fromInputStream( // Give the files an odd prefix, so that they do not conflict // with the user's files. "externs.zip//" + entry.getName(), entryStream)); } Preconditions.checkState( externsMap.keySet().equals(Sets.newHashSet(DEFAULT_EXTERNS_NAMES)), "Externs zip must match our hard-coded list of externs."); // Order matters, so the resources must be added to the result list // in the expected order. List<SourceFile> externs = Lists.newArrayList(); for (String key : DEFAULT_EXTERNS_NAMES) { externs.add(externsMap.get(key)); } return externs; }
/** Parses expected JS inputs and returns the root of the parse tree. */ protected Node parseExpectedJs(String[] expected) { List<SourceFile> inputs = Lists.newArrayList(); for (int i = 0; i < expected.length; i++) { inputs.add(SourceFile.fromCode("expected" + i, expected[i])); } return parseExpectedJs(inputs); }
private void splitFiles(String[] input) { Compiler compiler = new Compiler(); List<SourceFile> files = Lists.newArrayList(); for (int i = 0; i < input.length; i++) { files.add(SourceFile.fromCode("file" + i, input[i])); } compiler.init(ImmutableList.<SourceFile>of(), files, new CompilerOptions()); compiler.parse(); Node original = compiler.getRoot(); Node root = original.cloneTree(); AstParallelizer parallelizer = AstParallelizer.createNewFileLevelAstParallelizer(root); List<Node> forest = parallelizer.split(); assertEquals(input.length, forest.size()); int i = 0; for (Node n : forest) { Node tree = compiler.parseTestCode(input[i++]); assertEquals(compiler.toSource(tree), compiler.toSource(n)); } parallelizer.join(); assertTrue(original.isEquivalentTo(root)); }
public void testNoPackagePrivateAccessForProperties6() { // Overriding a private property with a non-package-private property // in a different file causes problems. test( ImmutableList.of( SourceFile.fromCode( "foo/bar.js", "/** @constructor */ function Foo() {} " + "/** @package */ Foo.prototype.bar = function() {};"), SourceFile.fromCode( "baz/quux.js", "/** @constructor \n * @extends {Foo} */ " + "function SubFoo() {};" + "SubFoo.prototype.bar = function() {};")), null, BAD_PACKAGE_PROPERTY_ACCESS); }
public void testModuleName() { setFilename("foo/bar"); testModules( "var name = require('other');", "goog.provide('module$foo$bar'); var module$foo$bar = {};" + "goog.require('module$other');" + "var name$$module$foo$bar = module$other;"); ProcessEs6ModulesTest.testModules( this, ImmutableList.of( SourceFile.fromCode("foo/name.js", ""), SourceFile.fromCode("foo/bar.js", "var name = require('./name');")), "goog.provide('module$foo$bar');" + "var module$foo$bar = {};" + "goog.require('module$foo$name');" + "var name$$module$foo$bar = module$foo$name;"); }
/** * Generates a list of modules from a list of inputs. Does not generate any dependencies between * the modules. */ static JSModule[] createModules(String... inputs) { JSModule[] modules = new JSModule[inputs.length]; for (int i = 0; i < inputs.length; i++) { JSModule module = modules[i] = new JSModule("m" + i); module.add(SourceFile.fromCode("i" + i, inputs[i])); } return modules; }
public void testNoPackagePrivateAccessForProperties7() { // It's OK to override a package-private property with a // non-package-private property in the same file, but you'll get // yelled at when you try to use it. test( ImmutableList.of( SourceFile.fromCode( "foo/bar.js", "/** @constructor */ function Foo() {} " + "/** @package */ Foo.prototype.bar = function() {};" + "/** @constructor \n * @extends {Foo} */ " + "function SubFoo() {};" + "SubFoo.prototype.bar = function() {};"), SourceFile.fromCode( "baz/quux.js", "SubFoo.prototype.baz = function() { this.bar(); }")), null, BAD_PACKAGE_PROPERTY_ACCESS); }
/** * Verifies that the compiler pass's JS output is the same as its input and (optionally) that an * expected warning and description is issued. * * @param externs Externs input * @param js Input and output * @param warning Expected warning, or null if no warning is expected * @param description The description of the expected warning, or null if no warning is expected * or if the warning's description should not be examined */ public void testSame( String externs, String js, DiagnosticType type, String description, boolean error) { List<SourceFile> externsInputs = ImmutableList.of(SourceFile.fromCode("externs", externs)); if (error) { test(externsInputs, js, js, type, null, description); } else { test(externsInputs, js, js, null, type, description); } }
public void testFileNotOnOnlyApplyToRegexpIsNotChecked() { configuration = "requirement: {\n" + " type: BANNED_NAME\n" + " value: 'eval'\n" + " error_message: 'eval is not allowed'\n" + " only_apply_to_regexp: 'test.js$'\n " + "}"; testSame(ImmutableList.of(SourceFile.fromCode("bar.js", "eval()"))); }
protected void deleteGeneratedFiles(IFile[] deletedGeneratedFiles) { // delete generated files and recompile any affected source files try { for (int j = deletedGeneratedFiles.length; --j >= 0; ) { IFile deletedFile = deletedGeneratedFiles[j]; if (deletedFile.exists()) continue; // only delete .class files for source files that were actually deleted SourceFile sourceFile = findSourceFile(deletedFile, false); String typeLocator = sourceFile.typeLocator(); int mdSegmentCount = sourceFile.sourceLocation.sourceFolder.getFullPath().segmentCount(); IPath typePath = sourceFile .resource .getFullPath() .removeFirstSegments(mdSegmentCount) .removeFileExtension(); addDependentsOf(typePath, true); // add dependents of the source file since its now deleted this.previousSourceFiles = null; // existing source files did not see it as deleted since they were compiled before // it was char[][] definedTypeNames = this.newState.getDefinedTypeNamesFor(typeLocator); if (definedTypeNames == null) { // defined a single type matching typePath removeClassFile(typePath, sourceFile.sourceLocation.binaryFolder); } else { if (definedTypeNames.length > 0) { // skip it if it failed to successfully define a type IPath packagePath = typePath.removeLastSegments(1); for (int d = 0, l = definedTypeNames.length; d < l; d++) removeClassFile( packagePath.append(new String(definedTypeNames[d])), sourceFile.sourceLocation.binaryFolder); } } this.newState.removeLocator(typeLocator); } } catch (CoreException e) { // must continue with compile loop so just log the CoreException Util.log( e, "JavaBuilder logging CompilationParticipant's CoreException to help debugging"); //$NON-NLS-1$ } }
protected void testExternChanges(String extern, String input, String expectedExtern) { Compiler compiler = createCompiler(); CompilerOptions options = getOptions(); compiler.init( ImmutableList.of(SourceFile.fromCode("extern", extern)), ImmutableList.of(SourceFile.fromCode("input", input)), options); compiler.parseInputs(); assertFalse(compiler.hasErrors()); Node externsAndJs = compiler.getRoot(); Node root = externsAndJs.getLastChild(); Node externs = externsAndJs.getFirstChild(); Node expected = compiler.parseTestCode(expectedExtern); assertFalse(compiler.hasErrors()); (getProcessor(compiler)).process(externs, root); if (compareAsTree) { // Expected output parsed without implied block. Preconditions.checkState(externs.isBlock()); Preconditions.checkState(compareJsDoc); Preconditions.checkState( externs.hasOneChild(), "Compare as tree only works when output has a single script."); externs = externs.getFirstChild(); String explanation = expected.checkTreeEqualsIncludingJsDoc(externs); assertNull( "\nExpected: " + compiler.toSource(expected) + "\nResult: " + compiler.toSource(externs) + "\n" + explanation, explanation); } else { String externsCode = compiler.toSource(externs); String expectedCode = compiler.toSource(expected); assertEquals(expectedCode, externsCode); } }
private Trie<Object> createTrie() throws ModelOperationException { Trie<Object> trie = new Trie<Object>(); for (AssetContainer assetContainer : app.getAllAssetContainers()) { try { if (assetContainer instanceof BundlableNode) { BundlableNode bundlableNode = (BundlableNode) assetContainer; for (AliasDefinition aliasDefinition : bundlableNode.aliasesFile().aliasDefinitions()) { if (!trie.containsKey(aliasDefinition.getName())) { trie.add(aliasDefinition.getName(), new AliasName(aliasDefinition.getName())); } } } for (SourceFile sourceFile : assetContainer.sourceFiles()) { ClassSourceFile classSourceFile = new ClassSourceFile(sourceFile); if (!sourceFile.getUnderlyingFile().equals(assetFile)) { trie.add(sourceFile.getRequirePath(), sourceFile); trie.add(classSourceFile.getClassName(), classSourceFile); } } for (AssetLocation assetLocation : assetContainer.getAllAssetLocations()) { for (AliasDefinition aliasDefinition : assetLocation.aliasDefinitionsFile().aliasDefinitions()) { if (!trie.containsKey(aliasDefinition.getName())) { trie.add(aliasDefinition.getName(), new AliasName(aliasDefinition.getName())); } } } } catch (TrieKeyAlreadyExistsException | EmptyTrieKeyException | BundlerFileProcessingException ex) { throw new ModelOperationException(ex); } } return trie; }
/** * Verifies that the compiler pass's JS output matches the expected output and (optionally) that * an expected warning is issued. Or, if an error is expected, this method just verifies that the * error is encountered. * * @param js Inputs * @param expected Expected JS output * @param error Expected error, or null if no error is expected * @param warning Expected warning, or null if no warning is expected * @param description The description of the expected warning, or null if no warning is expected * or if the warning's description should not be examined */ public void test( String[] js, String[] expected, DiagnosticType error, DiagnosticType warning, String description) { List<SourceFile> inputs = Lists.newArrayList(); for (int i = 0; i < js.length; i++) { inputs.add(SourceFile.fromCode("input" + i, js[i])); } test(inputs, expected, error, warning, description); }
public void testWithoutExports() { setFilename("test"); testModules( "var name = require('other');" + "name()", "goog.provide('module$test');" + "var module$test = {};" + "goog.require('module$other');" + "var name$$module$test = module$other;" + "name$$module$test();"); setFilename("test/sub"); ProcessEs6ModulesTest.testModules( this, ImmutableList.of( SourceFile.fromCode("mod/name.js", ""), SourceFile.fromCode( "test/sub.js", "var name = require('mod/name');" + "(function() { name(); })();")), "goog.provide('module$test$sub');" + "var module$test$sub = {};" + "goog.require('module$mod$name');" + "var name$$module$test$sub = module$mod$name;" + "(function() { name$$module$test$sub(); })();"); }