use of com.sun.tools.javac.tree.JCTree.JCIdent in project error-prone by google.
the class AbstractReturnValueIgnored method describe.
/**
* Fixes the error by assigning the result of the call to the receiver reference, or deleting the
* method call.
*/
public Description describe(MethodInvocationTree methodInvocationTree, VisitorState state) {
// Find the root of the field access chain, i.e. a.intern().trim() ==> a.
ExpressionTree identifierExpr = ASTHelpers.getRootAssignable(methodInvocationTree);
String identifierStr = null;
Type identifierType = null;
if (identifierExpr != null) {
identifierStr = identifierExpr.toString();
if (identifierExpr instanceof JCIdent) {
identifierType = ((JCIdent) identifierExpr).sym.type;
} else if (identifierExpr instanceof JCFieldAccess) {
identifierType = ((JCFieldAccess) identifierExpr).sym.type;
} else {
throw new IllegalStateException("Expected a JCIdent or a JCFieldAccess");
}
}
Type returnType = ASTHelpers.getReturnType(((JCMethodInvocation) methodInvocationTree).getMethodSelect());
Fix fix;
if (identifierStr != null && !"this".equals(identifierStr) && returnType != null && state.getTypes().isAssignable(returnType, identifierType)) {
// Fix by assigning the assigning the result of the call to the root receiver reference.
fix = SuggestedFix.prefixWith(methodInvocationTree, identifierStr + " = ");
} else {
// Unclear what the programmer intended. Delete since we don't know what else to do.
Tree parent = state.getPath().getParentPath().getLeaf();
fix = SuggestedFix.delete(parent);
}
return describeMatch(methodInvocationTree, fix);
}
use of com.sun.tools.javac.tree.JCTree.JCIdent in project error-prone by google.
the class Matchers method receiverSameAsArgument.
/**
* Matches when the receiver of an instance method is the same reference as a particular argument
* to the method. For example, receiverSameAsArgument(1) would match {@code obj.method("", obj)}
*
* @param argNum The number of the argument to compare against (zero-based.
*/
public static Matcher<? super MethodInvocationTree> receiverSameAsArgument(final int argNum) {
return new Matcher<MethodInvocationTree>() {
@Override
public boolean matches(MethodInvocationTree t, VisitorState state) {
List<? extends ExpressionTree> args = t.getArguments();
if (args.size() <= argNum) {
return false;
}
ExpressionTree arg = args.get(argNum);
JCExpression methodSelect = (JCExpression) t.getMethodSelect();
if (methodSelect instanceof JCFieldAccess) {
JCFieldAccess fieldAccess = (JCFieldAccess) methodSelect;
return ASTHelpers.sameVariable(fieldAccess.getExpression(), arg);
} else if (methodSelect instanceof JCIdent) {
// A bare method call: "equals(foo)". Receiver is implicitly "this".
return "this".equals(arg.toString());
}
return false;
}
};
}
use of com.sun.tools.javac.tree.JCTree.JCIdent in project ceylon-compiler by ceylon.
the class JavaPositionsRetriever method getJavaSourceCodeWithCeylonPositions.
public String getJavaSourceCodeWithCeylonPositions() {
final CharArrayWriter writer = new CharArrayWriter();
Pretty printer = new Pretty(writer, true) {
int previousCeylonPosition = -1;
int previousPositionInString = 0;
private void outputCeylonPosition(JCTree tree) {
try {
int currentCeylonPosition = tree.getPreferredPosition();
int currentPositionInString = writer.size();
if (previousCeylonPosition != currentCeylonPosition || previousPositionInString != currentPositionInString) {
if (currentCeylonPosition != -1 && currentCeylonPosition != 0) {
writer.write("/* " + formatCeylonPosition(currentCeylonPosition) + " */");
}
previousCeylonPosition = currentCeylonPosition;
previousPositionInString = writer.size();
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void visitTopLevel(JCCompilationUnit tree) {
outputCeylonPosition(tree);
super.visitTopLevel(tree);
}
@Override
public void visitImport(JCImport tree) {
outputCeylonPosition(tree);
super.visitImport(tree);
}
@Override
public void visitClassDef(JCClassDecl tree) {
outputCeylonPosition(tree);
super.visitClassDef(tree);
}
@Override
public void visitMethodDef(JCMethodDecl tree) {
outputCeylonPosition(tree);
super.visitMethodDef(tree);
}
@Override
public void visitVarDef(JCVariableDecl tree) {
outputCeylonPosition(tree);
super.visitVarDef(tree);
}
@Override
public void visitSkip(JCSkip tree) {
outputCeylonPosition(tree);
super.visitSkip(tree);
}
@Override
public void visitBlock(JCBlock tree) {
outputCeylonPosition(tree);
super.visitBlock(tree);
tree.endpos = currentPosition - 1;
}
@Override
public void visitDoLoop(JCDoWhileLoop tree) {
outputCeylonPosition(tree);
super.visitDoLoop(tree);
}
@Override
public void visitWhileLoop(JCWhileLoop tree) {
outputCeylonPosition(tree);
super.visitWhileLoop(tree);
}
@Override
public void visitForLoop(JCForLoop tree) {
outputCeylonPosition(tree);
super.visitForLoop(tree);
}
@Override
public void visitForeachLoop(JCEnhancedForLoop tree) {
outputCeylonPosition(tree);
super.visitForeachLoop(tree);
}
@Override
public void visitLabelled(JCLabeledStatement tree) {
outputCeylonPosition(tree);
super.visitLabelled(tree);
}
@Override
public void visitSwitch(JCSwitch tree) {
outputCeylonPosition(tree);
super.visitSwitch(tree);
}
@Override
public void visitCase(JCCase tree) {
outputCeylonPosition(tree);
super.visitCase(tree);
}
@Override
public void visitSynchronized(JCSynchronized tree) {
outputCeylonPosition(tree);
super.visitSynchronized(tree);
}
@Override
public void visitTry(JCTry tree) {
outputCeylonPosition(tree);
super.visitTry(tree);
}
@Override
public void visitCatch(JCCatch tree) {
outputCeylonPosition(tree);
super.visitCatch(tree);
}
@Override
public void visitConditional(JCConditional tree) {
outputCeylonPosition(tree);
super.visitConditional(tree);
}
@Override
public void visitIf(JCIf tree) {
outputCeylonPosition(tree);
super.visitIf(tree);
}
@Override
public void visitExec(JCExpressionStatement tree) {
outputCeylonPosition(tree);
super.visitExec(tree);
}
@Override
public void visitBreak(JCBreak tree) {
outputCeylonPosition(tree);
super.visitBreak(tree);
}
@Override
public void visitContinue(JCContinue tree) {
outputCeylonPosition(tree);
super.visitContinue(tree);
}
@Override
public void visitReturn(JCReturn tree) {
outputCeylonPosition(tree);
super.visitReturn(tree);
}
@Override
public void visitThrow(JCThrow tree) {
outputCeylonPosition(tree);
super.visitThrow(tree);
}
@Override
public void visitAssert(JCAssert tree) {
outputCeylonPosition(tree);
super.visitAssert(tree);
}
@Override
public void visitApply(JCMethodInvocation tree) {
outputCeylonPosition(tree);
super.visitApply(tree);
}
@Override
public void visitNewClass(JCNewClass tree) {
outputCeylonPosition(tree);
super.visitNewClass(tree);
}
@Override
public void visitNewArray(JCNewArray tree) {
outputCeylonPosition(tree);
super.visitNewArray(tree);
}
@Override
public void visitParens(JCParens tree) {
outputCeylonPosition(tree);
super.visitParens(tree);
}
@Override
public void visitAssign(JCAssign tree) {
outputCeylonPosition(tree);
super.visitAssign(tree);
}
@Override
public void visitAssignop(JCAssignOp tree) {
outputCeylonPosition(tree);
super.visitAssignop(tree);
}
@Override
public void visitUnary(JCUnary tree) {
outputCeylonPosition(tree);
super.visitUnary(tree);
}
@Override
public void visitBinary(JCBinary tree) {
outputCeylonPosition(tree);
super.visitBinary(tree);
}
@Override
public void visitTypeCast(JCTypeCast tree) {
outputCeylonPosition(tree);
super.visitTypeCast(tree);
}
@Override
public void visitTypeTest(JCInstanceOf tree) {
outputCeylonPosition(tree);
super.visitTypeTest(tree);
}
@Override
public void visitIndexed(JCArrayAccess tree) {
outputCeylonPosition(tree);
super.visitIndexed(tree);
}
@Override
public void visitSelect(JCFieldAccess tree) {
outputCeylonPosition(tree);
super.visitSelect(tree);
}
@Override
public void visitIdent(JCIdent tree) {
outputCeylonPosition(tree);
super.visitIdent(tree);
}
@Override
public void visitLiteral(JCLiteral tree) {
outputCeylonPosition(tree);
super.visitLiteral(tree);
}
@Override
public void visitTypeIdent(JCPrimitiveTypeTree tree) {
outputCeylonPosition(tree);
super.visitTypeIdent(tree);
}
@Override
public void visitTypeArray(JCArrayTypeTree tree) {
outputCeylonPosition(tree);
super.visitTypeArray(tree);
}
@Override
public void visitTypeApply(JCTypeApply tree) {
outputCeylonPosition(tree);
super.visitTypeApply(tree);
}
@Override
public void visitTypeParameter(JCTypeParameter tree) {
outputCeylonPosition(tree);
super.visitTypeParameter(tree);
}
@Override
public void visitWildcard(JCWildcard tree) {
outputCeylonPosition(tree);
super.visitWildcard(tree);
}
@Override
public void visitTypeBoundKind(TypeBoundKind tree) {
outputCeylonPosition(tree);
super.visitTypeBoundKind(tree);
}
@Override
public void visitErroneous(JCErroneous tree) {
outputCeylonPosition(tree);
super.visitErroneous(tree);
}
@Override
public void visitLetExpr(LetExpr tree) {
outputCeylonPosition(tree);
super.visitLetExpr(tree);
}
@Override
public void visitModifiers(JCModifiers mods) {
outputCeylonPosition(mods);
super.visitModifiers(mods);
}
@Override
public void visitAnnotation(JCAnnotation tree) {
outputCeylonPosition(tree);
super.visitAnnotation(tree);
}
@Override
public void visitTree(JCTree tree) {
outputCeylonPosition(tree);
super.visitTree(tree);
}
};
printer.visitTopLevel(unit);
return writer.toString();
}
use of com.sun.tools.javac.tree.JCTree.JCIdent in project ceylon-compiler by ceylon.
the class Attr method visitNewClass.
public void visitNewClass(JCNewClass tree) {
Type owntype = types.createErrorType(tree.type);
// The local environment of a class creation is
// a new environment nested in the current one.
Env<AttrContext> localEnv = env.dup(tree, env.info.dup());
// The anonymous inner class definition of the new expression,
// if one is defined by it.
JCClassDecl cdef = tree.def;
// If enclosing class is given, attribute it, and
// complete class name to be fully qualified
// Class field following new
JCExpression clazz = tree.clazz;
// Identifier in class field
JCExpression clazzid = (clazz.getTag() == JCTree.TYPEAPPLY) ? ((JCTypeApply) clazz).clazz : clazz;
// The same in fully qualified form
JCExpression clazzid1 = clazzid;
if (tree.encl != null) {
// We are seeing a qualified new, of the form
// <expr>.new C <...> (...) ...
// In this case, we let clazz stand for the name of the
// allocated class C prefixed with the type of the qualifier
// expression, so that we can
// resolve it with standard techniques later. I.e., if
// <expr> has type T, then <expr>.new C <...> (...)
// yields a clazz T.C.
Type encltype = chk.checkRefType(tree.encl.pos(), attribExpr(tree.encl, env));
clazzid1 = make.at(clazz.pos).Select(make.Type(encltype), ((JCIdent) clazzid).name);
if (clazz.getTag() == JCTree.TYPEAPPLY)
clazz = make.at(tree.pos).TypeApply(clazzid1, ((JCTypeApply) clazz).arguments);
else
clazz = clazzid1;
}
// Attribute clazz expression and store
// symbol + type back into the attributed tree.
Type clazztype = attribType(clazz, env);
Pair<Scope, Scope> mapping = getSyntheticScopeMapping(clazztype);
clazztype = chk.checkDiamond(tree, clazztype);
chk.validate(clazz, localEnv);
if (tree.encl != null) {
// We have to work in this case to store
// symbol + type back into the attributed tree.
tree.clazz.type = clazztype;
TreeInfo.setSymbol(clazzid, TreeInfo.symbol(clazzid1));
clazzid.type = ((JCIdent) clazzid).sym.type;
if (!clazztype.isErroneous()) {
if (cdef != null && clazztype.tsym.isInterface()) {
log.error(tree.encl.pos(), "anon.class.impl.intf.no.qual.for.new");
} else if (clazztype.tsym.isStatic()) {
log.error(tree.encl.pos(), "qualified.new.of.static.class", clazztype.tsym);
}
}
} else if (!clazztype.tsym.isInterface() && clazztype.getEnclosingType().tag == CLASS) {
// Check for the existence of an apropos outer instance
rs.resolveImplicitThis(tree.pos(), env, clazztype);
}
// Attribute constructor arguments.
List<Type> argtypes = attribArgs(tree.args, localEnv);
List<Type> typeargtypes = attribTypes(tree.typeargs, localEnv);
if (TreeInfo.isDiamond(tree) && !clazztype.isErroneous()) {
clazztype = attribDiamond(localEnv, tree, clazztype, mapping, argtypes, typeargtypes);
clazz.type = clazztype;
} else if (allowDiamondFinder && tree.def == null && !clazztype.isErroneous() && clazztype.getTypeArguments().nonEmpty() && findDiamonds) {
boolean prevDeferDiags = log.deferDiagnostics;
Queue<JCDiagnostic> prevDeferredDiags = log.deferredDiagnostics;
Type inferred = null;
try {
// disable diamond-related diagnostics
log.deferDiagnostics = true;
log.deferredDiagnostics = ListBuffer.lb();
inferred = attribDiamond(localEnv, tree, clazztype, mapping, argtypes, typeargtypes);
} finally {
log.deferDiagnostics = prevDeferDiags;
log.deferredDiagnostics = prevDeferredDiags;
}
if (inferred != null && !inferred.isErroneous() && inferred.tag == CLASS && types.isAssignable(inferred, pt.tag == NONE ? clazztype : pt, Warner.noWarnings)) {
String key = types.isSameType(clazztype, inferred) ? "diamond.redundant.args" : "diamond.redundant.args.1";
log.warning(tree.clazz.pos(), key, clazztype, inferred);
}
}
// If we have made no mistakes in the class type...
if (clazztype.tag == CLASS) {
// Enums may not be instantiated except implicitly
if (allowEnums && (clazztype.tsym.flags_field & Flags.ENUM) != 0 && (env.tree.getTag() != JCTree.VARDEF || (((JCVariableDecl) env.tree).mods.flags & Flags.ENUM) == 0 || ((JCVariableDecl) env.tree).init != tree))
log.error(tree.pos(), "enum.cant.be.instantiated");
// Check that class is not abstract
if (cdef == null && (clazztype.tsym.flags() & (ABSTRACT | INTERFACE)) != 0) {
log.error(tree.pos(), "abstract.cant.be.instantiated", clazztype.tsym);
} else if (cdef != null && clazztype.tsym.isInterface()) {
// anonymous classes implementing an interface
if (!argtypes.isEmpty())
log.error(tree.args.head.pos(), "anon.class.impl.intf.no.args");
if (!typeargtypes.isEmpty())
log.error(tree.typeargs.head.pos(), "anon.class.impl.intf.no.typeargs");
// Error recovery: pretend no arguments were supplied.
argtypes = List.nil();
typeargtypes = List.nil();
} else // Resolve the called constructor under the assumption
// that we are referring to a superclass instance of the
// current instance (JLS ???).
{
// the following code alters some of the fields in the current
// AttrContext - hence, the current context must be dup'ed in
// order to avoid downstream failures
Env<AttrContext> rsEnv = localEnv.dup(tree);
rsEnv.info.selectSuper = cdef != null;
rsEnv.info.varArgs = false;
tree.constructor = rs.resolveConstructor(tree.pos(), rsEnv, clazztype, argtypes, typeargtypes);
tree.constructorType = tree.constructor.type.isErroneous() ? syms.errType : checkMethod(clazztype, tree.constructor, rsEnv, tree.args, argtypes, typeargtypes, rsEnv.info.varArgs);
if (rsEnv.info.varArgs)
Assert.check(tree.constructorType.isErroneous() || tree.varargsElement != null);
}
if (cdef != null) {
// }
if (Resolve.isStatic(env))
cdef.mods.flags |= STATIC;
if (clazztype.tsym.isInterface()) {
cdef.implementing = List.of(clazz);
} else {
cdef.extending = clazz;
}
attribStat(cdef, localEnv);
// and delete it from the new expression
if (tree.encl != null && !clazztype.tsym.isInterface()) {
tree.args = tree.args.prepend(makeNullCheck(tree.encl));
argtypes = argtypes.prepend(tree.encl.type);
tree.encl = null;
}
// Reassign clazztype and recompute constructor.
clazztype = cdef.sym.type;
boolean useVarargs = tree.varargsElement != null;
Symbol sym = rs.resolveConstructor(tree.pos(), localEnv, clazztype, argtypes, typeargtypes, true, useVarargs);
Assert.check(sym.kind < AMBIGUOUS || tree.constructor.type.isErroneous());
tree.constructor = sym;
if (tree.constructor.kind > ERRONEOUS) {
tree.constructorType = syms.errType;
} else {
tree.constructorType = checkMethod(clazztype, tree.constructor, localEnv, tree.args, argtypes, typeargtypes, useVarargs);
}
}
if (tree.constructor != null && tree.constructor.kind == MTH)
owntype = clazztype;
}
result = check(tree, owntype, VAL, pkind, pt);
chk.validate(tree.typeargs, localEnv);
}
use of com.sun.tools.javac.tree.JCTree.JCIdent in project ceylon-compiler by ceylon.
the class Attr method enumConstant.
// where
/**
* Return the selected enumeration constant symbol, or null.
*/
private Symbol enumConstant(JCTree tree, Type enumType) {
if (tree.getTag() != JCTree.IDENT) {
log.error(tree.pos(), "enum.label.must.be.unqualified.enum");
return syms.errSymbol;
}
JCIdent ident = (JCIdent) tree;
Name name = ident.name;
for (Scope.Entry e = enumType.tsym.members().lookup(name); e.scope != null; e = e.next()) {
if (e.sym.kind == VAR) {
Symbol s = ident.sym = e.sym;
// ensure initializer is evaluated
((VarSymbol) s).getConstValue();
ident.type = s.type;
return ((s.flags_field & Flags.ENUM) == 0) ? null : s;
}
}
return null;
}
Aggregations