Search in sources :

Example 36 with TreePath

use of com.sun.source.util.TreePath in project st-js by st-js.

the class InstanceofWriter method visit.

@SuppressWarnings("unchecked")
@Override
public JS visit(WriterVisitor<JS> visitor, InstanceOfTree tree, GenerationContext<JS> context) {
    // build stjs.isInstanceOf(expr.constructor, type);
    // TODO do I need a check or parenthesis around !?
    TypeMirror type = context.getTrees().getTypeMirror(new TreePath(context.getCurrentPath(), tree.getType()));
    JS getConstructor = context.js().property(visitor.scan(tree.getExpression(), context), JavascriptKeywords.CONSTRUCTOR);
    JS targetInst = context.js().property(context.js().name("stjs"), "isInstanceOf");
    JS typeName = context.js().name(context.getNames().getTypeName(context, type, DependencyType.STATIC));
    return context.js().functionCall(targetInst, Arrays.asList(getConstructor, typeName));
}
Also used : TreePath(com.sun.source.util.TreePath) TypeMirror(javax.lang.model.type.TypeMirror)

Example 37 with TreePath

use of com.sun.source.util.TreePath in project st-js by st-js.

the class TreePathScannerContributors method scan.

@Override
public R scan(Tree tree, P p) {
    if (tree == null) {
        return null;
    }
    TreePath prev = p.getCurrentPath();
    p.setCurrentPath(new TreePath(prev, tree));
    try {
        return visit(tree, p, null);
    } finally {
        p.setCurrentPath(prev);
    }
}
Also used : TreePath(com.sun.source.util.TreePath)

Example 38 with TreePath

use of com.sun.source.util.TreePath in project st-js by st-js.

the class GenerationContext method getTreePath.

private TreePath getTreePath(Element enclosingElement) {
    TreePath path = trees.getPath(enclosingElement);
    if (path == null) {
        Tree tree = trees.getTree(enclosingElement);
        if (tree == null) {
            tree = new DummyTree();
        }
        // XXX: this is ugly, null is better here?
        path = new TreePath(new TreePath(compilationUnit), tree);
    }
    return path;
}
Also used : TreePath(com.sun.source.util.TreePath) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) Tree(com.sun.source.tree.Tree)

Example 39 with TreePath

use of com.sun.source.util.TreePath in project st-js by st-js.

the class MemberReferenceWriter method visit.

@Override
public JS visit(WriterVisitor<JS> visitor, MemberReferenceTree tree, GenerationContext<JS> context) {
    ExecutableElement methodElement = (ExecutableElement) context.getTrees().getElement(context.getCurrentPath());
    Element qualifierElement = context.getTrees().getElement(new TreePath(context.getCurrentPath(), tree.getQualifierExpression()));
    // qualifierElemenet.getKind());
    if (tree.getMode() == ReferenceMode.INVOKE) {
        if (qualifierElement.getKind() == ElementKind.CLASS || qualifierElement.getKind() == ElementKind.INTERFACE) {
            if (JavaNodes.isStatic(methodElement)) {
                return generateStaticRef(tree, context, methodElement);
            }
            return generateInstanceRef(tree, context, methodElement);
        }
        return generateCapturingRef(visitor, tree, context, methodElement);
    }
    return generateConstructorRef(context, methodElement);
}
Also used : TreePath(com.sun.source.util.TreePath) ExecutableElement(javax.lang.model.element.ExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element)

Example 40 with TreePath

use of com.sun.source.util.TreePath in project error-prone by google.

the class CodeTransformerTestHelper method transform.

public JavaFileObject transform(JavaFileObject original) {
    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(original));
    try {
        SourceFile sourceFile = SourceFile.create(original);
        Iterable<? extends CompilationUnitTree> trees = task.parse();
        task.analyze();
        JCCompilationUnit tree = Iterables.getOnlyElement(Iterables.filter(trees, JCCompilationUnit.class));
        DescriptionBasedDiff diff = DescriptionBasedDiff.create(tree, ImportOrganizer.STATIC_FIRST_ORGANIZER);
        transformer().apply(new TreePath(tree), task.getContext(), diff);
        diff.applyDifferences(sourceFile);
        return JavaFileObjects.forSourceString(Iterables.getOnlyElement(Iterables.filter(tree.getTypeDecls(), JCClassDecl.class)).sym.getQualifiedName().toString(), sourceFile.getSourceText());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) DescriptionBasedDiff(com.google.errorprone.apply.DescriptionBasedDiff) JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavaCompiler(javax.tools.JavaCompiler) IOException(java.io.IOException) JavaFileObject(javax.tools.JavaFileObject) TreePath(com.sun.source.util.TreePath) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) SourceFile(com.google.errorprone.apply.SourceFile)

Aggregations

TreePath (com.sun.source.util.TreePath)151 ExpressionTree (com.sun.source.tree.ExpressionTree)60 Tree (com.sun.source.tree.Tree)60 VariableTree (com.sun.source.tree.VariableTree)50 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)46 MethodTree (com.sun.source.tree.MethodTree)46 ClassTree (com.sun.source.tree.ClassTree)45 MemberSelectTree (com.sun.source.tree.MemberSelectTree)39 IdentifierTree (com.sun.source.tree.IdentifierTree)37 BlockTree (com.sun.source.tree.BlockTree)36 NewClassTree (com.sun.source.tree.NewClassTree)32 StatementTree (com.sun.source.tree.StatementTree)32 JCTree (com.sun.tools.javac.tree.JCTree)31 AssignmentTree (com.sun.source.tree.AssignmentTree)27 BinaryTree (com.sun.source.tree.BinaryTree)27 TypeCastTree (com.sun.source.tree.TypeCastTree)26 ExpressionStatementTree (com.sun.source.tree.ExpressionStatementTree)25 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)25 LiteralTree (com.sun.source.tree.LiteralTree)25 CompoundAssignmentTree (com.sun.source.tree.CompoundAssignmentTree)23