Search in sources :

Example 6 with JCExpression

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

the class HandleBuilder method generateToBuilderMethod.

private JCMethodDecl generateToBuilderMethod(String toBuilderMethodName, String builderClassName, JavacNode type, List<JCTypeParameter> typeParams, java.util.List<BuilderFieldData> builderFields, boolean fluent, JCAnnotation ast) {
    // return new ThingieBuilder<A, B>().setA(this.a).setB(this.b);
    JavacTreeMaker maker = type.getTreeMaker();
    ListBuffer<JCExpression> typeArgs = new ListBuffer<JCExpression>();
    for (JCTypeParameter typeParam : typeParams) {
        typeArgs.append(maker.Ident(typeParam.name));
    }
    JCExpression call = maker.NewClass(null, List.<JCExpression>nil(), namePlusTypeParamsToTypeReference(maker, type.toName(builderClassName), typeParams), List.<JCExpression>nil(), null);
    JCExpression invoke = call;
    for (BuilderFieldData bfd : builderFields) {
        Name setterName = fluent ? bfd.name : type.toName(HandlerUtil.buildAccessorName("set", bfd.name.toString()));
        JCExpression arg;
        if (bfd.obtainVia == null || !bfd.obtainVia.field().isEmpty()) {
            arg = maker.Select(maker.Ident(type.toName("this")), bfd.obtainVia == null ? bfd.rawName : type.toName(bfd.obtainVia.field()));
        } else {
            if (bfd.obtainVia.isStatic()) {
                JCExpression c = maker.Select(maker.Ident(type.toName(type.getName())), type.toName(bfd.obtainVia.method()));
                arg = maker.Apply(List.<JCExpression>nil(), c, List.<JCExpression>of(maker.Ident(type.toName("this"))));
            } else {
                JCExpression c = maker.Select(maker.Ident(type.toName("this")), type.toName(bfd.obtainVia.method()));
                arg = maker.Apply(List.<JCExpression>nil(), c, List.<JCExpression>nil());
            }
        }
        invoke = maker.Apply(List.<JCExpression>nil(), maker.Select(invoke, setterName), List.of(arg));
    }
    JCStatement statement = maker.Return(invoke);
    JCBlock body = maker.Block(0, List.<JCStatement>of(statement));
    return maker.MethodDef(maker.Modifiers(Flags.PUBLIC), type.toName(toBuilderMethodName), namePlusTypeParamsToTypeReference(maker, type.toName(builderClassName), typeParams), List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null);
}
Also used : JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) Name(com.sun.tools.javac.util.Name)

Example 7 with JCExpression

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

the class JavacResolution method typeToJCTree.

private static JCExpression typeToJCTree(Type type, JavacAST ast, boolean allowCompound, boolean allowVoid) throws TypeNotConvertibleException {
    int dims = 0;
    Type type0 = type;
    while (type0 instanceof ArrayType) {
        dims++;
        type0 = ((ArrayType) type0).elemtype;
    }
    JCExpression result = typeToJCTree0(type0, ast, allowCompound, allowVoid);
    while (dims > 0) {
        result = ast.getTreeMaker().TypeArray(result);
        dims--;
    }
    return result;
}
Also used : ArrayType(com.sun.tools.javac.code.Type.ArrayType) ClassType(com.sun.tools.javac.code.Type.ClassType) CapturedType(com.sun.tools.javac.code.Type.CapturedType) ArrayType(com.sun.tools.javac.code.Type.ArrayType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression)

Example 8 with JCExpression

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

the class JavacResolution method createJavaLangObject.

public static JCExpression createJavaLangObject(JavacAST ast) {
    JavacTreeMaker maker = ast.getTreeMaker();
    JCExpression out = maker.Ident(ast.toName("java"));
    out = maker.Select(out, ast.toName("lang"));
    out = maker.Select(out, ast.toName("Object"));
    return out;
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression)

Example 9 with JCExpression

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

the class HandleConstructor method addConstructorProperties.

public static void addConstructorProperties(JCModifiers mods, JavacNode node, List<JavacNode> fields) {
    if (fields.isEmpty())
        return;
    JavacTreeMaker maker = node.getTreeMaker();
    JCExpression constructorPropertiesType = chainDots(node, "java", "beans", "ConstructorProperties");
    ListBuffer<JCExpression> fieldNames = new ListBuffer<JCExpression>();
    for (JavacNode field : fields) {
        Name fieldName = removePrefixFromField(field);
        fieldNames.append(maker.Literal(fieldName.toString()));
    }
    JCExpression fieldNamesArray = maker.NewArray(null, List.<JCExpression>nil(), fieldNames.toList());
    JCAnnotation annotation = maker.Annotation(constructorPropertiesType, List.of(fieldNamesArray));
    mods.annotations = mods.annotations.append(annotation);
}
Also used : JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) Name(com.sun.tools.javac.util.Name)

Example 10 with JCExpression

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

the class HandleDelegate method createDelegateMethod.

