use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class Template method callCheckMethod.
/**
* Reflectively invoke Resolve.checkMethod(), which despite being package-private is apparently
* the only useful entry-point into javac8's type inference implementation.
*/
private MethodType callCheckMethod(Warner warner, Inliner inliner, Object resultInfo, List<Type> actualArgTypes, MethodSymbol methodSymbol, Type site, Env<AttrContext> env) throws InferException {
try {
Method checkMethod;
checkMethod = Resolve.class.getDeclaredMethod("checkMethod", Env.class, Type.class, Symbol.class, Class.forName(// ResultInfo is package-private
"com.sun.tools.javac.comp.Attr$ResultInfo"), List.class, List.class, Warner.class);
checkMethod.setAccessible(true);
return (MethodType) checkMethod.invoke(Resolve.instance(inliner.getContext()), env, site, methodSymbol, resultInfo, actualArgTypes, /*freeTypeVariables=*/
List.<Type>nil(), warner);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof Resolve.InapplicableMethodException) {
throw new InferException(ImmutableList.of(((Resolve.InapplicableMethodException) e.getTargetException()).getDiagnostic()));
}
throw new LinkageError(e.getMessage(), e.getCause());
} catch (ReflectiveOperationException e) {
throw new LinkageError(e.getMessage(), e);
}
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class InconsistentOverloads method processGroupMethods.
private void processGroupMethods(List<MethodTree> groupMethodTrees, VisitorState state) {
Preconditions.checkArgument(!groupMethodTrees.isEmpty());
for (ParameterOrderingViolation violation : getViolations(groupMethodTrees)) {
MethodSymbol methodSymbol = getSymbol(violation.methodTree());
if (ASTHelpers.findSuperMethods(methodSymbol, state.getTypes()).isEmpty()) {
Description.Builder description = buildDescription(violation.methodTree());
description.setMessage(violation.getDescription());
state.reportMatch(description.build());
}
}
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class ConstructorInvokesOverridable method traverse.
@Override
protected void traverse(Tree tree, VisitorState state) {
// If class is final, no method is overridable.
ClassTree classTree = state.findEnclosing(ClassTree.class);
if (classTree.getModifiers().getFlags().contains(Modifier.FINAL)) {
return;
}
ClassSymbol classSym = ASTHelpers.getSymbol(classTree);
tree.accept(new TreeScanner<Void, Void>() {
@Override
public Void visitMethodInvocation(MethodInvocationTree node, Void data) {
MethodSymbol method = ASTHelpers.getSymbol(node);
if (method != null && !method.isConstructor() && !method.isStatic() && !method.isPrivate() && !method.getModifiers().contains(Modifier.FINAL) && method.isMemberOf(classSym, state.getTypes())) {
state.reportMatch(describeMatch(node));
}
return super.visitMethodInvocation(node, data);
}
}, null);
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class EqualsHashCode method matchClass.
@Override
public Description matchClass(ClassTree classTree, VisitorState state) {
TypeSymbol symbol = ASTHelpers.getSymbol(classTree);
if (symbol.getKind() != ElementKind.CLASS) {
return Description.NO_MATCH;
}
MethodTree equals = null;
for (Tree member : classTree.getMembers()) {
if (!(member instanceof MethodTree)) {
continue;
}
MethodTree methodTree = (MethodTree) member;
if (EQUALS_MATCHER.matches(methodTree, state)) {
equals = methodTree;
}
}
if (equals == null || isSuppressed(equals)) {
return Description.NO_MATCH;
}
MethodSymbol hashCodeSym = ASTHelpers.resolveExistingMethod(state, symbol, state.getName("hashCode"), ImmutableList.<Type>of(), ImmutableList.<Type>of());
if (hashCodeSym.owner.equals(state.getSymtab().objectType.tsym)) {
return describeMatch(equals);
}
return Description.NO_MATCH;
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class EqualsIncompatibleType method compatibilityOfTypes.
public static TypeCompatibilityReport compatibilityOfTypes(Type receiverType, Type argumentType, Set<Type> previousReceiverTypes, Set<Type> previousArgumentTypes, VisitorState state) {
if (receiverType == null || argumentType == null) {
return TypeCompatibilityReport.createCompatibleReport();
}
// 1.7: java.lang.Object can be cast to primitives (implicitly through the boxed primitive type)
if (ASTHelpers.isCastable(argumentType, receiverType, state)) {
return leastUpperBoundGenericMismatch(receiverType, argumentType, previousReceiverTypes, previousArgumentTypes, state);
}
// Otherwise, we explore the superclasses of the receiver type as well as the interfaces it
// implements and we collect all overrides of java.lang.Object.equals(). If one of those
// overrides is inherited by the argument, then we don't flag the equality test.
Types types = state.getTypes();
Predicate<MethodSymbol> equalsPredicate = methodSymbol -> !methodSymbol.isStatic() && ((methodSymbol.flags() & Flags.SYNTHETIC) == 0) && types.isSameType(methodSymbol.getReturnType(), state.getSymtab().booleanType) && methodSymbol.getParameters().size() == 1 && types.isSameType(methodSymbol.getParameters().get(0).type, state.getSymtab().objectType);
Set<MethodSymbol> overridesOfEquals = ASTHelpers.findMatchingMethods(state.getName("equals"), equalsPredicate, receiverType, types);
Symbol argumentClass = ASTHelpers.getUpperBound(argumentType, state.getTypes()).tsym;
for (MethodSymbol method : overridesOfEquals) {
ClassSymbol methodClass = method.enclClass();
if (argumentClass.isSubClass(methodClass, types) && !methodClass.equals(state.getSymtab().objectType.tsym) && !methodClass.equals(state.getSymtab().enumSym)) {
// These should be compatible, but check any generic types for their compatbilities.
return leastUpperBoundGenericMismatch(receiverType, argumentType, previousReceiverTypes, previousArgumentTypes, state);
}
}
return TypeCompatibilityReport.incompatible(receiverType, argumentType);
}
Aggregations