本文整理汇总了Java中com.sun.tools.javac.tree.TreeScanner类的典型用法代码示例。如果您正苦于以下问题:Java TreeScanner类的具体用法?Java TreeScanner怎么用?Java TreeScanner使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TreeScanner类属于com.sun.tools.javac.tree包,在下文中一共展示了TreeScanner类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: translateTopLevelClass
import com.sun.tools.javac.tree.TreeScanner; //导入依赖的package包/类
@Override
public List<JCTree> translateTopLevelClass(Env<AttrContext> env, JCTree cdef, TreeMaker make) {
List<JCTree> result = super.translateTopLevelClass(env, cdef, make);
new TreeScanner() {
@Override
public void visitBinary(JCBinary tree) {
hasNullCheck |= tree.operator.getSimpleName().contentEquals("!=") &&
"resource".equals(String.valueOf(TreeInfo.name(tree.lhs))) &&
TreeInfo.isNull(tree.rhs);
super.visitBinary(tree);
}
}.scan(result);
return result;
}
示例2: run
import com.sun.tools.javac.tree.TreeScanner; //导入依赖的package包/类
void run() throws Exception {
Context context = new Context();
JavacFileManager.preRegister(context);
final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, Arrays.asList(new JavaSource()));
Iterable<? extends CompilationUnitTree> elements = ct.parse();
ct.analyze();
Assert.check(elements.iterator().hasNext());
JCTree topLevel = (JCTree)elements.iterator().next();
new TreeScanner() {
@Override
public void visitReference(JCMemberReference tree) {
Assert.check(tree.getOverloadKind() != null);
}
}.scan(topLevel);
}
示例3: translateTopLevelClass
import com.sun.tools.javac.tree.TreeScanner; //导入依赖的package包/类
@Override
public List<JCTree> translateTopLevelClass(Env<AttrContext> env, JCTree cdef, TreeMaker make) {
List<JCTree> result = super.translateTopLevelClass(env, cdef, make);
Map<Symbol, JCMethodDecl> declarations = new HashMap<>();
Set<Symbol> toDump = new TreeSet<>(symbolComparator);
new TreeScanner() {
@Override
public void visitMethodDef(JCMethodDecl tree) {
if (tree.name.toString().startsWith("dump")) {
toDump.add(tree.sym);
}
declarations.put(tree.sym, tree);
super.visitMethodDef(tree);
}
}.scan(result);
for (Symbol d : toDump) {
dump(d, declarations, new HashSet<>());
}
return result;
}
示例4: compile
import com.sun.tools.javac.tree.TreeScanner; //导入依赖的package包/类
protected void compile(TreeScanner scanner, JavaFileObject fileObject) {
JavaCompiler compiler = JavacTool.create();
DiagnosticCollector<JavaFileObject> diagnosticsCollector =
new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager =
compiler.getStandardFileManager(diagnosticsCollector, Locale.ENGLISH, UTF_8);
JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(CharStreams.nullWriter(),
fileManager,
diagnosticsCollector,
ImmutableList.<String>of(),
null,
ImmutableList.of(fileObject));
try {
this.sourceFile = SourceFile.create(fileObject);
Iterable<? extends CompilationUnitTree> trees = task.parse();
task.analyze();
for (CompilationUnitTree tree : trees) {
scanner.scan((JCCompilationUnit) tree);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
this.context = task.getContext();
}
示例5: isStringConcat
import com.sun.tools.javac.tree.TreeScanner; //导入依赖的package包/类
private boolean isStringConcat(ExpressionTree first) {
final boolean[] stringLiteral = {true};
final boolean[] formatString = {false};
new TreeScanner() {
@Override
public void scan(JCTree tree) {
if (tree == null) {
return;
}
switch (tree.getKind()) {
case STRING_LITERAL:
break;
case PLUS:
super.scan(tree);
break;
default:
stringLiteral[0] = false;
break;
}
if (tree.getKind() == STRING_LITERAL) {
Object value = ((LiteralTree) tree).getValue();
if (value instanceof String && FORMAT_SPECIFIER.matcher(value.toString()).find()) {
formatString[0] = true;
}
}
}
}.scan((JCTree) first);
return stringLiteral[0] && formatString[0];
}
示例6: test
import com.sun.tools.javac.tree.TreeScanner; //导入依赖的package包/类
static void test(boolean genEndPos) throws IOException {
Context context = new Context();
Options options = Options.instance(context);
options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");
Log log = Log.instance(context);
log.multipleErrors = true;
JavacFileManager.preRegister(context);
ParserFactory pfac = ParserFactory.instance(context);
final String text =
"public class Foo {\n"
+ " public static void main(String[] args) {\n"
+ " if (args.length == 0)\n"
+ " System.out.println(\"no args\");\n"
+ " else\n"
+ " System.out.println(args.length + \" args\");\n"
+ " }\n"
+ "}\n";
JavaFileObject fo = new StringJavaFileObject("Foo", text);
log.useSource(fo);
CharSequence cs = fo.getCharContent(true);
Parser parser = pfac.newParser(cs, false, genEndPos, false);
JCTree.JCCompilationUnit tree = parser.parseCompilationUnit();
log.setEndPosTable(fo, tree.endPositions);
TreeScanner ts = new LogTester(log, tree.endPositions);
ts.scan(tree);
check(log.nerrors, 4, "errors");
check(log.nwarnings, 4, "warnings");
}
示例7: test
import com.sun.tools.javac.tree.TreeScanner; //导入依赖的package包/类
static void test(boolean genEndPos) throws IOException {
Context context = new Context();
Options options = Options.instance(context);
options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");
Log log = Log.instance(context);
log.multipleErrors = true;
JavacFileManager.preRegister(context);
Scanner.Factory sfac = Scanner.Factory.instance(context);
Parser.Factory pfac = Parser.Factory.instance(context);
final String text =
"public class Foo {\n"
+ " public static void main(String[] args) {\n"
+ " if (args.length == 0)\n"
+ " System.out.println(\"no args\");\n"
+ " else\n"
+ " System.out.println(args.length + \" args\");\n"
+ " }\n"
+ "}\n";
JavaFileObject fo = new StringJavaFileObject("Foo", text);
log.useSource(fo);
Scanner s = sfac.newScanner(fo.getCharContent(true));
Parser parser = pfac.newParser(s, false, genEndPos);
JCTree.JCCompilationUnit tree = parser.compilationUnit();
log.setEndPosTable(fo, tree.endPositions);
TreeScanner ts = new LogTester(log, tree.endPositions);
ts.scan(tree);
check(log.nerrors, 4, "errors");
check(log.nwarnings, 4, "warnings");
}
示例8: renameTypeVariable
import com.sun.tools.javac.tree.TreeScanner; //导入依赖的package包/类
static SuggestedFix renameTypeVariable(
TypeParameterTree typeParameter,
Tree owningTree,
String typeVarReplacement,
VisitorState state) {
Symbol typeVariableSymbol = ASTHelpers.getSymbol(typeParameter);
// replace only the type parameter name (and not any upper bounds)
String name = typeParameter.getName().toString();
int pos = ((JCTree) typeParameter).getStartPosition();
SuggestedFix.Builder fixBuilder =
SuggestedFix.builder().replace(pos, pos + name.length(), typeVarReplacement);
((JCTree) owningTree)
.accept(
new TreeScanner() {
@Override
public void visitIdent(JCTree.JCIdent tree) {
Symbol identSym = ASTHelpers.getSymbol(tree);
if (Objects.equal(identSym, typeVariableSymbol)) {
// Lambda parameters can be desugared early, so we need to make sure the source
// is there. In the example below, we would try to suggest replacing the node 't'
// with T2, since the compiler desugars to g((T t) -> false). The extra condition
// prevents us from doing that.
// Foo<T> {
// <G> void g(Predicate<G> p) {},
// <T> void blah() {
// g(t -> false);
// }
// }
if (Objects.equal(state.getSourceForNode(tree), name)) {
fixBuilder.replace(tree, typeVarReplacement);
}
}
}
});
return fixBuilder.build();
}
示例9: compile
import com.sun.tools.javac.tree.TreeScanner; //导入依赖的package包/类
protected void compile(TreeScanner scanner, JavaFileObject fileObject) {
JavaCompiler compiler = JavacTool.create();
DiagnosticCollector<JavaFileObject> diagnosticsCollector =
new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager =
compiler.getStandardFileManager(diagnosticsCollector, Locale.ENGLISH, UTF_8);
JavacTaskImpl task =
(JavacTaskImpl)
compiler.getTask(
CharStreams.nullWriter(),
fileManager,
diagnosticsCollector,
ImmutableList.<String>of(),
null,
ImmutableList.of(fileObject));
try {
this.sourceFile = SourceFile.create(fileObject);
Iterable<? extends CompilationUnitTree> trees = task.parse();
task.analyze();
for (CompilationUnitTree tree : trees) {
scanner.scan((JCCompilationUnit) tree);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
this.context = task.getContext();
}
示例10: postAttribute
import com.sun.tools.javac.tree.TreeScanner; //导入依赖的package包/类
/**
* Records the super type closure of all class declarations in the given compilation unit. Called
* after attribute is complete for a compilation unit.
*/
public void postAttribute(Env<AttrContext> result) {
result.toplevel.accept(
new TreeScanner() {
@Override
public void visitClassDef(JCClassDecl tree) {
recordSuperClosure(tree.sym);
super.visitClassDef(tree);
}
});
}
示例11: test
import com.sun.tools.javac.tree.TreeScanner; //导入依赖的package包/类
static void test(boolean genEndPos) throws Exception {
Context context = new Context();
Options options = Options.instance(context);
options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");
Log log = Log.instance(context);
Factory diagnosticFactory = JCDiagnostic.Factory.instance(context);
Field defaultErrorFlagsField =
JCDiagnostic.Factory.class.getDeclaredField("defaultErrorFlags");
defaultErrorFlagsField.setAccessible(true);
Set<DiagnosticFlag> defaultErrorFlags =
(Set<DiagnosticFlag>) defaultErrorFlagsField.get(diagnosticFactory);
defaultErrorFlags.add(DiagnosticFlag.MULTIPLE);
JavacFileManager.preRegister(context);
ParserFactory pfac = ParserFactory.instance(context);
final String text =
"public class Foo {\n"
+ " public static void main(String[] args) {\n"
+ " if (args.length == 0)\n"
+ " System.out.println(\"no args\");\n"
+ " else\n"
+ " System.out.println(args.length + \" args\");\n"
+ " }\n"
+ "}\n";
JavaFileObject fo = new StringJavaFileObject("Foo", text);
log.useSource(fo);
CharSequence cs = fo.getCharContent(true);
Parser parser = pfac.newParser(cs, false, genEndPos, false);
JCTree.JCCompilationUnit tree = parser.parseCompilationUnit();
log.setEndPosTable(fo, tree.endPositions);
TreeScanner ts = new LogTester(log, tree.endPositions);
ts.scan(tree);
check(log.nerrors, 4, "errors");
check(log.nwarnings, 4, "warnings");
}