use of com.sun.tools.javac.tree.JCTree.JCExpressionStatement in project bazel by bazelbuild.
the class InternalUtils method constructor.
/**
* Determines the symbol for a constructor given an invocation via
* {@code new}.
*
* If the tree is a declaration of an anonymous class, then method returns
* constructor that gets invoked in the extended class, rather than the
* anonymous constructor implicitly added by the constructor (JLS 15.9.5.1)
*
* @param tree the constructor invocation
* @return the {@link ExecutableElement} corresponding to the constructor
* call in {@code tree}
*/
public static ExecutableElement constructor(NewClassTree tree) {
if (!(tree instanceof JCTree.JCNewClass)) {
ErrorReporter.errorAbort("InternalUtils.constructor: not a javac internal tree");
// dead code
return null;
}
JCNewClass newClassTree = (JCNewClass) tree;
if (RETURN_INVOKE_CONSTRUCTOR && tree.getClassBody() != null) {
// anonymous constructor bodies should contain exactly one statement
// in the form:
// super(arg1, ...)
// or
// o.super(arg1, ...)
//
// which is a method invocation (!) to the actual constructor
// the method call is guaranteed to return nonnull
JCMethodDecl anonConstructor = (JCMethodDecl) TreeInfo.declarationFor(newClassTree.constructor, newClassTree);
assert anonConstructor != null;
assert anonConstructor.body.stats.size() == 1;
JCExpressionStatement stmt = (JCExpressionStatement) anonConstructor.body.stats.head;
JCTree.JCMethodInvocation superInvok = (JCMethodInvocation) stmt.expr;
return (ExecutableElement) TreeInfo.symbol(superInvok.meth);
}
Element e = newClassTree.constructor;
assert e instanceof ExecutableElement;
return (ExecutableElement) e;
}
use of com.sun.tools.javac.tree.JCTree.JCExpressionStatement in project error-prone by google.
the class Template method pretty.
protected static Pretty pretty(Context context, final Writer writer) {
final JCCompilationUnit unit = context.get(JCCompilationUnit.class);
try {
final String unitContents = unit.getSourceFile().getCharContent(false).toString();
return new Pretty(writer, true) {
{
// Work-around for b/22196513
width = 0;
}
@Override
public void visitAnnotation(JCAnnotation anno) {
if (anno.getArguments().isEmpty()) {
try {
print("@");
printExpr(anno.annotationType);
} catch (IOException e) {
// the supertype swallows exceptions too
throw new RuntimeException(e);
}
} else {
super.visitAnnotation(anno);
}
}
@Override
public void printExpr(JCTree tree, int prec) throws IOException {
EndPosTable endPositions = unit.endPositions;
/*
* Modifiers, and specifically flags like final, appear to just need weird special
* handling.
*
* Note: we can't use {@code TreeInfo.getEndPos()} or {@code JCTree.getEndPosition()}
* here, because they will return the end position of an enclosing AST node for trees
* whose real end positions aren't stored.
*/
int endPos = endPositions.getEndPos(tree);
boolean hasRealEndPosition = endPos != Position.NOPOS;
if (tree.getKind() != Kind.MODIFIERS && hasRealEndPosition) {
writer.append(unitContents.substring(tree.getStartPosition(), endPos));
} else {
super.printExpr(tree, prec);
}
}
@Override
public void visitApply(JCMethodInvocation tree) {
JCExpression select = tree.getMethodSelect();
if (select != null && select.toString().equals("Refaster.emitCommentBefore")) {
String commentLiteral = (String) ((JCLiteral) tree.getArguments().get(0)).getValue();
JCExpression expr = tree.getArguments().get(1);
try {
print("/* " + commentLiteral + " */ ");
} catch (IOException e) {
throw new RuntimeException(e);
}
expr.accept(this);
} else {
super.visitApply(tree);
}
}
@Override
public void printStat(JCTree tree) throws IOException {
if (tree instanceof JCExpressionStatement && ((JCExpressionStatement) tree).getExpression() instanceof JCMethodInvocation) {
JCMethodInvocation invocation = (JCMethodInvocation) ((JCExpressionStatement) tree).getExpression();
JCExpression select = invocation.getMethodSelect();
if (select != null && select.toString().equals("Refaster.emitComment")) {
String commentLiteral = (String) ((JCLiteral) invocation.getArguments().get(0)).getValue();
print("// " + commentLiteral);
return;
}
}
super.printStat(tree);
}
@Override
public void visitTry(JCTry tree) {
if (tree.getResources().isEmpty()) {
super.visitTry(tree);
return;
}
try {
print("try (");
boolean first = true;
for (JCTree resource : tree.getResources()) {
if (!first) {
print(";");
println();
}
printExpr(resource);
first = false;
}
print(")");
printStat(tree.body);
for (JCCatch catchStmt : tree.getCatches()) {
printStat(catchStmt);
}
if (tree.getFinallyBlock() != null) {
print(" finally ");
printStat(tree.getFinallyBlock());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of com.sun.tools.javac.tree.JCTree.JCExpressionStatement in project lombok by rzwitserloot.
the class PrettyPrinter method visitForLoop.
@Override
public void visitForLoop(JCForLoop tree) {
aPrint("for (");
if (tree.init.nonEmpty()) {
// ForInit is either a StatementExpressionList or a LocalVariableDeclaration
if (tree.init.head instanceof JCVariableDecl) {
boolean first = true;
int dims = 0;
for (JCStatement i : tree.init) {
JCVariableDecl vd = (JCVariableDecl) i;
if (first) {
printVarDefInline(vd);
dims = dims(vd.vartype);
} else {
print(", ");
print(vd.name);
int dimDiff = dims(vd.vartype) - dims;
for (int j = 0; j < dimDiff; j++) print("[]");
if (vd.init != null) {
print(" = ");
print(vd.init);
}
}
first = false;
}
} else {
boolean first = true;
for (JCStatement exprStatement : tree.init) {
if (!first)
print(", ");
first = false;
print(((JCExpressionStatement) exprStatement).expr);
}
}
}
print("; ");
if (tree.cond != null)
print(tree.cond);
print("; ");
boolean first = true;
for (JCExpressionStatement exprStatement : tree.step) {
if (!first)
print(", ");
first = false;
print(exprStatement.expr);
}
print(") ");
print(tree.body);
}
use of com.sun.tools.javac.tree.JCTree.JCExpressionStatement in project lombok by rzwitserloot.
the class JavacHandlerUtil method isConstructorCall.
public static boolean isConstructorCall(final JCStatement statement) {
if (!(statement instanceof JCExpressionStatement))
return false;
JCExpression expr = ((JCExpressionStatement) statement).expr;
if (!(expr instanceof JCMethodInvocation))
return false;
JCExpression invocation = ((JCMethodInvocation) expr).meth;
String name;
if (invocation instanceof JCFieldAccess) {
name = ((JCFieldAccess) invocation).name.toString();
} else if (invocation instanceof JCIdent) {
name = ((JCIdent) invocation).name.toString();
} else {
name = "";
}
return "super".equals(name) || "this".equals(name);
}
use of com.sun.tools.javac.tree.JCTree.JCExpressionStatement in project ceylon-compiler by ceylon.
the class Attr method checkFirstConstructorStat.
//where
/** Check that given application node appears as first statement
* in a constructor call.
* @param tree The application node
* @param env The environment current at the application.
*/
boolean checkFirstConstructorStat(JCMethodInvocation tree, Env<AttrContext> env) {
JCMethodDecl enclMethod = env.enclMethod;
if (enclMethod != null && enclMethod.name == names.init) {
JCBlock body = enclMethod.body;
if (body.stats.head.getTag() == JCTree.EXEC && ((JCExpressionStatement) body.stats.head).expr == tree)
return true;
// given tree allowed as last stmt in a Let
if (body.stats.head.getTag() == JCTree.EXEC && (((JCExpressionStatement) body.stats.head).expr instanceof LetExpr) && ((LetExpr) ((JCExpressionStatement) body.stats.head).expr).stats.last().getTag() == JCTree.EXEC && ((JCExpressionStatement) ((LetExpr) ((JCExpressionStatement) body.stats.head).expr).stats.last()).expr == tree)
return true;
}
log.error(tree.pos(), "call.must.be.first.stmt.in.ctor", TreeInfo.name(tree.meth));
return false;
}
Aggregations