Search in sources :

Example 46 with MethodSymbol

use of com.sun.tools.javac.code.Symbol.MethodSymbol in project ceylon-compiler by ceylon.

the class JavacClass method getDirectMethods.

@Override
public List<MethodMirror> getDirectMethods() {
    if (methods == null) {
        List<MethodMirror> ret = new LinkedList<MethodMirror>();
        for (Symbol sym : classSymbol.getEnclosedElements()) {
            if (sym instanceof MethodSymbol && (sym.flags() & Flags.PRIVATE) == 0) {
                ret.add(new JavacMethod(this, (MethodSymbol) sym));
            }
        }
        methods = Collections.unmodifiableList(ret);
    }
    return methods;
}
Also used : MethodMirror(com.redhat.ceylon.model.loader.mirror.MethodMirror) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Symbol(com.sun.tools.javac.code.Symbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) LinkedList(java.util.LinkedList)

Example 47 with MethodSymbol

use of com.sun.tools.javac.code.Symbol.MethodSymbol in project ceylon-compiler by ceylon.

the class CeylonModelLoader method isOverloadingMethod.

/**
 * Returns true if the given method is overloading an inherited method (from super class or interfaces).
 */
private boolean isOverloadingMethod(final MethodSymbol method) {
    /*
         * Copied from getOverriddenMethod and adapted for overloading
         */
    try {
        // interfaces have a different way to work
        if (method.owner.isInterface())
            return overloaded(method, method.owner.type.tsym, types);
        // so we stop there for it, especially since it does not have any overloading
        if (method.owner.type.tsym.getQualifiedName().toString().equals("ceylon.language.Exception"))
            return false;
        for (Type superType = types.supertype(method.owner.type); superType.tsym != null; superType = types.supertype(superType)) {
            TypeSymbol i = superType.tsym;
            String fqn = i.getQualifiedName().toString();
            // never go above this type since it has no supertype in Ceylon (does in Java though)
            if (fqn.equals("ceylon.language.Anything"))
                break;
            try {
                for (Entry e = i.members().lookup(method.name); e.scope != null; e = e.next()) {
                    // ignore some methods
                    if (isIgnored(e.sym))
                        continue;
                    if (!method.overrides(e.sym, (TypeSymbol) method.owner, types, false)) {
                        return true;
                    }
                }
                // try in the interfaces
                if (overloaded(method, i, types))
                    return true;
            } catch (Symbol.CompletionFailure x) {
            // just ignore unresolved interfaces, error will be logged when we try to add it
            }
            // so we stop there for it, especially since it does not have any overloading
            if (fqn.equals("ceylon.language.Exception"))
                break;
        }
        // try in the interfaces
        if (overloaded(method, method.owner.type.tsym, types))
            return true;
        return false;
    } catch (CompletionFailure x) {
        handleCompletionFailure(method, x);
        return false;
    }
}
Also used : Type(com.sun.tools.javac.code.Type) UnknownType(com.redhat.ceylon.model.typechecker.model.UnknownType) Entry(com.sun.tools.javac.code.Scope.Entry) CompletionFailure(com.sun.tools.javac.code.Symbol.CompletionFailure) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) CompletionFailure(com.sun.tools.javac.code.Symbol.CompletionFailure) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol)

Example 48 with MethodSymbol

use of com.sun.tools.javac.code.Symbol.MethodSymbol in project ceylon-compiler by ceylon.

the class CeylonModelLoader method implemented.

/**
 * Copied from MethodSymbol.implemented and adapted for ignoring methods
 */
private Symbol implemented(MethodSymbol m, TypeSymbol c, Types types) {
    Symbol impl = null;
    for (List<Type> is = types.interfaces(c.type); impl == null && is.nonEmpty(); is = is.tail) {
        TypeSymbol i = is.head.tsym;
        impl = implementedIn(m, i, types);
        if (impl == null)
            impl = implemented(m, i, types);
    }
    return impl;
}
Also used : Type(com.sun.tools.javac.code.Type) UnknownType(com.redhat.ceylon.model.typechecker.model.UnknownType) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol)

Example 49 with MethodSymbol

use of com.sun.tools.javac.code.Symbol.MethodSymbol in project checker-framework by typetools.

the class DefaultReflectionResolver method getMethodSymbolsfor.

/**
 * Get set of MethodSymbols based on class name, method name, and parameter length.
 *
 * @param className the class that contains the method
 * @param methodName the method's name
 * @param paramLength the number of parameters
 * @param env the environment
 * @return the (potentially empty) set of corresponding method Symbol(s)
 */
private List<Symbol> getMethodSymbolsfor(String className, String methodName, int paramLength, Env<AttrContext> env) {
    Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
    Resolve resolve = Resolve.instance(context);
    Names names = Names.instance(context);
    Symbol sym = getSymbol(className, env, names, resolve);
    if (!sym.exists()) {
        debugReflection("Unable to resolve class: " + className);
        return Collections.emptyList();
    }
    // The common case is probably that `result` is a singleton at method exit.
    List<Symbol> result = new ArrayList<>();
    ClassSymbol classSym = (ClassSymbol) sym;
    while (classSym != null) {
        for (Symbol s : classSym.getEnclosedElements()) {
            // check all member methods
            if (s.getKind() == ElementKind.METHOD) {
                // Check for method name and number of arguments
                if (names.fromString(methodName) == s.name && ((MethodSymbol) s).getParameters().size() == paramLength) {
                    result.add(s);
                }
            }
        }
        if (!result.isEmpty()) {
            break;
        }
        Type t = classSym.getSuperclass();
        if (!t.hasTag(TypeTag.CLASS) || t.isErroneous()) {
            break;
        }
        classSym = (ClassSymbol) t.tsym;
    }
    if (result.isEmpty()) {
        debugReflection("Unable to resolve method: " + className + "@" + methodName);
    }
    return result;
}
Also used : Context(com.sun.tools.javac.util.Context) AttrContext(com.sun.tools.javac.comp.AttrContext) Names(com.sun.tools.javac.util.Names) AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) ParameterizedExecutableType(org.checkerframework.framework.type.AnnotatedTypeFactory.ParameterizedExecutableType) Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Symbol(com.sun.tools.javac.code.Symbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) ArrayList(java.util.ArrayList) Resolve(com.sun.tools.javac.comp.Resolve)

Example 50 with MethodSymbol

use of com.sun.tools.javac.code.Symbol.MethodSymbol in project checker-framework by typetools.

the class DefaultReflectionResolver method getCorrectedArgs.

private com.sun.tools.javac.util.List<JCExpression> getCorrectedArgs(Symbol symbol, com.sun.tools.javac.util.List<JCExpression> args) {
    if (symbol.getKind() == ElementKind.METHOD) {
        MethodSymbol method = ((MethodSymbol) symbol);
        // neg means too many arg,
        // pos means to few args
        int diff = method.getParameters().size() - args.size();
        if (diff > 0) {
            // means too few args
            int origArgSize = args.size();
            for (int i = 0; i < diff; i++) {
                args = args.append(args.get(i % origArgSize));
            }
        } else if (diff < 0) {
            // means too many args
            com.sun.tools.javac.util.List<JCExpression> tmp = com.sun.tools.javac.util.List.nil();
            for (int i = 0; i < method.getParameters().size(); i++) {
                tmp = tmp.append(args.get(i));
            }
            args = tmp;
        }
    }
    return args;
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) List(java.util.List) ArrayList(java.util.ArrayList)

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