本文整理汇总了Java中com.sun.tools.javac.tree.JCTree类的典型用法代码示例。如果您正苦于以下问题:Java JCTree类的具体用法?Java JCTree怎么用?Java JCTree使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JCTree类属于com.sun.tools.javac.tree包,在下文中一共展示了JCTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testPreferredPositionForBinaryOp
import com.sun.tools.javac.tree.JCTree; //导入依赖的package包/类
@Test
void testPreferredPositionForBinaryOp() throws IOException {
String code = "package test; public class Test {"
+ "private void test() {"
+ "Object o = null; boolean b = o != null && o instanceof String;"
+ "} private Test() {}}";
CompilationUnitTree cut = getCompilationUnitTree(code);
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
MethodTree method = (MethodTree) clazz.getMembers().get(0);
VariableTree condSt = (VariableTree) method.getBody().getStatements().get(1);
BinaryTree cond = (BinaryTree) condSt.getInitializer();
JCTree condJC = (JCTree) cond;
int condStartPos = code.indexOf("&&");
assertEquals("testPreferredPositionForBinaryOp",
condStartPos, condJC.pos);
}
示例2: visitTry
import com.sun.tools.javac.tree.JCTree; //导入依赖的package包/类
@Override
public void visitTry(JCTry tree) {
print("try");
if (!tree.getResources().isEmpty()) {
print(" ("); //XXX: space should be according to the code style!
for (Iterator<? extends JCTree> it = tree.getResources().iterator(); it.hasNext();) {
JCTree r = it.next();
//XXX: disabling copying of original text, as the ending ';' needs to be removed in some cases.
oldTrees.remove(r);
printPrecedingComments(r, false);
printExpr(r, 0);
printTrailingComments(r, false);
if (it.hasNext()) print(";");
}
print(") "); //XXX: space should be according to the code style!
}
printBlock(tree.body, cs.getOtherBracePlacement(), cs.spaceBeforeTryLeftBrace());
for (List < JCCatch > l = tree.catchers; l.nonEmpty(); l = l.tail)
printStat(l.head);
if (tree.finalizer != null) {
printFinallyBlock(tree.finalizer);
}
}
示例3: reorderBySourcePosition
import com.sun.tools.javac.tree.JCTree; //导入依赖的package包/类
/**
* Rotate the list of dimension specifiers until all dimensions with type annotations appear in
* source order.
* <p>
* <p>javac reorders dimension specifiers in method declarations with mixed-array notation, which
* means that any type annotations don't appear in source order.
* <p>
* <p>For example, the type of {@code int @A [] f() @B [] {}} is parsed as {@code @B [] @A []}.
* <p>
* <p>This doesn't handle cases with un-annotated dimension specifiers, so the formatting logic
* checks the token stream to figure out which side of the method name they appear on.
*/
private static Iterable<List<AnnotationTree>> reorderBySourcePosition(
Deque<List<AnnotationTree>> dims) {
int lastAnnotation = -1;
int lastPos = -1;
int idx = 0;
for (List<AnnotationTree> dim : dims) {
if (!dim.isEmpty()) {
int pos = ((JCTree) dim.get(0)).getStartPosition();
if (pos < lastPos) {
List<List<AnnotationTree>> list = new ArrayList<>(dims);
Collections.rotate(list, -(lastAnnotation + 1));
return list;
}
lastPos = pos;
lastAnnotation = idx;
}
idx++;
}
return dims;
}
示例4: visitApply
import com.sun.tools.javac.tree.JCTree; //导入依赖的package包/类
@Override
public void visitApply(JCMethodInvocation tree) {
boolean prevCheckThis = checkThis;
try {
Symbol meth = TreeInfo.symbol(tree.meth);
Name methName = TreeInfo.name(tree.meth);
if (meth != null && meth.name == names.init) {
Symbol c = meth.owner;
if (c.hasOuterInstance()) {
checkThis = false;
if (tree.meth.getTag() != JCTree.Tag.SELECT && (c.isLocal() || methName == names._this)) {
checkThis(tree.meth.pos(), c.type.getEnclosingType().tsym);
}
}
}
super.visitApply(tree);
} finally {
checkThis = prevCheckThis;
}
}
示例5: finished
import com.sun.tools.javac.tree.JCTree; //导入依赖的package包/类
@Override @DefinedBy(Api.COMPILER_TREE)
public void finished(TaskEvent taskEvent) {
List<AbstractCodingRulesAnalyzer> currentAnalyzers = this.analyzers.get(taskEvent.getKind());
if (currentAnalyzers != null) {
TypeElement typeElem = taskEvent.getTypeElement();
Tree tree = trees.getTree(typeElem);
if (tree != null) {
JavaFileObject prevSource = log.currentSourceFile();
try {
log.useSource(taskEvent.getCompilationUnit().getSourceFile());
for (AbstractCodingRulesAnalyzer analyzer : currentAnalyzers) {
analyzer.treeVisitor.scan((JCTree)tree);
}
} finally {
log.useSource(prevSource);
}
}
}
}
示例6: getSourcePosition
import com.sun.tools.javac.tree.JCTree; //导入依赖的package包/类
public JavacSourcePosition getSourcePosition(Element e, AnnotationMirror a) {
Pair<JCTree, JCCompilationUnit> treeTop = getTreeAndTopLevel(e);
if (treeTop == null)
return null;
JCTree tree = treeTop.fst;
JCCompilationUnit toplevel = treeTop.snd;
JavaFileObject sourcefile = toplevel.sourcefile;
if (sourcefile == null)
return null;
JCTree annoTree = matchAnnoToTree(a, e, tree);
if (annoTree == null)
return null;
return new JavacSourcePosition(sourcefile, annoTree.pos,
toplevel.lineMap);
}
示例7: getElement
import com.sun.tools.javac.tree.JCTree; //导入依赖的package包/类
public Element getElement(TreePath path) {
JCTree tree = (JCTree) path.getLeaf();
Symbol sym = TreeInfo.symbolFor(tree);
if (sym == null && TreeInfo.isDeclaration(tree)) {
for (TreePath p = path; p != null; p = p.getParentPath()) {
JCTree t = (JCTree) p.getLeaf();
if (t.getTag() == JCTree.CLASSDEF) {
JCClassDecl ct = (JCClassDecl) t;
if (ct.sym != null) {
if ((ct.sym.flags_field & Flags.UNATTRIBUTED) != 0) {
attr.attribClass(ct.pos(), ct.sym);
sym = TreeInfo.symbolFor(tree);
}
break;
}
}
}
}
return sym;
}
示例8: run
import com.sun.tools.javac.tree.JCTree; //导入依赖的package包/类
void run(String code) throws IOException {
String src = "public class Test {" + code + ";}";
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
null, Arrays.asList(new MyFileObject(src)));
for (CompilationUnitTree cut : ct.parse()) {
JCTree.JCVariableDecl var =
(JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
if (!code.equals(var.toString())) {
System.err.println("Expected: " + code);
System.err.println("Obtained: " + var.toString());
throw new RuntimeException("strings do not match!");
}
}
}
示例9: getTree
import com.sun.tools.javac.tree.JCTree; //导入依赖的package包/类
public JCTree getTree(Element element) {
Symbol symbol = (Symbol) element;
TypeSymbol enclosing = symbol.enclClass();
Env<AttrContext> env = enter.getEnv(enclosing);
if (env == null)
return null;
JCClassDecl classNode = env.enclClass;
if (classNode != null) {
if (TreeInfo.symbolFor(classNode) == element)
return classNode;
for (JCTree node : classNode.getMembers())
if (TreeInfo.symbolFor(node) == element)
return node;
}
return null;
}
示例10: gatherTreeSpans
import com.sun.tools.javac.tree.JCTree; //导入依赖的package包/类
public List<int[]> gatherTreeSpans(File file, String content) throws IOException {
JCCompilationUnit unit = read(file.toURI(), content);
List<int[]> spans = new ArrayList<>();
new TreePathScanner<Void, Void>() {
@Override
public Void scan(Tree tree, Void p) {
if (tree != null) {
int start = ((JCTree) tree).getStartPosition();
int end = ((JCTree) tree).getEndPosition(unit.endPositions);
spans.add(new int[] {start, end});
}
return super.scan(tree, p);
}
}.scan(unit, null);
return spans;
}
示例11: resolve
import com.sun.tools.javac.tree.JCTree; //导入依赖的package包/类
/**
* Resolves an {@link Element} from the {@link ElementHandle}.
* @param compilationInfo representing the {@link javax.tools.CompilationTask}
* in which the {@link Element} should be resolved.
* @return resolved subclass of {@link Element} or null if the elment does not exist on
* the classpath/sourcepath of {@link javax.tools.CompilationTask}.
*/
@SuppressWarnings ("unchecked") // NOI18N
public @CheckForNull T resolve (@NonNull final CompilationInfo compilationInfo) {
Parameters.notNull("compilationInfo", compilationInfo); // NOI18N
ModuleElement module = compilationInfo.getFileObject() != null
? ((JCTree.JCCompilationUnit)compilationInfo.getCompilationUnit()).modle
: null;
T result = resolveImpl (module, compilationInfo.impl.getJavacTask());
if (result == null) {
if (log.isLoggable(Level.INFO))
log.log(Level.INFO, "Cannot resolve: {0}", toString()); //NOI18N
} else {
if (log.isLoggable(Level.FINE))
log.log(Level.FINE, "Resolved element = {0}", result);
}
return result;
}
示例12: getClassDecl
import com.sun.tools.javac.tree.JCTree; //导入依赖的package包/类
private JCTree.JCClassDecl getClassDecl( Model model )
{
JCTree.JCClassDecl classDecl = model.getClassDecl();
if( classDecl != null )
{
return classDecl;
}
List<CompilationUnitTree> trees = new ArrayList<>();
JavaParser.instance().parseText( getSource( model ), trees, null, null, null );
if( trees.isEmpty() )
{
return null;
}
classDecl = (JCTree.JCClassDecl)trees.get( 0 ).getTypeDecls().get( 0 );
model.setClassDecl( classDecl );
return classDecl;
}
示例13: visitLambda
import com.sun.tools.javac.tree.JCTree; //导入依赖的package包/类
@Override
public void visitLambda( JCTree.JCLambda tree )
{
super.visitLambda( tree );
if( _tp.isGenerate() && !shouldProcessForGeneration() )
{
// Don't process tree during GENERATE, unless the tree was generated e.g., a bridge method
return;
}
tree.type = eraseStructureType( tree.type );
ArrayList<Type> types = new ArrayList<>();
for( Type target : tree.targets )
{
types.add( eraseStructureType( target ) );
}
tree.targets = List.from( types );
}
示例14: run
import com.sun.tools.javac.tree.JCTree; //导入依赖的package包/类
void run() throws Exception {
Context context = new Context();
JavacFileManager.preRegister(context);
Trees trees = JavacTrees.instance(context);
StringWriter strOut = new StringWriter();
PrintWriter pw = new PrintWriter(strOut);
DPrinter dprinter = new DPrinter(pw, trees);
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());
dprinter.treeTypes(true).printTree("", (JCTree)elements.iterator().next());
String output = strOut.toString();
Assert.check(!output.contains("java.lang.Object"), "there shouldn't be any type instantiated to Object");
}
示例15: getResourceName
import com.sun.tools.javac.tree.JCTree; //导入依赖的package包/类
@CheckForNull
private static String getResourceName (@NullAllowed final CompilationUnitTree cu) {
if (cu instanceof JCTree.JCCompilationUnit) {
JavaFileObject jfo = ((JCTree.JCCompilationUnit)cu).sourcefile;
if (jfo != null) {
URI uri = jfo.toUri();
if (uri != null && uri.isAbsolute()) {
try {
FileObject fo = URLMapper.findFileObject(uri.toURL());
if (fo != null) {
ClassPath cp = ClassPath.getClassPath(fo,ClassPath.SOURCE);
if (cp != null) {
return cp.getResourceName(fo,'.',false);
}
}
} catch (MalformedURLException e) {
Exceptions.printStackTrace(e);
}
}
}
}
return null;
}