use of com.sun.source.tree.LiteralTree in project checker-framework by typetools.
the class InterningVisitor method suppressEarlyEquals.
/**
* Pattern matches to prevent false positives of the forms:
*
* <pre>
* (a == b) || a.equals(b)
* (a == b) || (a != null ? a.equals(b) : false)
* (a == b) || (a != null && a.equals(b))
* </pre>
*
* Returns true iff the given node fits this pattern.
*
* @return true iff the node fits a pattern such as (a == b || a.equals(b))
*/
private boolean suppressEarlyEquals(final BinaryTree node) {
// Only handle == binary trees
if (node.getKind() != Tree.Kind.EQUAL_TO) {
return false;
}
// should strip parens
final ExpressionTree left = TreeUtils.skipParens(node.getLeftOperand());
final ExpressionTree right = TreeUtils.skipParens(node.getRightOperand());
// looking for ((a == b || a.equals(b))
Heuristics.Matcher matcherEqOrEquals = new Heuristics.Matcher() {
/**
* Returns true if e is either "e1 != null" or "e2 != null".
*/
private boolean isNeqNull(ExpressionTree e, ExpressionTree e1, ExpressionTree e2) {
e = TreeUtils.skipParens(e);
if (e.getKind() != Tree.Kind.NOT_EQUAL_TO) {
return false;
}
ExpressionTree neqLeft = ((BinaryTree) e).getLeftOperand();
ExpressionTree neqRight = ((BinaryTree) e).getRightOperand();
return (((sameTree(neqLeft, e1) || sameTree(neqLeft, e2)) && neqRight.getKind() == Tree.Kind.NULL_LITERAL) || // also check for "null != e1" and "null != e2"
((sameTree(neqRight, e1) || sameTree(neqRight, e2)) && neqLeft.getKind() == Tree.Kind.NULL_LITERAL));
}
@Override
public Boolean visitBinary(BinaryTree tree, Void p) {
ExpressionTree leftTree = tree.getLeftOperand();
ExpressionTree rightTree = tree.getRightOperand();
if (tree.getKind() == Tree.Kind.CONDITIONAL_OR) {
if (sameTree(leftTree, node)) {
// similar
return visit(rightTree, p);
} else {
return false;
}
}
if (tree.getKind() == Tree.Kind.CONDITIONAL_AND) {
// looking for: (a != null && a.equals(b)))
if (isNeqNull(leftTree, left, right)) {
return visit(rightTree, p);
}
return false;
}
return false;
}
@Override
public Boolean visitConditionalExpression(ConditionalExpressionTree tree, Void p) {
// looking for: (a != null ? a.equals(b) : false)
ExpressionTree cond = tree.getCondition();
ExpressionTree trueExp = tree.getTrueExpression();
ExpressionTree falseExp = tree.getFalseExpression();
if (isNeqNull(cond, left, right) && (falseExp.getKind() == Tree.Kind.BOOLEAN_LITERAL) && ((LiteralTree) falseExp).getValue().equals(false)) {
return visit(trueExp, p);
}
return false;
}
@Override
public Boolean visitMethodInvocation(MethodInvocationTree tree, Void p) {
if (!isInvocationOfEquals(tree)) {
return false;
}
List<? extends ExpressionTree> args = tree.getArguments();
if (args.size() != 1) {
return false;
}
ExpressionTree arg = args.get(0);
// if (arg.getKind() != Tree.Kind.IDENTIFIER) {
// return false;
// }
// Element argElt = TreeUtils.elementFromUse((IdentifierTree) arg);
ExpressionTree exp = tree.getMethodSelect();
if (exp.getKind() != Tree.Kind.MEMBER_SELECT) {
return false;
}
MemberSelectTree member = (MemberSelectTree) exp;
ExpressionTree receiver = member.getExpression();
if (sameTree(receiver, left) && sameTree(arg, right)) {
return true;
}
if (sameTree(receiver, right) && sameTree(arg, left)) {
return true;
}
return false;
}
};
boolean okay = Heuristics.Matchers.withIn(Heuristics.Matchers.ofKind(Tree.Kind.CONDITIONAL_OR, matcherEqOrEquals)).match(getCurrentPath());
return okay;
}
use of com.sun.source.tree.LiteralTree in project checker-framework by typetools.
the class SignednessVisitor method isCastedShift.
/**
* Determines if a right shift operation, {@code >>} or {@code >>>}, is type casted such that
* the cast renders the shift signedness ({@code >>} vs {@code >>>}) irrelevent by discarding
* the bits duplicated into the shift result. For example, the following pair of right shifts on
* {@code short s} both produce the same results under any input, because of type casting:
*
* <p>{@code (byte)(s >> 8) == (byte)(b >>> 8);}
*
* @param shiftExpr a right shift expression: {@code expr1 >> expr2} or {@code expr1 >>> expr2}
* @return true iff the right shift is type casted such that a signed or unsigned right shift
* has the same effect
*/
private boolean isCastedShift(BinaryTree shiftExpr) {
// enclosing is the operation or statement that immediately contains shiftExpr
Tree enclosing;
{
TreePath parentPath = visitorState.getPath().getParentPath();
enclosing = parentPath.getLeaf();
// Strip away all parentheses from the shift operation
while (enclosing.getKind() == Kind.PARENTHESIZED) {
parentPath = parentPath.getParentPath();
enclosing = parentPath.getLeaf();
}
}
PrimitiveTypeTree castPrimitiveType = primitiveTypeCast(enclosing);
if (castPrimitiveType == null) {
return false;
}
TypeKind castTypeKind = castPrimitiveType.getPrimitiveTypeKind();
// Determine the type of the shift result
TypeKind shiftTypeKind = atypeFactory.getAnnotatedType(shiftExpr).getUnderlyingType().getKind();
// Determine shift literal
ExpressionTree shiftAmountExpr = shiftExpr.getRightOperand();
if (!isLiteral(shiftAmountExpr)) {
return false;
}
LiteralTree shiftLit = (LiteralTree) shiftAmountExpr;
return castIgnoresMSB(shiftTypeKind, castTypeKind, shiftLit);
}
use of com.sun.source.tree.LiteralTree in project error-prone by google.
the class StringLiteralTest method notMatches.
@Test
public void notMatches() {
// TODO(b/67738557): consolidate helpers for creating fake trees
LiteralTree tree = new LiteralTree() {
@Override
public Kind getKind() {
throw new UnsupportedOperationException();
}
@Override
public <R, D> R accept(TreeVisitor<R, D> visitor, D data) {
throw new UnsupportedOperationException();
}
@Override
public Object getValue() {
return "a string literal";
}
};
assertFalse(new StringLiteral("different string").matches(tree, null));
IdentifierTree idTree = new IdentifierTree() {
@Override
public Name getName() {
return null;
}
@Override
public Kind getKind() {
throw new UnsupportedOperationException();
}
@Override
public <R, D> R accept(TreeVisitor<R, D> visitor, D data) {
throw new UnsupportedOperationException();
}
};
assertFalse(new StringLiteral("test").matches(idTree, null));
// TODO(b/67738557): consolidate helpers for creating fake trees
LiteralTree intTree = new LiteralTree() {
@Override
public Object getValue() {
return 5;
}
@Override
public Kind getKind() {
throw new UnsupportedOperationException();
}
@Override
public <R, D> R accept(TreeVisitor<R, D> visitor, D data) {
throw new UnsupportedOperationException();
}
};
assertFalse(new StringLiteral("test").matches(intTree, null));
}
use of com.sun.source.tree.LiteralTree in project error-prone by google.
the class OrphanedFormatString method matchLiteral.
@Override
public Description matchLiteral(LiteralTree tree, VisitorState state) {
Object value = tree.getValue();
if (!(value instanceof String)) {
return NO_MATCH;
}
if (!missingFormatArgs((String) value)) {
return NO_MATCH;
}
Tree parent = state.getPath().getParentPath().getLeaf();
if (!MATCHER.matches(parent, state)) {
return NO_MATCH;
}
return describeMatch(tree);
}
use of com.sun.source.tree.LiteralTree in project groovy-cps by cloudbees.
the class Translator method translateMethod.
/**
* @param e
* Method in {@code fqcn} to translate.
*/
private void translateMethod(final CompilationUnitTree cut, ExecutableElement e, JDefinedClass $output, String fqcn, String overloadResolved) {
String methodName = n(e);
boolean isPublic = e.getModifiers().contains(Modifier.PUBLIC);
JMethod delegating = $output.method(isPublic ? JMod.PUBLIC | JMod.STATIC : JMod.STATIC, (JType) null, methodName);
JMethod m = $output.method(JMod.PRIVATE | JMod.STATIC, (JType) null, overloadResolved);
Map<String, JTypeVar> typeVars = new HashMap<>();
e.getTypeParameters().forEach(p -> {
String name = n(p);
JTypeVar typeVar = delegating.generify(name);
JTypeVar typeVar2 = m.generify(name);
p.getBounds().forEach(b -> {
JClass binding = (JClass) t(b, typeVars);
typeVar.bound(binding);
typeVar2.bound(binding);
});
typeVars.put(name, typeVar);
});
JType type = t(e.getReturnType(), typeVars);
delegating.type(type);
m.type(type);
List<JVar> delegatingParams = new ArrayList<>();
List<JVar> params = new ArrayList<>();
e.getParameters().forEach(p -> {
JType paramType = t(p.asType(), typeVars);
delegatingParams.add(e.isVarArgs() && p == e.getParameters().get(e.getParameters().size() - 1) ? delegating.varParam(paramType.elementType(), n(p)) : delegating.param(paramType, n(p)));
params.add(m.param(paramType, n(p)));
});
e.getThrownTypes().forEach(ex -> {
delegating._throws((JClass) t(ex));
m._throws((JClass) t(ex));
});
boolean returnsVoid = e.getReturnType().getKind() == TypeKind.VOID;
if (isPublic) {
// preamble
/*
If the call to this method happen outside CPS code, execute normally via DefaultGroovyMethods
*/
delegating.body()._if(JOp.cand(JOp.not($Caller.staticInvoke("isAsynchronous").tap(inv -> {
inv.arg(delegatingParams.get(0));
inv.arg(methodName);
for (int i = 1; i < delegatingParams.size(); i++) inv.arg(delegatingParams.get(i));
})), JOp.not($Caller.staticInvoke("isAsynchronous").arg($output.dotclass()).arg(methodName).args(params))))._then().tap(blk -> {
JClass $WhateverGroovyMethods = codeModel.ref(fqcn);
JInvocation forward = $WhateverGroovyMethods.staticInvoke(methodName).args(delegatingParams);
if (returnsVoid) {
blk.add(forward);
blk._return();
} else {
blk._return(forward);
}
});
}
JInvocation delegateCall = $output.staticInvoke(overloadResolved);
if (returnsVoid) {
delegating.body().add(delegateCall);
} else {
delegating.body()._return(delegateCall);
}
delegatingParams.forEach(p -> delegateCall.arg(p));
JVar $b = m.body().decl($Builder, "b", JExpr._new($Builder).arg(JExpr.invoke("loc").arg(methodName)).invoke("contextualize").arg(codeModel.ref("com.cloudbees.groovy.cps.sandbox.Trusted").staticRef("INSTANCE")));
JInvocation f = JExpr._new($CpsFunction);
// parameter names
f.arg(codeModel.ref(Arrays.class).staticInvoke("asList").tap(inv -> {
e.getParameters().forEach(p -> inv.arg(n(p)));
}));
// translate the method body into an expression that invokes Builder
f.arg(trees.getTree(e).getBody().accept(new SimpleTreeVisitor<JExpression, Void>() {
private JExpression visit(Tree t) {
if (t == null)
return JExpr._null();
return visit(t, null);
}
/**
* Maps a symbol to its source location.
*/
private JExpression loc(Tree t) {
long pos = trees.getSourcePositions().getStartPosition(cut, t);
return JExpr.lit((int) cut.getLineMap().getLineNumber(pos));
}
@Override
public JExpression visitWhileLoop(WhileLoopTree wt, Void __) {
return $b.invoke("while_").arg(// TODO: label
JExpr._null()).arg(visit(wt.getCondition())).arg(visit(wt.getStatement()));
}
@Override
public JExpression visitMethodInvocation(MethodInvocationTree mt, Void __) {
ExpressionTree ms = mt.getMethodSelect();
JInvocation inv;
if (ms instanceof MemberSelectTree) {
MemberSelectTree mst = (MemberSelectTree) ms;
// of the other known translated classes.
if (mst.getExpression() instanceof JCIdent && !((JCIdent) mst.getExpression()).sym.toString().equals(fqcn) && otherTranslated.containsKey(((JCIdent) mst.getExpression()).sym.toString())) {
inv = $b.invoke("functionCall").arg(loc(mt)).arg($b.invoke("constant").arg(otherTranslated.get(((JCIdent) mst.getExpression()).sym.toString()).dotclass())).arg(n(mst.getIdentifier()));
} else {
inv = $b.invoke("functionCall").arg(loc(mt)).arg(visit(mst.getExpression())).arg(n(mst.getIdentifier()));
}
} else if (ms instanceof JCIdent) {
// invocation without object selection, like foo(bar,zot)
JCIdent it = (JCIdent) ms;
if (!it.sym.owner.toString().equals(fqcn)) {
if (otherTranslated.containsKey(it.sym.owner.toString())) {
// static import from transformed class
inv = $b.invoke("functionCall").arg(loc(mt)).arg($b.invoke("constant").arg(otherTranslated.get(it.sym.owner.toString()).dotclass())).arg(n(it));
} else {
// static import from non-transformed class
inv = $b.invoke("functionCall").arg(loc(mt)).arg($b.invoke("constant").arg(t(it.sym.owner.type).dotclass())).arg(n(it));
}
} else {
// invocation on this class
String overloadResolved = mangledName((Symbol.MethodSymbol) it.sym);
Optional<? extends Element> callSite = elements.getTypeElement(fqcn).getEnclosedElements().stream().filter(e -> e.getKind() == ElementKind.METHOD && mangledName((ExecutableElement) e).equals(overloadResolved)).findAny();
if (callSite.isPresent()) {
ExecutableElement e = (ExecutableElement) callSite.get();
if (e.getModifiers().contains(Modifier.PUBLIC) && !e.isVarArgs() && !e.getParameters().stream().anyMatch(p -> types.isAssignable(p.asType(), closureType))) {
// Delegate to the standard version.
inv = $b.invoke("staticCall").arg(loc(mt)).arg(t(it.sym.owner.type).dotclass()).arg(n(e));
} else if (overloadsResolved.containsKey(overloadResolved)) {
// Private, so delegate to our mangled version.
// TODO add a String parameter to each internal helper method for the expected methodName to pass to CpsCallableInvocation.<init>
// (It could be improved to take a parameter for the name under which we expect methodCall to be invoking it.
// Usually just `each`, but might be `$each__java_util_Iterable__groovy_lang_Closure` for the case that one DGM method is delegating to another.
// See comment in ContinuationGroup, where we are unable to enforce continuation name mismatches in this case.)
inv = $b.invoke("staticCall").arg(loc(mt)).arg($output.dotclass()).arg(overloadResolved);
} else {
throw new IllegalStateException("Not yet translating a " + e.getModifiers() + " method; translatable.txt might need to include: " + fqcn + "." + e);
}
} else {
throw new IllegalStateException("Could not find self-call site " + overloadResolved + " for " + mt);
}
}
} else {
// TODO: figure out what can come here
throw new UnsupportedOperationException(ms.toString());
}
mt.getArguments().forEach(a -> inv.arg(visit(a)));
return inv;
}
@Override
public JExpression visitVariable(VariableTree vt, Void __) {
return $b.invoke("declareVariable").arg(loc(vt)).arg(cpsTypeTranslation(erasure(vt))).arg(n(vt)).arg(visit(vt.getInitializer()));
}
@Override
public JExpression visitIdentifier(IdentifierTree it, Void __) {
JCIdent idt = (JCIdent) it;
return idt.sym.accept(new DefaultSymbolVisitor<JExpression, Void>() {
@Override
public JExpression visitClassSymbol(ClassSymbol cs, Void __) {
return $b.invoke("constant").arg(t(cs.asType()).dotclass());
}
@Override
public JExpression visitVarSymbol(VarSymbol s, Void __) {
return $b.invoke("localVariable").arg(n(s.name));
}
@Override
public JExpression visitSymbol(Symbol s, Void __) {
throw new UnsupportedOperationException(s.toString());
}
}, __);
}
@Override
public JExpression visitBlock(BlockTree bt, Void __) {
JInvocation inv = $b.invoke("block");
bt.getStatements().forEach(s -> inv.arg(visit(s)));
return inv;
}
@Override
public JExpression visitReturn(ReturnTree rt, Void __) {
return $b.invoke("return_").arg(visit(rt.getExpression()));
}
/**
* When used outside {@link MethodInvocationTree}, this is property access.
*/
@Override
public JExpression visitMemberSelect(MemberSelectTree mt, Void __) {
return $b.invoke("property").arg(loc(mt)).arg(visit(mt.getExpression())).arg(n(mt.getIdentifier()));
}
@Override
public JExpression visitTypeCast(TypeCastTree tt, Void __) {
return $b.invoke("cast").arg(loc(tt)).arg(visit(tt.getExpression())).arg(erasure(tt.getType()).dotclass()).arg(JExpr.lit(false));
}
@Override
public JExpression visitIf(IfTree it, Void __) {
JInvocation inv = $b.invoke("if_").arg(visit(it.getCondition())).arg(visit(it.getThenStatement()));
if (it.getElseStatement() != null)
inv.arg(visit(it.getElseStatement()));
return inv;
}
@Override
public JExpression visitNewClass(NewClassTree nt, Void __) {
// TODO: outer class
if (nt.getEnclosingExpression() != null)
throw new UnsupportedOperationException();
return $b.invoke("new_").tap(inv -> {
inv.arg(loc(nt));
inv.arg(cpsTypeTranslation(t(((JCTree) nt).type)));
nt.getArguments().forEach(et -> inv.arg(visit(et)));
});
}
@Override
public JExpression visitExpressionStatement(ExpressionStatementTree et, Void __) {
return visit(et.getExpression());
}
@Override
public JExpression visitLiteral(LiteralTree lt, Void __) {
return $b.invoke("constant").arg(JExpr.literal(lt.getValue()));
}
@Override
public JExpression visitParenthesized(ParenthesizedTree pt, Void __) {
return visit(pt.getExpression());
}
@Override
public JExpression visitBinary(BinaryTree bt, Void __) {
return $b.invoke(opName(bt.getKind())).arg(loc(bt)).arg(visit(bt.getLeftOperand())).arg(visit(bt.getRightOperand()));
}
@Override
public JExpression visitUnary(UnaryTree ut, Void __) {
return $b.invoke(opName(ut.getKind())).arg(loc(ut)).arg(visit(ut.getExpression()));
}
@Override
public JExpression visitCompoundAssignment(CompoundAssignmentTree ct, Void __) {
return $b.invoke(opName(ct.getKind())).arg(loc(ct)).arg(visit(ct.getVariable())).arg(visit(ct.getExpression()));
}
private String opName(Kind kind) {
switch(kind) {
case EQUAL_TO:
return "compareEqual";
case NOT_EQUAL_TO:
return "compareNotEqual";
case LESS_THAN_EQUAL:
return "lessThanEqual";
case LESS_THAN:
return "lessThan";
case GREATER_THAN_EQUAL:
return "greaterThanEqual";
case GREATER_THAN:
return "greaterThan";
case PREFIX_INCREMENT:
return "prefixInc";
case POSTFIX_INCREMENT:
return "postfixInc";
case POSTFIX_DECREMENT:
return "postfixDec";
case LOGICAL_COMPLEMENT:
return "not";
case CONDITIONAL_OR:
return "logicalOr";
case CONDITIONAL_AND:
return "logicalAnd";
case PLUS:
return "plus";
case PLUS_ASSIGNMENT:
return "plusEqual";
case MINUS:
return "minus";
case MINUS_ASSIGNMENT:
return "minusEqual";
}
throw new UnsupportedOperationException(kind.toString());
}
@Override
public JExpression visitAssignment(AssignmentTree at, Void __) {
return $b.invoke("assign").arg(loc(at)).arg(visit(at.getVariable())).arg(visit(at.getExpression()));
}
/**
* This is needed to handle cases like {@code Object[].class}.
*/
@Override
public JExpression visitArrayType(ArrayTypeTree at, Void __) {
if (at.getType() instanceof IdentifierTree) {
return visitIdentifier((IdentifierTree) at.getType(), __);
} else {
return defaultAction(at, __);
}
}
@Override
public JExpression visitNewArray(NewArrayTree nt, Void __) {
if (nt.getInitializers() != null) {
return $b.invoke("newArrayFromInitializers").tap(inv -> {
nt.getInitializers().forEach(d -> inv.arg(visit(d)));
});
} else {
return $b.invoke("newArray").tap(inv -> {
inv.arg(loc(nt));
inv.arg(t(nt.getType()).dotclass());
nt.getDimensions().forEach(d -> inv.arg(visit(d)));
});
}
}
@Override
public JExpression visitForLoop(ForLoopTree ft, Void __) {
return $b.invoke("forLoop").arg(JExpr._null()).arg($b.invoke("sequence").tap(inv -> ft.getInitializer().forEach(i -> inv.arg(visit(i))))).arg(visit(ft.getCondition())).arg($b.invoke("sequence").tap(inv -> ft.getUpdate().forEach(i -> inv.arg(visit(i))))).arg(visit(ft.getStatement()));
}
@Override
public JExpression visitEnhancedForLoop(EnhancedForLoopTree et, Void __) {
return $b.invoke("forInLoop").arg(loc(et)).arg(JExpr._null()).arg(erasure(et.getVariable()).dotclass()).arg(n(et.getVariable())).arg(visit(et.getExpression())).arg(visit(et.getStatement()));
}
@Override
public JExpression visitArrayAccess(ArrayAccessTree at, Void __) {
return $b.invoke("array").arg(loc(at)).arg(visit(at.getExpression())).arg(visit(at.getIndex()));
}
@Override
public JExpression visitBreak(BreakTree node, Void __) {
if (node.getLabel() != null)
throw new UnsupportedOperationException();
return $b.invoke("break_").arg(JExpr._null());
}
@Override
public JExpression visitContinue(ContinueTree node, Void aVoid) {
if (node.getLabel() != null)
throw new UnsupportedOperationException();
return $b.invoke("continue_").arg(JExpr._null());
}
@Override
public JExpression visitInstanceOf(InstanceOfTree it, Void __) {
return $b.invoke("instanceOf").arg(loc(it)).arg(visit(it.getExpression())).arg($b.invoke("constant").arg(t(it.getType()).dotclass()));
}
@Override
public JExpression visitThrow(ThrowTree tt, Void __) {
return $b.invoke("throw_").arg(loc(tt)).arg(visit(tt.getExpression()));
}
@Override
public JExpression visitDoWhileLoop(DoWhileLoopTree dt, Void __) {
return $b.invoke("doWhile").arg(JExpr._null()).arg(visit(dt.getStatement())).arg(visit(dt.getCondition()));
}
@Override
public JExpression visitConditionalExpression(ConditionalExpressionTree ct, Void __) {
return $b.invoke("ternaryOp").arg(visit(ct.getCondition())).arg(visit(ct.getTrueExpression())).arg(visit(ct.getFalseExpression()));
}
@Override
public JExpression visitTry(TryTree tt, Void __) {
return $b.invoke("tryCatch").arg(visit(tt.getBlock())).arg(visit(tt.getFinallyBlock())).tap(inv -> tt.getCatches().forEach(ct -> JExpr._new($CatchExpression).arg(t(ct.getParameter()).dotclass()).arg(n(ct.getParameter())).arg(visit(ct.getBlock()))));
}
@Override
protected JExpression defaultAction(Tree node, Void aVoid) {
throw new UnsupportedOperationException(node.toString());
}
}, null));
JVar $f = m.body().decl($CpsFunction, "f", f);
m.body()._throw(JExpr._new($CpsCallableInvocation).arg(JExpr.lit(methodName)).arg($f).arg(JExpr._null()).args(params));
}
Aggregations