Search in sources :

Example 21 with JCClassDecl

use of com.sun.tools.javac.tree.JCTree.JCClassDecl in project robolectric by robolectric.

the class Helpers method isInShadowClass.

public static boolean isInShadowClass(TreePath path, VisitorState state) {
    Tree leaf = path.getLeaf();
    JCClassDecl classDecl = JCClassDecl.class.isInstance(leaf) ? (JCClassDecl) leaf : findEnclosingNode(state.getPath(), JCClassDecl.class);
    return hasAnnotation(classDecl, Implements.class, state);
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) Tree(com.sun.source.tree.Tree)

Example 22 with JCClassDecl

use of com.sun.tools.javac.tree.JCTree.JCClassDecl in project ceylon-compiler by ceylon.

the class CallableBuilder method build.

public JCExpression build() {
    // Generate a subclass of Callable
    ListBuffer<JCTree> classBody = new ListBuffer<JCTree>();
    gen.at(node);
    if (parameterDefaultValueMethods != null) {
        for (MethodDefinitionBuilder mdb : parameterDefaultValueMethods) {
            classBody.append(mdb.build());
        }
    }
    transformation.appendMethods(classBody);
    JCClassDecl classDef = gen.make().AnonymousClassDef(gen.make().Modifiers(0, annotations != null ? annotations : List.<JCAnnotation>nil()), classBody.toList());
    int variadicIndex = isVariadic ? numParams - 1 : -1;
    Type callableType;
    if (typeModel.isTypeConstructor()) {
        callableType = typeModel.getDeclaration().getExtendedType();
    } else {
        callableType = typeModel;
    }
    JCNewClass callableInstance = gen.make().NewClass(null, null, gen.makeJavaType(callableType, JT_EXTENDS | JT_CLASS_NEW), List.<JCExpression>of(gen.makeReifiedTypeArgument(callableType.getTypeArgumentList().get(0)), gen.makeReifiedTypeArgument(callableType.getTypeArgumentList().get(1)), gen.make().Literal(callableType.asString(true)), gen.make().TypeCast(gen.syms().shortType, gen.makeInteger(variadicIndex))), classDef);
    JCExpression result;
    if (typeModel.isTypeConstructor()) {
        result = buildTypeConstructor(callableType, callableInstance);
    } else {
        result = callableInstance;
    }
    gen.at(null);
    if (instanceSubstitution != null) {
        instanceSubstitution.close();
    }
    return result;
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCTree(com.sun.tools.javac.tree.JCTree) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass)

Example 23 with JCClassDecl

use of com.sun.tools.javac.tree.JCTree.JCClassDecl in project ceylon-compiler by ceylon.

the class Attr method attribClassBody.

/**
 * Finish the attribution of a class.
 */
