use of com.sun.tools.javac.tree.TreeMaker in project error-prone by google.
the class UUnary method inline.
@Override
public JCExpression inline(Inliner inliner) throws CouldNotResolveImportException {
JCExpression expr = getExpression().inline(inliner);
final TreeMaker maker = inliner.maker();
if (getKind() == Kind.LOGICAL_COMPLEMENT) {
return new TreeCopier<Void>(maker) {
@SuppressWarnings("unchecked")
// essentially depends on T being a superclass of JCExpression
@Override
public <T extends JCTree> T copy(T t, Void v) {
if (t instanceof BinaryTree || t instanceof UnaryTree || t instanceof ConditionalExpressionTree) {
return super.copy(t, v);
} else {
return (T) defaultNegation(t);
}
}
public JCExpression defaultNegation(Tree expr) {
return maker.Unary(JCTree.Tag.NOT, (JCExpression) expr);
}
@Override
public JCExpression visitBinary(BinaryTree tree, Void v) {
if (UBinary.DEMORGAN.containsKey(tree.getKind())) {
JCExpression negLeft = copy((JCExpression) tree.getLeftOperand());
JCExpression negRight = copy((JCExpression) tree.getRightOperand());
return maker.Binary(UBinary.OP_CODES.get(UBinary.DEMORGAN.get(tree.getKind())), negLeft, negRight);
} else if (UBinary.NEGATION.containsKey(tree.getKind())) {
JCExpression left = (JCExpression) tree.getLeftOperand();
JCExpression right = (JCExpression) tree.getRightOperand();
return maker.Binary(UBinary.OP_CODES.get(UBinary.NEGATION.get(tree.getKind())), left, right);
} else {
return defaultNegation(tree);
}
}
@Override
public JCExpression visitUnary(UnaryTree tree, Void v) {
if (tree.getKind() == Kind.LOGICAL_COMPLEMENT) {
return (JCExpression) tree.getExpression();
} else {
return defaultNegation(tree);
}
}
@Override
public JCConditional visitConditionalExpression(ConditionalExpressionTree tree, Void v) {
return maker.Conditional((JCExpression) tree.getCondition(), copy((JCExpression) tree.getTrueExpression()), copy((JCExpression) tree.getFalseExpression()));
}
}.copy(expr);
} else {
return inliner.maker().Unary(UNARY_OP_CODES.get(getKind()), getExpression().inline(inliner));
}
}
use of com.sun.tools.javac.tree.TreeMaker in project error-prone by google.
the class UVariableDecl method inline.
private JCVariableDecl inline(@Nullable UExpression type, Inliner inliner) throws CouldNotResolveImportException {
Optional<LocalVarBinding> binding = inliner.getOptionalBinding(key());
JCModifiers modifiers;
Name name;
TreeMaker maker = inliner.maker();
if (binding.isPresent()) {
modifiers = (JCModifiers) binding.get().getModifiers();
name = binding.get().getName();
} else {
modifiers = maker.Modifiers(0L);
name = getName().inline(inliner);
}
return maker.VarDef(modifiers, name, (type == null) ? null : type.inline(inliner), (getInitializer() == null) ? null : getInitializer().inline(inliner));
}
use of com.sun.tools.javac.tree.TreeMaker in project error-prone by google.
the class TypeParameterQualifier method matchMemberSelect.
@Override
public Description matchMemberSelect(MemberSelectTree tree, VisitorState state) {
Symbol baseSym = ASTHelpers.getSymbol(tree.getExpression());
if (baseSym == null || baseSym.getKind() != ElementKind.TYPE_PARAMETER) {
return Description.NO_MATCH;
}
TreeMaker make = TreeMaker.instance(state.context).forToplevel((JCCompilationUnit) state.getPath().getCompilationUnit());
JCExpression qual = make.QualIdent(ASTHelpers.getSymbol(tree));
return describeMatch(tree, SuggestedFix.replace(tree, qual.toString()));
}
use of com.sun.tools.javac.tree.TreeMaker in project error-prone by google.
the class GuardedBySymbolResolver method attribIdent.
private Symbol attribIdent(String name) {
Attr attr = Attr.instance(context);
TreeMaker tm = TreeMaker.instance(context);
return attr.attribIdent(tm.Ident(getName(name)), compilationUnit);
}
use of com.sun.tools.javac.tree.TreeMaker in project checker-framework by typetools.
the class DefaultReflectionResolver method resolveReflectiveMethod.
/**
* Resolves a reflective method call and returns all possible corresponding method calls.
*
* @param tree the MethodInvocationTree node that is to be resolved (Method.invoke)
* @return a (potentially empty) list of all resolved MethodInvocationTrees
*/
private List<MethodInvocationTree> resolveReflectiveMethod(MethodInvocationTree tree, AnnotatedTypeFactory reflectionFactory) {
assert isReflectiveMethodInvocation(tree);
JCMethodInvocation methodInvocation = (JCMethodInvocation) tree;
Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
TreeMaker make = TreeMaker.instance(context);
TreePath path = reflectionFactory.getPath(tree);
JavacScope scope = (JavacScope) trees.getScope(path);
Env<AttrContext> env = scope.getEnv();
List<MethodInvocationTree> methods = new ArrayList<>();
boolean unknown = isUnknownMethod(tree);
AnnotationMirror estimate = getMethodVal(tree);
if (estimate == null) {
debugReflection("MethodVal is unknown for: " + tree);
debugReflection("UnknownMethod annotation: " + unknown);
return methods;
}
debugReflection("MethodVal type system annotations: " + estimate);
List<String> listClassNames = AnnotationUtils.getElementValueArray(estimate, "className", String.class, true);
List<String> listMethodNames = AnnotationUtils.getElementValueArray(estimate, "methodName", String.class, true);
List<Integer> listParamLenghts = AnnotationUtils.getElementValueArray(estimate, "params", Integer.class, true);
assert listClassNames.size() == listMethodNames.size() && listClassNames.size() == listParamLenghts.size();
for (int i = 0; i < listClassNames.size(); ++i) {
String className = listClassNames.get(i);
String methodName = listMethodNames.get(i);
int paramLength = listParamLenghts.get(i);
// Get receiver, which is always the first argument of the invoke
// method
JCExpression receiver = methodInvocation.args.head;
// The remaining list contains the arguments
com.sun.tools.javac.util.List<JCExpression> args = methodInvocation.args.tail;
// Resolve the Symbol(s) for the current method
for (Symbol symbol : getMethodSymbolsfor(className, methodName, paramLength, env)) {
if ((symbol.flags() & Flags.PUBLIC) > 0) {
debugReflection("Resolved public method: " + symbol.owner + "." + symbol);
} else {
debugReflection("Resolved non-public method: " + symbol.owner + "." + symbol);
}
JCExpression method = make.Select(receiver, symbol);
args = getCorrectedArgs(symbol, args);
// Build method invocation tree depending on the number of
// parameters
JCMethodInvocation syntTree = paramLength > 0 ? make.App(method, args) : make.App(method);
// add method invocation tree to the list of possible methods
methods.add(syntTree);
}
}
return methods;
}
Aggregations