Search in sources :

Example 61 with MethodSymbol

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);
}
Also used : Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodTree(com.sun.source.tree.MethodTree) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol)

Example 62 with MethodSymbol

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));
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) AnnotationTree(com.sun.source.tree.AnnotationTree)

Example 63 with MethodSymbol

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);
}
Also used : MethodType(com.sun.tools.javac.code.Type.MethodType) NullCheckExceptionType(lombok.core.configuration.NullCheckExceptionType) MethodType(com.sun.tools.javac.code.Type.MethodType) Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol)

Example 64 with 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);
    }
}
Also used : Resolver(org.checkerframework.javacutil.Resolver) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver) VariableElement(javax.lang.model.element.VariableElement) MethodCall(org.checkerframework.dataflow.analysis.FlowExpressions.MethodCall) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) TypeMirror(javax.lang.model.type.TypeMirror) ClassName(org.checkerframework.dataflow.analysis.FlowExpressions.ClassName) Pair(org.checkerframework.javacutil.Pair) DeclaredType(javax.lang.model.type.DeclaredType)

Example 65 with MethodSymbol

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);
}
Also used : TypeCompound(com.sun.tools.javac.code.Attribute.TypeCompound) AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) JCTree(com.sun.tools.javac.tree.JCTree) List(com.sun.tools.javac.util.List) TypeAnnotationPosition(com.sun.tools.javac.code.TypeAnnotationPosition)

Aggregations

MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)98 Symbol (com.sun.tools.javac.code.Symbol)35 Type (com.sun.tools.javac.code.Type)32 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)27 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)22 MethodTree (com.sun.source.tree.MethodTree)22 Tree (com.sun.source.tree.Tree)21 ArrayList (java.util.ArrayList)18 ExpressionTree (com.sun.source.tree.ExpressionTree)17 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)13 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)13 Description (com.google.errorprone.matchers.Description)11 ClassTree (com.sun.source.tree.ClassTree)11 VisitorState (com.google.errorprone.VisitorState)10 JCTree (com.sun.tools.javac.tree.JCTree)9 Types (com.sun.tools.javac.code.Types)8 BugPattern (com.google.errorprone.BugPattern)7 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)7 MemberSelectTree (com.sun.source.tree.MemberSelectTree)7 MethodType (com.sun.tools.javac.code.Type.MethodType)7