Search in sources :

Example 1 with JCClassDecl

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

the class HandleBuilder method makeBuilderClass.

public JavacNode makeBuilderClass(boolean isStatic, JavacNode source, JavacNode tdParent, String builderClassName, List<JCTypeParameter> typeParams, JCAnnotation ast) {
    JavacTreeMaker maker = tdParent.getTreeMaker();
    int modifiers = Flags.PUBLIC;
    if (isStatic)
        modifiers |= Flags.STATIC;
    JCModifiers mods = maker.Modifiers(modifiers);
    JCClassDecl builder = maker.ClassDef(mods, tdParent.toName(builderClassName), copyTypeParams(source, typeParams), null, List.<JCExpression>nil(), List.<JCTree>nil());
    return injectType(tdParent, builder);
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers)

Example 2 with JCClassDecl

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

the class HandleConstructor method createStaticConstructor.

public JCMethodDecl createStaticConstructor(String name, AccessLevel level, JavacNode typeNode, List<JavacNode> fields, JCTree source) {
    JavacTreeMaker maker = typeNode.getTreeMaker();
    JCClassDecl type = (JCClassDecl) typeNode.get();
    JCModifiers mods = maker.Modifiers(Flags.STATIC | toJavacModifier(level));
    JCExpression returnType, constructorType;
    ListBuffer<JCTypeParameter> typeParams = new ListBuffer<JCTypeParameter>();
    ListBuffer<JCVariableDecl> params = new ListBuffer<JCVariableDecl>();
    ListBuffer<JCExpression> typeArgs1 = new ListBuffer<JCExpression>();
    ListBuffer<JCExpression> typeArgs2 = new ListBuffer<JCExpression>();
    ListBuffer<JCExpression> args = new ListBuffer<JCExpression>();
    if (!type.typarams.isEmpty()) {
        for (JCTypeParameter param : type.typarams) {
            typeArgs1.append(maker.Ident(param.name));
            typeArgs2.append(maker.Ident(param.name));
            typeParams.append(maker.TypeParameter(param.name, param.bounds));
        }
        returnType = maker.TypeApply(maker.Ident(type.name), typeArgs1.toList());
        constructorType = maker.TypeApply(maker.Ident(type.name), typeArgs2.toList());
    } else {
        returnType = maker.Ident(type.name);
        constructorType = maker.Ident(type.name);
    }
    for (JavacNode fieldNode : fields) {
        JCVariableDecl field = (JCVariableDecl) fieldNode.get();
        Name fieldName = removePrefixFromField(fieldNode);
        JCExpression pType = cloneType(maker, field.vartype, source, typeNode.getContext());
        List<JCAnnotation> nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN);
        List<JCAnnotation> nullables = findAnnotations(fieldNode, NULLABLE_PATTERN);
        long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext());
        JCVariableDecl param = maker.VarDef(maker.Modifiers(flags, nonNulls.appendList(nullables)), fieldName, pType, null);
        params.append(param);
        args.append(maker.Ident(fieldName));
    }
    JCReturn returnStatement = maker.Return(maker.NewClass(null, List.<JCExpression>nil(), constructorType, args.toList(), null));
    JCBlock body = maker.Block(0, List.<JCStatement>of(returnStatement));
    return recursiveSetGeneratedBy(maker.MethodDef(mods, typeNode.toName(name), returnType, typeParams.toList(), params.toList(), List.<JCExpression>nil(), body, null), source, typeNode.getContext());
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCReturn(com.sun.tools.javac.tree.JCTree.JCReturn) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JavacTreeMaker(lombok.javac.JavacTreeMaker) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 3 with JCClassDecl

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

the class HandleHelper method handle.

