本文整理汇总了Java中com.google.javascript.jscomp.Compiler类的典型用法代码示例。如果您正苦于以下问题:Java Compiler类的具体用法?Java Compiler怎么用?Java Compiler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Compiler类属于com.google.javascript.jscomp包,在下文中一共展示了Compiler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: compileFiles
import com.google.javascript.jscomp.Compiler; //导入依赖的package包/类
@SuppressWarnings("deprecation")
private OOPSourceCodeModel compileFiles(List<RawFile> files, List<String> projectFileTypes) {
OOPSourceCodeModel model = new OOPSourceCodeModel();
for (RawFile file : files) {
try {
Compiler compiler = new Compiler();
CompilerOptions options = new CompilerOptions();
options.setIdeMode(true);
options.setParseJsDocDocumentation(JsDocParsing.INCLUDE_DESCRIPTIONS_WITH_WHITESPACE);
compiler.initOptions(options);
Node root = new JsAst(SourceFile.fromCode(file.name(), file.content())).getAstRoot(compiler);
JavaScriptListener jsListener = new JavaScriptListener(model, file, projectFileTypes);
NodeTraversal.traverseEs6(compiler, root, jsListener);
} catch (Exception e) {
e.printStackTrace();
}
}
return model;
}
示例2: execute
import com.google.javascript.jscomp.Compiler; //导入依赖的package包/类
public void execute() {
if (this.outputFile == null) {
throw new BuildException("outputFile attribute must be set");
}
Compiler.setLoggingLevel(Level.OFF);
CompilerOptions options = createCompilerOptions();
Compiler compiler = createCompiler(options);
JSSourceFile[] externs = findExternFiles();
JSSourceFile[] sources = findSourceFiles();
log("Compiling " + sources.length + " file(s) with " +
externs.length + " extern(s)");
Result result = compiler.compile(externs, sources, options);
if (result.success) {
writeResult(compiler.toSource());
}
}
示例3: getDefaultExterns
import com.google.javascript.jscomp.Compiler; //导入依赖的package包/类
/**
* Gets the default externs set.
*
* Adapted from {@link CommandLineRunner}.
*/
private List<JSSourceFile> getDefaultExterns() {
try {
InputStream input = Compiler.class.getResourceAsStream(
"/externs.zip");
ZipInputStream zip = new ZipInputStream(input);
List<JSSourceFile> externs = Lists.newLinkedList();
for (ZipEntry entry; (entry = zip.getNextEntry()) != null; ) {
LimitInputStream entryStream =
new LimitInputStream(zip, entry.getSize());
externs.add(
JSSourceFile.fromInputStream(entry.getName(), entryStream));
}
return externs;
} catch (IOException e) {
throw new BuildException(e);
}
}
示例4: create
import com.google.javascript.jscomp.Compiler; //导入依赖的package包/类
/**
* Creates a symbolic executor which will instrument the given file and then,
* when run, symbolically execute starting by calling entryFunction with the
* given inputs.
*
* @param filename a JavaScript file which will be instrumented and then
* symbolically executed
* @param loader a FileLoader to help load files
* @param newInputGenerator the object that will generate new inputs from old
* ones
* @param maxNumInputs symbolic execution will stop after this many inputs are
* generated. This isn't a strict maximum, though, because new inputs
* are generated in batches, and we stop after the first <em>batch</em>
* puts us at or past this limit.
* @param entryFunction the function at which symbolic execution will start
* @param inputs the initial inputs to use when calling entryFunction
*/
public static SymbolicExecutor create(String filename, FileLoader loader,
NewInputGenerator newInputGenerator, int maxNumInputs,
String entryFunction, String... inputs) {
CompilerOptions options = new CompilerOptions();
// Enable instrumentation
options.symbolicExecutionInstrumentation = true;
options.prettyPrint=true;
// Compile the program; this is where the instrumentation happens.
Compiler compiler = new Compiler();
compiler.compile(new ArrayList<JSSourceFile>(),
Lists.newArrayList(JSSourceFile.fromFile(loader.getPath(filename))),
options);
// Add the call to symbolicExecution.execute
String code = String.format("%s;symbolicExecution.execute(this,%s,inputs)",
compiler.toSource(), entryFunction);
return create(code, loader, newInputGenerator, inputs, maxNumInputs);
}
示例5: execute
import com.google.javascript.jscomp.Compiler; //导入依赖的package包/类
public void execute() {
if (this.outputFile == null) {
throw new BuildException("outputFile attribute must be set");
}
Compiler.setLoggingLevel(Level.OFF);
CompilerOptions options = createCompilerOptions();
Compiler compiler = createCompiler(options);
JSSourceFile[] externs = findExternFiles();
JSSourceFile[] sources = findSourceFiles();
log("Compiling " + sources.length + " file(s) with " +
externs.length + " extern(s)");
Result result = compiler.compile(externs, sources, options);
if (result.success) {
writeResult(compiler.toSource());
} else {
throw new BuildException("Compilation failed.");
}
}
示例6: PropTypesExtractor
import com.google.javascript.jscomp.Compiler; //导入依赖的package包/类
public PropTypesExtractor(
Node propTypesNode,
Node getDefaultPropsNode,
String typeName,
String interfaceTypeName,
Compiler compiler) {
this.propTypesNode = propTypesNode;
this.sourceFileName = propTypesNode.getSourceFileName();
this.getDefaultPropsNode = getDefaultPropsNode;
this.typeName = typeName;
this.interfaceTypeName = interfaceTypeName;
this.compiler = compiler;
// Generate a unique global function name (so that the compiler can more
// easily see that it's a passthrough and inline and remove it).
String sanitizedTypeName = typeName.replaceAll("\\.", "\\$\\$");
this.validatorFuncName = sanitizedTypeName + PROPS_VALIDATOR_SUFFIX;
this.childrenValidatorFuncName =
sanitizedTypeName + CHILDREN_VALIDATOR_SUFFIX;
this.childrenPropTypeNode = null;
}
示例7: testRemoveGoogRequire
import com.google.javascript.jscomp.Compiler; //导入依赖的package包/类
@Test
public void testRemoveGoogRequire() {
String before = "/** @fileoverview blah */\n\n"
+ "goog.provide('js.Foo');\n\n";
String googRequire = "goog.require('abc.def');";
String input =
before
+ googRequire
+ "\n"
+ "/** @private */\n"
+ "function foo_() {};\n";
Compiler compiler = getCompiler(input);
Node root = compileToScriptRoot(compiler);
Match match = new Match(root.getFirstChild(), new NodeMetadata(compiler));
SuggestedFix fix = new SuggestedFix.Builder()
.removeGoogRequire(match, "abc.def")
.build();
CodeReplacement replacement = CodeReplacement.create(before.length(), googRequire.length(), "");
assertReplacement(fix, replacement);
}
示例8: testApplySuggestedFixes_insideJSDoc
import com.google.javascript.jscomp.Compiler; //导入依赖的package包/类
@Test
public void testApplySuggestedFixes_insideJSDoc() throws Exception {
String code = "/** @type {Foo} */\nvar foo = new Foo()";
Compiler compiler = getCompiler(code);
Node root = compileToScriptRoot(compiler);
Node varNode = root.getFirstChild();
Node jsdocRoot =
Iterables.getOnlyElement(varNode.getJSDocInfo().getTypeNodes());
SuggestedFix fix = new SuggestedFix.Builder()
.insertBefore(jsdocRoot, "!")
.build();
List<SuggestedFix> fixes = ImmutableList.of(fix);
Map<String, String> codeMap = ImmutableMap.of("test", code);
Map<String, String> newCodeMap = ApplySuggestedFixes.applySuggestedFixesToCode(
fixes, codeMap);
assertThat(newCodeMap).hasSize(1);
assertThat(newCodeMap).containsEntry("test", "/** @type {!Foo} */\nvar foo = new Foo()");
}
示例9: testRemoveGoogRequire_doesNotExist
import com.google.javascript.jscomp.Compiler; //导入依赖的package包/类
@Test
public void testRemoveGoogRequire_doesNotExist() {
String input =
"goog.require('abc.def');\n"
+ "\n"
+ "/** @private */\n"
+ "function foo_() {};\n";
Compiler compiler = getCompiler(input);
Node root = compileToScriptRoot(compiler);
Match match = new Match(root.getFirstChild(), new NodeMetadata(compiler));
SuggestedFix fix = new SuggestedFix.Builder()
.removeGoogRequire(match, "fakefake")
.build();
SetMultimap<String, CodeReplacement> replacementMap = fix.getReplacements();
assertThat(replacementMap).isEmpty();
}
示例10: map
import com.google.javascript.jscomp.Compiler; //导入依赖的package包/类
@Override
public Resource<String> map(Resource<String> resource) {
try {
CompilerOptions options = new CompilerOptions();
config.getCompilationLevel().setOptionsForCompilationLevel(options);
options.setOutputCharset("UTF-8");
options.setLanguageIn(config.getLanguage());
SourceFile input = SourceFile.fromInputStream("dummy.js",
new ByteArrayInputStream(resource.getContent().getBytes(Charsets.UTF_8)));
List<SourceFile> externs = config.getExterns();
Compiler compiler = new Compiler();
compiler.compile(externs, Lists.newArrayList(input), options);
if (compiler.hasErrors()) {
throw new EvaluatorException(compiler.getErrors()[0].description);
}
return new Resource<String>(resource.getMimeType(), compiler.toSource(), StringHasher.instance);
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
示例11: testPropertyAccess
import com.google.javascript.jscomp.Compiler; //导入依赖的package包/类
@Test
public void testPropertyAccess() {
String input = "foo.bar.method();";
Compiler compiler = getCompiler(input);
Node root = compileToScriptRoot(compiler);
Node methodNode = root.getFirstFirstChild().getFirstChild();
Node barNode = methodNode.getFirstChild();
assertTrue(Matchers.propertyAccess().matches(methodNode, new NodeMetadata(compiler)));
assertTrue(Matchers.propertyAccess().matches(barNode, new NodeMetadata(compiler)));
assertTrue(Matchers.propertyAccess("foo.bar.method").matches(
methodNode, new NodeMetadata(compiler)));
assertTrue(Matchers.propertyAccess("foo.bar").matches(
barNode, new NodeMetadata(compiler)));
assertFalse(Matchers.propertyAccess("foo").matches(
barNode.getFirstChild(), new NodeMetadata(compiler)));
}
示例12: compile
import com.google.javascript.jscomp.Compiler; //导入依赖的package包/类
public CompileResult compile(Path path, String code) {
Compiler compiler = compiler();
Result result =
compiler.compile(EXTERNS, SourceFile.fromCode(path.toString(), code), options());
String source = compiler.toSource();
StringBuilder sourceMap = new StringBuilder();
if (result.sourceMap != null) {
try {
result.sourceMap.appendTo(sourceMap, path.toString());
} catch (IOException e) {
// impossible, and not a big deal even if it did happen.
}
}
boolean transpiled = !result.transpiledFiles.isEmpty();
if (result.errors.length > 0) {
throw new TranspilationException(compiler, result.errors, result.warnings);
}
return new CompileResult(
source,
transpiled,
transpiled ? sourceMap.toString() : "");
}
示例13: testConstructorPropertyDeclaration
import com.google.javascript.jscomp.Compiler; //导入依赖的package包/类
@Test
public void testConstructorPropertyDeclaration() {
String externs = "";
String input = ""
+ "/** @constructor */\n"
+ "function MyClass() {\n"
+ " this.foo = 5;\n"
+ " var bar = 10;\n"
+ "}";
Compiler compiler = getCompiler(externs, input);
Node root = compileToScriptRoot(compiler);
// The ASSIGN node
Node node = root.getFirstChild().getLastChild().getFirstFirstChild();
assertTrue(
Matchers.constructorPropertyDeclaration().matches(node, new NodeMetadata(compiler)));
// The VAR node
node = root.getFirstChild().getLastChild().getLastChild().getFirstChild();
assertFalse(
Matchers.constructorPropertyDeclaration().matches(node, new NodeMetadata(compiler)));
}
示例14: compile
import com.google.javascript.jscomp.Compiler; //导入依赖的package包/类
public CompileResult compile(Path path, String code) {
Compiler compiler = compiler();
Result result =
compiler.compile(EXTERNS, SourceFile.fromCode(path.toString(), code), options());
String source = compiler.toSource();
StringBuilder sourceMap = new StringBuilder();
if (result.sourceMap != null) {
try {
result.sourceMap.appendTo(sourceMap, path.toString());
} catch (IOException e) {
// impossible, and not a big deal even if it did happen.
}
}
boolean transformed = transformed(result);
return new CompileResult(
source, result.errors, transformed, transformed ? sourceMap.toString() : "");
}
示例15: testIsPrivate
import com.google.javascript.jscomp.Compiler; //导入依赖的package包/类
@Test
public void testIsPrivate() {
String externs = "";
String input = ""
+ "/** @private */\n"
+ "var foo = 3;";
Compiler compiler = getCompiler(externs, input);
Node root = compileToScriptRoot(compiler);
Node node = root.getFirstChild();
assertTrue(Matchers.isPrivate().matches(node, new NodeMetadata(compiler)));
input = ""
+ "/** @package */\n"
+ "var foo = 3;";
compiler = getCompiler(externs, input);
root = compileToScriptRoot(compiler);
node = root.getFirstChild();
assertFalse(Matchers.isPrivate().matches(node, new NodeMetadata(compiler)));
}