use of com.sun.tools.javac.tree.JCTree in project epoxy by airbnb.
the class ResourceProcessor method processorResources.
void processorResources(RoundEnvironment env) {
resources.clear();
if (trees == null) {
return;
}
RClassScanner scanner = new RClassScanner();
for (Class<? extends Annotation> annotation : EpoxyProcessor.getSupportedAnnotations()) {
for (Element element : env.getElementsAnnotatedWith(annotation)) {
JCTree tree = (JCTree) trees.getTree(element, getMirror(element, annotation));
if (tree != null) {
// tree can be null if the references are compiled types and not source
tree.accept(scanner);
}
}
}
for (String rClass : scanner.getRClasses()) {
parseRClass(rClass, resources);
}
}
use of com.sun.tools.javac.tree.JCTree in project butterknife by JakeWharton.
the class ButterKnifeProcessor method scanForRClasses.
private void scanForRClasses(RoundEnvironment env) {
if (trees == null)
return;
RClassScanner scanner = new RClassScanner();
for (Class<? extends Annotation> annotation : getSupportedAnnotations()) {
for (Element element : env.getElementsAnnotatedWith(annotation)) {
JCTree tree = (JCTree) trees.getTree(element, getMirror(element, annotation));
if (tree != null) {
// tree can be null if the references are compiled types and not source
String respectivePackageName = elementUtils.getPackageOf(element).getQualifiedName().toString();
scanner.setCurrentPackageName(respectivePackageName);
tree.accept(scanner);
}
}
}
for (Map.Entry<String, Set<String>> packageNameToRClassSet : scanner.getRClasses().entrySet()) {
String respectivePackageName = packageNameToRClassSet.getKey();
for (String rClass : packageNameToRClassSet.getValue()) {
parseRClass(respectivePackageName, rClass);
}
}
}
use of com.sun.tools.javac.tree.JCTree in project butterknife by JakeWharton.
the class ButterKnifeProcessor method parseRClass.
private void parseRClass(String respectivePackageName, String rClass) {
Element element;
try {
element = elementUtils.getTypeElement(rClass);
} catch (MirroredTypeException mte) {
element = typeUtils.asElement(mte.getTypeMirror());
}
JCTree tree = (JCTree) trees.getTree(element);
if (tree != null) {
// tree can be null if the references are compiled types and not source
IdScanner idScanner = new IdScanner(symbols, elementUtils.getPackageOf(element).getQualifiedName().toString(), respectivePackageName);
tree.accept(idScanner);
} else {
parseCompiledR(respectivePackageName, (TypeElement) element);
}
}
use of com.sun.tools.javac.tree.JCTree in project lombok by rzwitserloot.
the class HandleCleanup method handle.
@Override
public void handle(AnnotationValues<Cleanup> annotation, JCAnnotation ast, JavacNode annotationNode) {
handleFlagUsage(annotationNode, ConfigurationKeys.CLEANUP_FLAG_USAGE, "@Cleanup");
if (inNetbeansEditor(annotationNode))
return;
deleteAnnotationIfNeccessary(annotationNode, Cleanup.class);
String cleanupName = annotation.getInstance().value();
if (cleanupName.length() == 0) {
annotationNode.addError("cleanupName cannot be the empty string.");
return;
}
if (annotationNode.up().getKind() != Kind.LOCAL) {
annotationNode.addError("@Cleanup is legal only on local variable declarations.");
return;
}
JCVariableDecl decl = (JCVariableDecl) annotationNode.up().get();
if (decl.init == null) {
annotationNode.addError("@Cleanup variable declarations need to be initialized.");
return;
}
JavacNode ancestor = annotationNode.up().directUp();
JCTree blockNode = ancestor.get();
final List<JCStatement> statements;
if (blockNode instanceof JCBlock) {
statements = ((JCBlock) blockNode).stats;
} else if (blockNode instanceof JCCase) {
statements = ((JCCase) blockNode).stats;
} else if (blockNode instanceof JCMethodDecl) {
statements = ((JCMethodDecl) blockNode).body.stats;
} else {
annotationNode.addError("@Cleanup is legal only on a local variable declaration inside a block.");
return;
}
boolean seenDeclaration = false;
ListBuffer<JCStatement> newStatements = new ListBuffer<JCStatement>();
ListBuffer<JCStatement> tryBlock = new ListBuffer<JCStatement>();
for (JCStatement statement : statements) {
if (!seenDeclaration) {
if (statement == decl)
seenDeclaration = true;
newStatements.append(statement);
} else {
tryBlock.append(statement);
}
}
if (!seenDeclaration) {
annotationNode.addError("LOMBOK BUG: Can't find this local variable declaration inside its parent.");
return;
}
doAssignmentCheck(annotationNode, tryBlock.toList(), decl.name);
JavacTreeMaker maker = annotationNode.getTreeMaker();
JCFieldAccess cleanupMethod = maker.Select(maker.Ident(decl.name), annotationNode.toName(cleanupName));
List<JCStatement> cleanupCall = List.<JCStatement>of(maker.Exec(maker.Apply(List.<JCExpression>nil(), cleanupMethod, List.<JCExpression>nil())));
JCExpression preventNullAnalysis = preventNullAnalysis(maker, annotationNode, maker.Ident(decl.name));
JCBinary isNull = maker.Binary(CTC_NOT_EQUAL, preventNullAnalysis, maker.Literal(CTC_BOT, null));
JCIf ifNotNullCleanup = maker.If(isNull, maker.Block(0, cleanupCall), null);
Context context = annotationNode.getContext();
JCBlock finalizer = recursiveSetGeneratedBy(maker.Block(0, List.<JCStatement>of(ifNotNullCleanup)), ast, context);
newStatements.append(setGeneratedBy(maker.Try(setGeneratedBy(maker.Block(0, tryBlock.toList()), ast, context), List.<JCCatch>nil(), finalizer), ast, context));
if (blockNode instanceof JCBlock) {
((JCBlock) blockNode).stats = newStatements.toList();
} else if (blockNode instanceof JCCase) {
((JCCase) blockNode).stats = newStatements.toList();
} else if (blockNode instanceof JCMethodDecl) {
((JCMethodDecl) blockNode).body.stats = newStatements.toList();
} else
throw new AssertionError("Should not get here");
ancestor.rebuild();
}
use of com.sun.tools.javac.tree.JCTree in project lombok by rzwitserloot.
the class JavacAST method printMessage.
/** Supply either a position or a node (in that case, position of the node is used) */
void printMessage(Diagnostic.Kind kind, String message, JavacNode node, DiagnosticPosition pos, boolean attemptToRemoveErrorsInRange) {
JavaFileObject oldSource = null;
JavaFileObject newSource = null;
JCTree astObject = node == null ? null : node.get();
JCCompilationUnit top = (JCCompilationUnit) top().get();
newSource = top.sourcefile;
if (newSource != null) {
oldSource = log.useSource(newSource);
if (pos == null)
pos = astObject.pos();
}
if (pos != null && attemptToRemoveErrorsInRange) {
removeFromDeferredDiagnostics(pos.getStartPosition(), node.getEndPosition(pos));
}
try {
switch(kind) {
case ERROR:
increaseErrorCount(messager);
boolean prev = log.multipleErrors;
log.multipleErrors = true;
try {
log.error(pos, "proc.messager", message);
} finally {
log.multipleErrors = prev;
}
break;
default:
case WARNING:
log.warning(pos, "proc.messager", message);
break;
}
} finally {
if (oldSource != null)
log.useSource(oldSource);
}
}
Aggregations