Search in sources :

Example 16 with ClassSymbol

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

the class TypeVariableImpl method owner.

/**
     * Return the class, interface, method, or constructor within
     * which this type variable is declared.
     */
public ProgramElementDoc owner() {
    Symbol osym = type.tsym.owner;
    if ((osym.kind & Kinds.TYP) != 0) {
        return env.getClassDoc((ClassSymbol) osym);
    }
    Names names = osym.name.table.names;
    if (osym.name == names.init) {
        return env.getConstructorDoc((MethodSymbol) osym);
    } else {
        return env.getMethodDoc((MethodSymbol) osym);
    }
}
Also used : Names(com.sun.tools.javac.util.Names) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Symbol(com.sun.tools.javac.code.Symbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol)

Example 17 with ClassSymbol

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

the class PackageDocImpl method getClasses.

/**
     * Return a list of all classes contained in this package, including
     * member classes of those classes, and their member classes, etc.
     */
private List<ClassDocImpl> getClasses(boolean filtered) {
    if (allClasses != null && !filtered) {
        return allClasses;
    }
    if (allClassesFiltered != null && filtered) {
        return allClassesFiltered;
    }
    ListBuffer<ClassDocImpl> classes = new ListBuffer<ClassDocImpl>();
    for (Scope.Entry e = sym.members().elems; e != null; e = e.sibling) {
        if (e.sym != null) {
            ClassSymbol s = (ClassSymbol) e.sym;
            ClassDocImpl c = env.getClassDoc(s);
            if (c != null && !c.isSynthetic())
                c.addAllClasses(classes, filtered);
        }
    }
    if (filtered)
        return allClassesFiltered = classes.toList();
    else
        return allClasses = classes.toList();
}
Also used : Scope(com.sun.tools.javac.code.Scope) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ListBuffer(com.sun.tools.javac.util.ListBuffer)

Example 18 with ClassSymbol

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

the class Attr method attribClass.

/** Attribute class definition associated with given class symbol.
     *  @param c   The class symbol whose definition will be attributed.
     */