@Override
public void handle(AnnotationValues<Helper> annotation, JCAnnotation ast, JavacNode annotationNode) {
    handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.HELPER_FLAG_USAGE, "@Helper");
    deleteAnnotationIfNeccessary(annotationNode, Helper.class);
    JavacNode annotatedType = annotationNode.up();
    JavacNode containingBlock = annotatedType == null ? null : annotatedType.directUp();
    List<JCStatement> origStatements = getStatementsFromJcNode(containingBlock == null ? null : containingBlock.get());
    if (annotatedType == null || annotatedType.getKind() != Kind.TYPE || origStatements == null) {
        annotationNode.addError("@Helper is legal only on method-local classes.");
        return;
    }
    JCClassDecl annotatedType_ = (JCClassDecl) annotatedType.get();
    Iterator<JCStatement> it = origStatements.iterator();
    while (it.hasNext()) {
        if (it.next() == annotatedType_) {
            break;
        }
    }
    java.util.List<String> knownMethodNames = new ArrayList<String>();
    for (JavacNode ch : annotatedType.down()) {
        if (ch.getKind() != Kind.METHOD)
            continue;
        String n = ch.getName();
        if (n == null || n.isEmpty() || n.charAt(0) == '<')
            continue;
        knownMethodNames.add(n);
    }
    Collections.sort(knownMethodNames);
    final String[] knownMethodNames_ = knownMethodNames.toArray(new String[knownMethodNames.size()]);
    final Name helperName = annotationNode.toName("$" + annotatedType_.name);
    final boolean[] helperUsed = new boolean[1];
    final JavacTreeMaker maker = annotationNode.getTreeMaker();
    TreeVisitor<Void, Void> visitor = new TreeScanner<Void, Void>() {

        @Override
        public Void visitMethodInvocation(MethodInvocationTree node, Void p) {
            JCMethodInvocation jcmi = (JCMethodInvocation) node;
            apply(jcmi);
            return super.visitMethodInvocation(node, p);
        }

        private void apply(JCMethodInvocation jcmi) {
            if (!(jcmi.meth instanceof JCIdent))
                return;
            JCIdent jci = (JCIdent) jcmi.meth;
            if (Arrays.binarySearch(knownMethodNames_, jci.name.toString()) < 0)
                return;
            jcmi.meth = maker.Select(maker.Ident(helperName), jci.name);
            helperUsed[0] = true;
        }
    };
    while (it.hasNext()) {
        JCStatement stat = it.next();
        stat.accept(visitor, null);
    }
    if (!helperUsed[0]) {
        annotationNode.addWarning("No methods of this helper class are ever used.");
        return;
    }
    ListBuffer<JCStatement> newStatements = new ListBuffer<JCStatement>();
    boolean mark = false;
    for (JCStatement stat : origStatements) {
        newStatements.append(stat);
        if (mark || stat != annotatedType_)
            continue;
        mark = true;
        JCExpression init = maker.NewClass(null, List.<JCExpression>nil(), maker.Ident(annotatedType_.name), List.<JCExpression>nil(), null);
        JCExpression varType = maker.Ident(annotatedType_.name);
        JCVariableDecl decl = maker.VarDef(maker.Modifiers(Flags.FINAL), helperName, varType, init);
        newStatements.append(decl);
    }
    setStatementsOfJcNode(containingBlock.get(), newStatements.toList());
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) JavacTreeMaker(lombok.javac.JavacTreeMaker) ListBuffer(com.sun.tools.javac.util.ListBuffer) ArrayList(java.util.ArrayList) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name) JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode) TreeScanner(com.sun.source.util.TreeScanner) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree)

Example 4 with JCClassDecl

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

the class HandleLog method processAnnotation.

public static void processAnnotation(LoggingFramework framework, AnnotationValues<?> annotation, JavacNode annotationNode, String loggerTopic) {
    deleteAnnotationIfNeccessary(annotationNode, framework.getAnnotationClass());
    JavacNode typeNode = annotationNode.up();
    switch(typeNode.getKind()) {
        case TYPE:
            String logFieldName = annotationNode.getAst().readConfiguration(ConfigurationKeys.LOG_ANY_FIELD_NAME);
            if (logFieldName == null)
                logFieldName = "log";
            boolean useStatic = !Boolean.FALSE.equals(annotationNode.getAst().readConfiguration(ConfigurationKeys.LOG_ANY_FIELD_IS_STATIC));
            if ((((JCClassDecl) typeNode.get()).mods.flags & Flags.INTERFACE) != 0) {
                annotationNode.addError("@Log is legal only on classes and enums.");
                return;
            }
            if (fieldExists(logFieldName, typeNode) != MemberExistsResult.NOT_EXISTS) {
                annotationNode.addWarning("Field '" + logFieldName + "' already exists.");
                return;
            }
            JCFieldAccess loggingType = selfType(typeNode);
            createField(framework, typeNode, loggingType, annotationNode.get(), logFieldName, useStatic, loggerTopic);
            break;
        default:
            annotationNode.addError("@Log is legal only on types.");
            break;
    }
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacNode(lombok.javac.JavacNode) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess)

Example 5 with JCClassDecl

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

the class HandleToString method getTypeName.

public static String getTypeName(JavacNode typeNode) {
    String typeName = ((JCClassDecl) typeNode.get()).name.toString();
    JavacNode upType = typeNode.up();
    while (upType.getKind() == Kind.TYPE) {
        typeName = ((JCClassDecl) upType.get()).name.toString() + "." + typeName;
        upType = upType.up();
    }
    return typeName;
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacNode(lombok.javac.JavacNode) ToString(lombok.ToString)

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