private void attribClassBody(Env<AttrContext> env, ClassSymbol c) {
    JCClassDecl tree = (JCClassDecl) env.tree;
    Assert.check(c == tree.sym);
    // Validate annotations
    chk.validateAnnotations(tree.mods.annotations, c);
    // Validate type parameters, supertype and interfaces.
    attribBounds(tree.typarams);
    if (!c.isAnonymous()) {
        // already checked if anonymous
        chk.validate(tree.typarams, env);
        chk.validate(tree.extending, env);
        chk.validate(tree.implementing, env);
    }
    // methods or unimplemented methods of an implemented interface.
    if ((c.flags() & (ABSTRACT | INTERFACE)) == 0) {
        if (!relax)
            chk.checkAllDefined(tree.pos(), c);
    }
    if ((c.flags() & ANNOTATION) != 0) {
        if (tree.implementing.nonEmpty())
            log.error(tree.implementing.head.pos(), "cant.extend.intf.annotation");
        if (tree.typarams.nonEmpty())
            log.error(tree.typarams.head.pos(), "intf.annotation.cant.have.type.params");
    } else {
        // Check that all extended classes and interfaces
        // are compatible (i.e. no two define methods with same arguments
        // yet different return types).  (JLS 8.4.6.3)
        chk.checkCompatibleSupertypes(tree.pos(), c.type);
    }
    // Check that class does not import the same parameterized interface
    // with two different argument lists.
    chk.checkClassBounds(tree.pos(), c.type);
    tree.type = c.type;
    for (List<JCTypeParameter> l = tree.typarams; l.nonEmpty(); l = l.tail) {
        Assert.checkNonNull(env.info.scope.lookup(l.head.name).scope);
    }
    // Check that a generic class doesn't extend Throwable
    if (!sourceLanguage.isCeylon() && !c.type.allparams().isEmpty() && types.isSubtype(c.type, syms.throwableType))
        log.error(tree.extending.pos(), "generic.throwable");
    // Check that all methods which implement some
    // method conform to the method they implement.
    chk.checkImplementations(tree);
    // check that a resource implementing AutoCloseable cannot throw InterruptedException
    checkAutoCloseable(tree.pos(), env, c.type);
    for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
        // Attribute declaration
        attribStat(l.head, env);
        // Make an exception for static constants.
        if (c.owner.kind != PCK && ((c.flags() & STATIC) == 0 || c.name == names.empty) && (TreeInfo.flags(l.head) & (STATIC | INTERFACE)) != 0) {
            Symbol sym = null;
            if (l.head.getTag() == JCTree.VARDEF)
                sym = ((JCVariableDecl) l.head).sym;
            if (sym == null || sym.kind != VAR || ((VarSymbol) sym).getConstValue() == null)
                log.error(l.head.pos(), "icls.cant.have.static.decl", c);
        }
    }
    // Check for cycles among non-initial constructors.
    chk.checkCyclicConstructors(tree);
    // Check for cycles among annotation elements.
    chk.checkNonCyclicElements(tree);
    // Check for proper use of serialVersionUID
    if (env.info.lint.isEnabled(LintCategory.SERIAL) && isSerializable(c) && (c.flags() & Flags.ENUM) == 0 && (c.flags() & ABSTRACT) == 0) {
        checkSerialVersionUID(tree, c);
    }
}
Also used : JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) 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) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) DynamicMethodSymbol(com.sun.tools.javac.code.Symbol.DynamicMethodSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) OperatorSymbol(com.sun.tools.javac.code.Symbol.OperatorSymbol) JCTree(com.sun.tools.javac.tree.JCTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 24 with JCClassDecl

use of com.sun.tools.javac.tree.JCTree.JCClassDecl in project bazel by bazelbuild.

the class AnnotationProcessingPlugin method recordInfo.

private void recordInfo(JCCompilationUnit toplevel) {
    CompilationUnit.Builder builder = CompilationUnit.newBuilder();
    if (toplevel.sourcefile != null) {
        // FileObject#getName() returns the original exec root-relative path of
        // the source file, which is want we want.
        // Paths.get(sourcefile.toUri()) would absolutize the path.
        Path path = Paths.get(toplevel.sourcefile.getName());
        builder.setPath(processingModule.stripSourceRoot(path).toString());
        builder.setGeneratedByAnnotationProcessor(processingModule.isGenerated(path));
    }
    if (toplevel.getPackageName() != null) {
        builder.setPkg(toplevel.getPackageName().toString());
    }
    for (JCTree decl : toplevel.defs) {
        if (decl instanceof JCClassDecl) {
            builder.addTopLevel(((JCClassDecl) decl).getSimpleName().toString());
        }
    }
    processingModule.recordUnit(builder.build());
}
Also used : CompilationUnit(com.google.devtools.build.buildjar.proto.JavaCompilation.CompilationUnit) JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) Path(java.nio.file.Path) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCTree(com.sun.tools.javac.tree.JCTree)

Example 25 with JCClassDecl

use of com.sun.tools.javac.tree.JCTree.JCClassDecl in project bazel by bazelbuild.

the class TreePrunerTest method parseField.

JCVariableDecl parseField(String line) {
    JCCompilationUnit unit = parseLines("class T {", line, "}");
    JCClassDecl classDecl = (JCClassDecl) getOnlyElement(unit.defs);
    return (JCVariableDecl) getOnlyElement(classDecl.defs);
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Aggregations

JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)46 JavacNode (lombok.javac.JavacNode)24 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)23 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)14 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)11 ListBuffer (com.sun.tools.javac.util.ListBuffer)11 JCTree (com.sun.tools.javac.tree.JCTree)10 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)9 JavacTreeMaker (lombok.javac.JavacTreeMaker)9 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)6 Name (com.sun.tools.javac.util.Name)6 Type (com.sun.tools.javac.code.Type)5 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)5 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)4 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)4 JCIdent (com.sun.tools.javac.tree.JCTree.JCIdent)4 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)4 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)3 Type (com.redhat.ceylon.model.typechecker.model.Type)2 Tree (com.sun.source.tree.Tree)2