void attribClass(ClassSymbol c) throws CompletionFailure {
    if (c.type.tag == ERROR)
        return;
    // Check for cycles in the inheritance graph, which can arise from
    // ill-formed class files.
    chk.checkNonCyclic(null, c.type);
    Type st = types.supertype(c.type);
    if ((c.flags_field & Flags.COMPOUND) == 0) {
        // First, attribute superclass.
        if (st.tag == CLASS)
            attribClass((ClassSymbol) st.tsym);
        // Next attribute owner, if it is a class.
        if (c.owner.kind == TYP && c.owner.type.tag == CLASS)
            attribClass((ClassSymbol) c.owner);
    }
    // UNATTRIBUTED.
    if ((c.flags_field & UNATTRIBUTED) != 0) {
        c.flags_field &= ~UNATTRIBUTED;
        // Get environment current at the point of class definition.
        Env<AttrContext> env = enter.typeEnvs.get(c);
        // The info.lint field in the envs stored in enter.typeEnvs is deliberately uninitialized,
        // because the annotations were not available at the time the env was created. Therefore,
        // we look up the environment chain for the first enclosing environment for which the
        // lint value is set. Typically, this is the parent env, but might be further if there
        // are any envs created as a result of TypeParameter nodes.
        Env<AttrContext> lintEnv = env;
        while (lintEnv.info.lint == null) lintEnv = lintEnv.next;
        // Having found the enclosing lint value, we can initialize the lint value for this class
        env.info.lint = lintEnv.info.lint.augment(c.attributes_field, c.flags());
        Lint prevLint = chk.setLint(env.info.lint);
        JavaFileObject prev = log.useSource(c.sourcefile);
        try {
            // java.lang.Enum may not be subclassed by a non-enum
            if (st.tsym == syms.enumSym && ((c.flags_field & (Flags.ENUM | Flags.COMPOUND)) == 0))
                log.error(env.tree.pos(), "enum.no.subclassing");
            // Enums may not be extended by source-level classes
            if (st.tsym != null && ((st.tsym.flags_field & Flags.ENUM) != 0) && ((c.flags_field & (Flags.ENUM | Flags.COMPOUND)) == 0) && !target.compilerBootstrap(c)) {
                log.error(env.tree.pos(), "enum.types.not.extensible");
            }
            attribClassBody(env, c);
            chk.checkDeprecatedAnnotation(env.tree.pos(), c);
        } finally {
            log.useSource(prev);
            chk.setLint(prevLint);
        }
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) ClassType(com.sun.tools.javac.code.Type.ClassType) MethodType(com.sun.tools.javac.code.Type.MethodType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type) ArrayType(com.sun.tools.javac.code.Type.ArrayType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Lint(com.sun.tools.javac.code.Lint)

Example 19 with ClassSymbol

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

the class Attr method visitMethodDef.

public void visitMethodDef(JCMethodDecl tree) {
    MethodSymbol m = tree.sym;
    Lint lint = env.info.lint.augment(m.attributes_field, m.flags());
    Lint prevLint = chk.setLint(lint);
    MethodSymbol prevMethod = chk.setMethod(m);
    try {
        deferredLintHandler.flush(tree.pos());
        chk.checkDeprecatedAnnotation(tree.pos(), m);
        attribBounds(tree.typarams);
        // JLS ???
        if (m.isStatic()) {
            chk.checkHideClashes(tree.pos(), env.enclClass.type, m);
        } else {
            chk.checkOverrideClashes(tree.pos(), env.enclClass.type, m);
        }
        chk.checkOverride(tree, m);
        // Create a new environment with local scope
        // for attributing the method.
        Env<AttrContext> localEnv = memberEnter.methodEnv(tree, env);
        localEnv.info.lint = lint;
        // Enter all type parameters into the local method scope.
        for (List<JCTypeParameter> l = tree.typarams; l.nonEmpty(); l = l.tail) localEnv.info.scope.enterIfAbsent(l.head.type.tsym);
        ClassSymbol owner = env.enclClass.sym;
        if ((owner.flags() & ANNOTATION) != 0 && tree.params.nonEmpty())
            log.error(tree.params.head.pos(), "intf.annotation.members.cant.have.params");
        // Attribute all value parameters.
        for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
            attribStat(l.head, localEnv);
        }
        chk.checkVarargsMethodDecl(localEnv, tree);
        // Check that type parameters are well-formed.
        chk.validate(tree.typarams, localEnv);
        // Check that result type is well-formed.
        chk.validate(tree.restype, localEnv);
        // annotation method checks
        if ((owner.flags() & ANNOTATION) != 0) {
            // annotation method cannot have throws clause
            if (tree.thrown.nonEmpty()) {
                log.error(tree.thrown.head.pos(), "throws.not.allowed.in.intf.annotation");
            }
            // annotation method cannot declare type-parameters
            if (tree.typarams.nonEmpty()) {
                log.error(tree.typarams.head.pos(), "intf.annotation.members.cant.have.type.params");
            }
            // validate annotation method's return type (could be an annotation type)
            chk.validateAnnotationType(tree.restype);
            // ensure that annotation method does not clash with members of Object/Annotation
            chk.validateAnnotationMethod(tree.pos(), m);
            if (tree.defaultValue != null) {
                // if default value is an annotation, check it is a well-formed
                // annotation value (e.g. no duplicate values, no missing values, etc.)
                chk.validateAnnotationTree(tree.defaultValue);
            }
        }
        for (List<JCExpression> l = tree.thrown; l.nonEmpty(); l = l.tail) chk.checkType(l.head.pos(), l.head.type, syms.throwableType);
        if (tree.body == null) {
            // in a retrofit signature class.
            if ((owner.flags() & INTERFACE) == 0 && (tree.mods.flags & (ABSTRACT | NATIVE)) == 0 && !relax)
                log.error(tree.pos(), "missing.meth.body.or.decl.abstract");
            if (tree.defaultValue != null) {
                if ((owner.flags() & ANNOTATION) == 0)
                    log.error(tree.pos(), "default.allowed.in.intf.annotation.member");
            }
        } else if ((owner.flags() & INTERFACE) != 0) {
            log.error(tree.body.pos(), "intf.meth.cant.have.body");
        } else if ((tree.mods.flags & ABSTRACT) != 0) {
            log.error(tree.pos(), "abstract.meth.cant.have.body");
        } else if ((tree.mods.flags & NATIVE) != 0) {
            log.error(tree.pos(), "native.meth.cant.have.body");
        } else {
            // or we are compiling class java.lang.Object.
            if (tree.name == names.init && owner.type != syms.objectType) {
                JCBlock body = tree.body;
                if (body.stats.isEmpty() || !TreeInfo.isSelfCall(names, body.stats.head)) {
                    body.stats = body.stats.prepend(memberEnter.SuperCall(make.at(body.pos), List.<Type>nil(), List.<JCVariableDecl>nil(), false));
                } else if ((env.enclClass.sym.flags() & ENUM) != 0 && (tree.mods.flags & GENERATEDCONSTR) == 0 && TreeInfo.isSuperCall(names, body.stats.head)) {
                    // enum constructors are not allowed to call super
                    // directly, so make sure there aren't any super calls
                    // in enum constructors, except in the compiler
                    // generated one.
                    log.error(tree.body.stats.head.pos(), "call.to.super.not.allowed.in.enum.ctor", env.enclClass.sym);
                }
            }
            // Attribute method body.
            attribStat(tree.body, localEnv);
        }
        localEnv.info.scope.leave();
        result = tree.type = m.type;
        chk.validateAnnotations(tree.mods.annotations, m);
    } finally {
        chk.setLint(prevLint);
        chk.setMethod(prevMethod);
    }
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Lint(com.sun.tools.javac.code.Lint) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) ClassType(com.sun.tools.javac.code.Type.ClassType) MethodType(com.sun.tools.javac.code.Type.MethodType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type) ArrayType(com.sun.tools.javac.code.Type.ArrayType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) DynamicMethodSymbol(com.sun.tools.javac.code.Symbol.DynamicMethodSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol)

Example 20 with ClassSymbol

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

the class JavacClass method getEnclosingClass.

@Override
public ClassMirror getEnclosingClass() {
    if (!enclosingClassSet) {
        Symbol encl = classSymbol.getEnclosingElement();
        if (encl != null && encl instanceof ClassSymbol) {
            enclosingClass = new JavacClass((ClassSymbol) encl);
        }
        enclosingClassSet = true;
    }
    return enclosingClass;
}
Also used : 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) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol)

Aggregations

ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)49 Symbol (com.sun.tools.javac.code.Symbol)20 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)20 Type (com.sun.tools.javac.code.Type)17 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)10 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)9 ClassType (com.sun.tools.javac.code.Type.ClassType)9 ClassTree (com.sun.source.tree.ClassTree)8 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)8 MethodTree (com.sun.source.tree.MethodTree)6 Tree (com.sun.source.tree.Tree)6 ArrayType (com.sun.tools.javac.code.Type.ArrayType)5 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)4 VariableTree (com.sun.source.tree.VariableTree)4 Name (com.sun.tools.javac.util.Name)4 ArrayList (java.util.ArrayList)4 Violation (com.google.errorprone.bugpatterns.threadsafety.ImmutableAnalysis.Violation)3 BlockTree (com.sun.source.tree.BlockTree)3 EnhancedForLoopTree (com.sun.source.tree.EnhancedForLoopTree)3 ForLoopTree (com.sun.source.tree.ForLoopTree)3