public JCMethodDecl createDelegateMethod(MethodSig sig, JavacNode annotation, Name delegateName, DelegateReceiver delegateReceiver) throws TypeNotConvertibleException, CantMakeDelegates {
    /* public <T, U, ...> ReturnType methodName(ParamType1 name1, ParamType2 name2, ...) throws T1, T2, ... {
		 *      (return) delegate.<T, U>methodName(name1, name2);
		 *  }
		 */
    checkConflictOfTypeVarNames(sig, annotation);
    JavacTreeMaker maker = annotation.getTreeMaker();
    com.sun.tools.javac.util.List<JCAnnotation> annotations;
    if (sig.isDeprecated) {
        annotations = com.sun.tools.javac.util.List.of(maker.Annotation(genJavaLangTypeRef(annotation, "Deprecated"), com.sun.tools.javac.util.List.<JCExpression>nil()));
    } else {
        annotations = com.sun.tools.javac.util.List.nil();
    }
    JCModifiers mods = maker.Modifiers(PUBLIC, annotations);
    JCExpression returnType = JavacResolution.typeToJCTree((Type) sig.type.getReturnType(), annotation.getAst(), true);
    boolean useReturn = sig.type.getReturnType().getKind() != TypeKind.VOID;
    ListBuffer<JCVariableDecl> params = sig.type.getParameterTypes().isEmpty() ? null : new ListBuffer<JCVariableDecl>();
    ListBuffer<JCExpression> args = sig.type.getParameterTypes().isEmpty() ? null : new ListBuffer<JCExpression>();
    ListBuffer<JCExpression> thrown = sig.type.getThrownTypes().isEmpty() ? null : new ListBuffer<JCExpression>();
    ListBuffer<JCTypeParameter> typeParams = sig.type.getTypeVariables().isEmpty() ? null : new ListBuffer<JCTypeParameter>();
    ListBuffer<JCExpression> typeArgs = sig.type.getTypeVariables().isEmpty() ? null : new ListBuffer<JCExpression>();
    Types types = Types.instance(annotation.getContext());
    for (TypeMirror param : sig.type.getTypeVariables()) {
        Name name = ((TypeVar) param).tsym.name;
        ListBuffer<JCExpression> bounds = new ListBuffer<JCExpression>();
        for (Type type : types.getBounds((TypeVar) param)) {
            bounds.append(JavacResolution.typeToJCTree(type, annotation.getAst(), true));
        }
        typeParams.append(maker.TypeParameter(name, bounds.toList()));
        typeArgs.append(maker.Ident(name));
    }
    for (TypeMirror ex : sig.type.getThrownTypes()) {
        thrown.append(JavacResolution.typeToJCTree((Type) ex, annotation.getAst(), true));
    }
    int idx = 0;
    String[] paramNames = sig.getParameterNames();
    boolean varargs = sig.elem.isVarArgs();
    for (TypeMirror param : sig.type.getParameterTypes()) {
        long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, annotation.getContext());
        JCModifiers paramMods = maker.Modifiers(flags);
        Name name = annotation.toName(paramNames[idx++]);
        if (varargs && idx == paramNames.length) {
            paramMods.flags |= VARARGS;
        }
        params.append(maker.VarDef(paramMods, name, JavacResolution.typeToJCTree((Type) param, annotation.getAst(), true), null));
        args.append(maker.Ident(name));
    }
    JCExpression delegateCall = maker.Apply(toList(typeArgs), maker.Select(delegateReceiver.get(annotation, delegateName), sig.name), toList(args));
    JCStatement body = useReturn ? maker.Return(delegateCall) : maker.Exec(delegateCall);
    JCBlock bodyBlock = maker.Block(0, com.sun.tools.javac.util.List.of(body));
    return recursiveSetGeneratedBy(maker.MethodDef(mods, sig.name, returnType, toList(typeParams), toList(params), toList(thrown), bodyBlock, null), annotation.get(), annotation.getContext());
}
Also used : JavacTypes(com.sun.tools.javac.model.JavacTypes) Types(com.sun.tools.javac.code.Types) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) Name(com.sun.tools.javac.util.Name) TypeMirror(javax.lang.model.type.TypeMirror) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) ClassType(com.sun.tools.javac.code.Type.ClassType) Type(com.sun.tools.javac.code.Type) ExecutableType(javax.lang.model.type.ExecutableType) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers)

Aggregations

JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)311 JCTree (com.sun.tools.javac.tree.JCTree)95 Type (com.redhat.ceylon.model.typechecker.model.Type)85 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)81 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)67 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)59 ListBuffer (com.sun.tools.javac.util.ListBuffer)54 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)39 Name (com.sun.tools.javac.util.Name)39 SyntheticName (com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName)37 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)35 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)34 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)34 JavacTreeMaker (lombok.javac.JavacTreeMaker)33 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)30 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)29 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)27 Function (com.redhat.ceylon.model.typechecker.model.Function)26 Parameter (com.redhat.ceylon.model.typechecker.model.Parameter)26 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)26