use of com.sun.tools.javac.code.Symbol.MethodSymbol in project checker-framework by typetools.
the class TypeVarUseApplier method getParameterAnnos.
/**
* Currently, the metadata for storing annotations (i.e. the Attribute.TypeCompounds) is null for
* binary-only parameters and type parameters. However, it is present on the method. So in order
* to ensure that we correctly retrieve the annotations we need to index from the method and
* retrieve the annotations from its metadata.
*
* @return a list of annotations that were found on METHOD_FORMAL_PARAMETERS that match the
* parameter index of the input element in the parent methods formal parameter list
*/
private static List<Attribute.TypeCompound> getParameterAnnos(final Element paramElem) {
final Element enclosingElement = paramElem.getEnclosingElement();
if (!(enclosingElement instanceof ExecutableElement)) {
throw new BugInCF("Bad element passed to TypeFromElement.getTypeParameterAnnotationAttributes: " + "element: " + paramElem + " not found in enclosing executable: " + enclosingElement);
}
final MethodSymbol enclosingMethod = (MethodSymbol) enclosingElement;
if (enclosingMethod.getKind() != ElementKind.CONSTRUCTOR && enclosingMethod.getKind() != ElementKind.METHOD) {
// Initializer blocks don't have parameters, so there is nothing to do.
return Collections.emptyList();
}
// TODO: for the parameter in a lambda expression, the enclosingMethod isn't
// the lambda expression. Does this read the correct annotations?
final int paramIndex = enclosingMethod.getParameters().indexOf(paramElem);
final List<Attribute.TypeCompound> annotations = enclosingMethod.getRawTypeAttributes();
final List<Attribute.TypeCompound> result = new ArrayList<>();
for (final Attribute.TypeCompound typeAnno : annotations) {
if (typeAnno.position.type == TargetType.METHOD_FORMAL_PARAMETER) {
if (typeAnno.position.parameter_index == paramIndex) {
result.add(typeAnno);
}
}
}
return result;
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project checker-framework by typetools.
the class TypeVarUseApplier method getReturnAnnos.
/**
* Returns the annotations on the return type of the input ExecutableElement.
*
* @param methodElem the method whose return type annotations to return
* @return the annotations on the return type of the input ExecutableElement
*/
private static List<Attribute.TypeCompound> getReturnAnnos(final Element methodElem) {
if (!(methodElem instanceof ExecutableElement)) {
throw new BugInCF("Bad element passed to TypeVarUseApplier.getReturnAnnos:" + methodElem);
}
final MethodSymbol enclosingMethod = (MethodSymbol) methodElem;
final List<Attribute.TypeCompound> annotations = enclosingMethod.getRawTypeAttributes();
final List<Attribute.TypeCompound> result = new ArrayList<>();
for (final Attribute.TypeCompound typeAnno : annotations) {
if (typeAnno.position.type == TargetType.METHOD_RETURN) {
result.add(typeAnno);
}
}
return result;
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project checker-framework by typetools.
the class TreeUtils method isSynthetic.
/**
* Returns true if the given method is synthetic. Also returns true if the method is a generated
* default constructor, which does not appear in source code but is not considered synthetic.
*
* @param ee a method or constructor element
* @return true iff the given method is synthetic
*/
public static boolean isSynthetic(ExecutableElement ee) {
MethodSymbol ms = (MethodSymbol) ee;
long mod = ms.flags();
// GENERATEDCONSTR is for generated constructors, which do not have SYNTHETIC set.
return (mod & (Flags.SYNTHETIC | Flags.GENERATEDCONSTR)) != 0;
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project bazel by bazelbuild.
the class TreeUtils method hasExplicitConstructor.
/**
* Determine whether the given class contains an explicit constructor.
*
* @param node A class tree.
* @return True, iff there is an explicit constructor.
*/
public static boolean hasExplicitConstructor(ClassTree node) {
TypeElement elem = TreeUtils.elementFromDeclaration(node);
for (ExecutableElement ee : ElementFilter.constructorsIn(elem.getEnclosedElements())) {
MethodSymbol ms = (MethodSymbol) ee;
long mod = ms.flags();
if ((mod & Flags.SYNTHETIC) == 0) {
return true;
}
}
return false;
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class AbstractExpectedExceptionChecker method buildBaseFix.
protected BaseFix buildBaseFix(VisitorState state, List<Tree> expectations) {
String exceptionClass = "Throwable";
// additional assertions to perform on the captured exception (if any)
List<String> newAsserts = new ArrayList<>();
Builder fix = SuggestedFix.builder();
for (Tree expectation : expectations) {
MethodInvocationTree invocation = (MethodInvocationTree) ((ExpressionStatementTree) expectation).getExpression();
MethodSymbol symbol = ASTHelpers.getSymbol(invocation);
Symtab symtab = state.getSymtab();
List<? extends ExpressionTree> args = invocation.getArguments();
switch(symbol.getSimpleName().toString()) {
case "expect":
if (isSubtype(getOnlyElement(symbol.getParameters()).asType(), symtab.classType, state)) {
// expect(Class<?>)
exceptionClass = state.getSourceForNode(getReceiver(getOnlyElement(args)));
} else {
// expect(Matcher)
fix.addStaticImport("org.hamcrest.MatcherAssert.assertThat");
newAsserts.add(String.format("assertThat(thrown, %s);", state.getSourceForNode(getOnlyElement(args))));
}
break;
case "expectCause":
ExpressionTree matcher = getOnlyElement(invocation.getArguments());
if (IS_A.matches(matcher, state)) {
fix.addStaticImport("com.google.common.truth.Truth.assertThat");
newAsserts.add(String.format("assertThat(thrown).hasCauseThat().isInstanceOf(%s);", state.getSourceForNode(getOnlyElement(((MethodInvocationTree) matcher).getArguments()))));
} else {
fix.addStaticImport("org.hamcrest.MatcherAssert.assertThat");
newAsserts.add(String.format("assertThat(thrown.getCause(), %s);", state.getSourceForNode(getOnlyElement(args))));
}
break;
case "expectMessage":
if (isSubtype(getOnlyElement(symbol.getParameters()).asType(), symtab.stringType, state)) {
// expectedMessage(String)
fix.addStaticImport("com.google.common.truth.Truth.assertThat");
newAsserts.add(String.format("assertThat(thrown).hasMessageThat().contains(%s);", state.getSourceForNode(getOnlyElement(args))));
} else {
// expectedMessage(Matcher)
fix.addStaticImport("org.hamcrest.MatcherAssert.assertThat");
newAsserts.add(String.format("assertThat(thrown.getMessage(), %s);", state.getSourceForNode(getOnlyElement(args))));
}
break;
default:
throw new AssertionError("unknown expect method: " + symbol.getSimpleName());
}
}
// remove all interactions with the ExpectedException rule
fix.replace(((JCTree) expectations.get(0)).getStartPosition(), state.getEndPosition(getLast(expectations)), "");
return new BaseFix(fix.build(), exceptionClass, newAsserts);
}
Aggregations