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));
}
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);
}
}
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;
}
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);
}
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);
}
}
Aggregations