use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class InputStreamSlowMultibyteRead method matchClass.
@Override
public Description matchClass(ClassTree classTree, VisitorState state) {
if (!IS_INPUT_STREAM.matches(classTree, state)) {
return Description.NO_MATCH;
}
TypeSymbol thisClassSymbol = ASTHelpers.getSymbol(classTree);
if (thisClassSymbol.getKind() != ElementKind.CLASS) {
return Description.NO_MATCH;
}
// Find the method that overrides the single-byte read. It should also override the multibyte
// read.
MethodTree readByteMethod = classTree.getMembers().stream().filter(MethodTree.class::isInstance).map(MethodTree.class::cast).filter(m -> READ_INT_METHOD.matches(m, state)).findFirst().orElse(null);
if (readByteMethod == null) {
return Description.NO_MATCH;
}
Type byteArrayType = state.arrayTypeForType(state.getSymtab().byteType);
Type intType = state.getSymtab().intType;
MethodSymbol multiByteReadMethod = ASTHelpers.resolveExistingMethod(state, thisClassSymbol, state.getName("read"), ImmutableList.of(byteArrayType, intType, intType), ImmutableList.of());
return multiByteReadMethod.owner.equals(thisClassSymbol) ? Description.NO_MATCH : maybeMatchReadByte(readByteMethod, state);
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class NullableVoid method matchMethod.
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
MethodSymbol sym = ASTHelpers.getSymbol(tree);
if (sym == null) {
return NO_MATCH;
}
if (sym.getReturnType().getKind() != TypeKind.VOID) {
return NO_MATCH;
}
AnnotationTree annotation = ASTHelpers.getAnnotationWithSimpleName(tree.getModifiers().getAnnotations(), "Nullable");
if (annotation == null) {
return NO_MATCH;
}
return describeMatch(annotation, SuggestedFix.delete(annotation));
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project lombok by rzwitserloot.
the class JavacHandlerUtil method fixMethodMirror.
private static void fixMethodMirror(Context context, Element typeMirror, long access, Name methodName, List<Type> paramTypes, Type returnType) {
if (typeMirror == null || paramTypes == null || returnType == null)
return;
ClassSymbol cs = (ClassSymbol) typeMirror;
MethodSymbol methodSymbol = new MethodSymbol(access, methodName, new MethodType(paramTypes, returnType, List.<Type>nil(), Symtab.instance(context).methodClass), cs);
ClassSymbolMembersField.enter(cs, methodSymbol);
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project checker-framework by typetools.
the class FlowExpressionParseUtil method parseMethod.
private static Receiver parseMethod(String s, FlowExpressionContext context, TreePath path, ProcessingEnvironment env) throws FlowExpressionParseException {
Pair<Pair<String, String>, String> method = parseMethod(s);
if (method == null) {
return null;
}
String methodName = method.first.first;
// parse parameter list
String parameterList = method.first.second;
List<Receiver> parameters = ParameterListParser.parseParameterList(parameterList, true, context.copyAndUseOuterReceiver(), path);
// get types for parameters
List<TypeMirror> parameterTypes = new ArrayList<>();
for (Receiver p : parameters) {
parameterTypes.add(p.getType());
}
ExecutableElement methodElement = null;
try {
Element element = null;
// try to find the correct method
Resolver resolver = new Resolver(env);
TypeMirror receiverType = context.receiver.getType();
if (receiverType.getKind() == TypeKind.ARRAY) {
element = resolver.findMethod(methodName, receiverType, path, parameterTypes);
}
// Search for method in each enclosing class.
while (receiverType.getKind() == TypeKind.DECLARED) {
element = resolver.findMethod(methodName, receiverType, path, parameterTypes);
if (element.getKind() == ElementKind.METHOD) {
break;
}
receiverType = getTypeOfEnclosingClass((DeclaredType) receiverType);
}
if (element == null) {
throw constructParserException(s, "element==null");
}
if (element.getKind() != ElementKind.METHOD) {
throw constructParserException(s, "element.getKind()==" + element.getKind());
}
methodElement = (ExecutableElement) element;
for (int i = 0; i < parameters.size(); i++) {
VariableElement formal = methodElement.getParameters().get(i);
TypeMirror formalType = formal.asType();
Receiver actual = parameters.get(i);
TypeMirror actualType = actual.getType();
// boxing necessary
if (TypesUtils.isBoxedPrimitive(formalType) && TypesUtils.isPrimitive(actualType)) {
MethodSymbol valueOfMethod = TreeBuilder.getValueOfMethod(env, formalType);
List<Receiver> p = new ArrayList<>();
p.add(actual);
Receiver boxedParam = new MethodCall(formalType, valueOfMethod, new ClassName(formalType), p);
parameters.set(i, boxedParam);
}
}
} catch (Throwable t) {
throw constructParserException(s, t);
}
assert methodElement != null;
/*if (!PurityUtils.isDeterministic(context.checkerContext.getAnnotationProvider(),
methodElement)) {
throw new FlowExpressionParseException(Result.failure(
"flowexpr.method.not.deterministic",
methodElement.getSimpleName()));
}*/
if (ElementUtils.isStatic(methodElement)) {
Element classElem = methodElement.getEnclosingElement();
Receiver staticClassReceiver = new ClassName(ElementUtils.getType(classElem));
return new MethodCall(ElementUtils.getType(methodElement), methodElement, staticClassReceiver, parameters);
} else {
if (context.receiver instanceof ClassName) {
throw constructParserException(s, "a non-static method call cannot have a class name as a receiver.");
}
TypeMirror methodType = TypesUtils.substituteMethodReturnType(methodElement, context.receiver.getType(), env);
return new MethodCall(methodType, methodElement, context.receiver, parameters);
}
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project checker-framework by typetools.
the class TypesIntoElements method storeMethod.
private static void storeMethod(ProcessingEnvironment processingEnv, Types types, AnnotatedTypeFactory atypeFactory, MethodTree meth) {
AnnotatedExecutableType mtype = atypeFactory.getAnnotatedType(meth);
MethodSymbol sym = (MethodSymbol) TreeUtils.elementFromDeclaration(meth);
TypeAnnotationPosition tapos;
List<Attribute.TypeCompound> tcs = List.nil();
storeTypeParameters(processingEnv, types, atypeFactory, meth.getTypeParameters(), sym);
{
// return type
JCTree ret = ((JCTree.JCMethodDecl) meth).getReturnType();
if (ret != null) {
tapos = TypeAnnotationUtils.methodReturnTAPosition(ret.pos);
tcs = tcs.appendList(generateTypeCompounds(processingEnv, mtype.getReturnType(), tapos));
}
}
{
// receiver
JCTree receiverTree = ((JCTree.JCMethodDecl) meth).getReceiverParameter();
if (receiverTree != null) {
tapos = TypeAnnotationUtils.methodReceiverTAPosition(receiverTree.pos);
tcs = tcs.appendList(generateTypeCompounds(processingEnv, mtype.getReceiverType(), tapos));
}
}
{
// parameters
int pidx = 0;
java.util.List<AnnotatedTypeMirror> ptypes = mtype.getParameterTypes();
for (JCTree param : ((JCTree.JCMethodDecl) meth).getParameters()) {
tapos = TypeAnnotationUtils.methodParameterTAPosition(pidx, param.pos);
tcs = tcs.appendList(generateTypeCompounds(processingEnv, ptypes.get(pidx), tapos));
++pidx;
}
}
{
// throws clauses
int tidx = 0;
java.util.List<AnnotatedTypeMirror> ttypes = mtype.getThrownTypes();
for (JCTree thr : ((JCTree.JCMethodDecl) meth).getThrows()) {
tapos = TypeAnnotationUtils.methodThrowsTAPosition(tidx, thr.pos);
tcs = tcs.appendList(generateTypeCompounds(processingEnv, ttypes.get(tidx), tapos));
++tidx;
}
}
addUniqueTypeCompounds(types, sym, tcs);
}
Aggregations