Search in sources :

Example 1 with JCNewClass

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

the class HandleWither method createWither.

public JCMethodDecl createWither(long access, JavacNode field, JavacTreeMaker maker, JavacNode source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam, boolean makeAbstract) {
    String witherName = toWitherName(field);
    if (witherName == null)
        return null;
    JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
    List<JCAnnotation> nonNulls = findAnnotations(field, NON_NULL_PATTERN);
    List<JCAnnotation> nullables = findAnnotations(field, NULLABLE_PATTERN);
    Name methodName = field.toName(witherName);
    JCExpression returnType = cloneSelfType(field);
    JCBlock methodBody = null;
    long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, field.getContext());
    List<JCAnnotation> annsOnParam = copyAnnotations(onParam).appendList(nonNulls).appendList(nullables);
    JCVariableDecl param = maker.VarDef(maker.Modifiers(flags, annsOnParam), fieldDecl.name, fieldDecl.vartype, null);
    if (!makeAbstract) {
        ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
        JCExpression selfType = cloneSelfType(field);
        if (selfType == null)
            return null;
        ListBuffer<JCExpression> args = new ListBuffer<JCExpression>();
        for (JavacNode child : field.up().down()) {
            if (child.getKind() != Kind.FIELD)
                continue;
            JCVariableDecl childDecl = (JCVariableDecl) child.get();
            // Skip fields that start with $
            if (childDecl.name.toString().startsWith("$"))
                continue;
            long fieldFlags = childDecl.mods.flags;
            // Skip static fields.
            if ((fieldFlags & Flags.STATIC) != 0)
                continue;
            // Skip initialized final fields.
            if (((fieldFlags & Flags.FINAL) != 0) && childDecl.init != null)
                continue;
            if (child.get() == field.get()) {
                args.append(maker.Ident(fieldDecl.name));
            } else {
                args.append(createFieldAccessor(maker, child, FieldAccess.ALWAYS_FIELD));
            }
        }
        JCNewClass newClass = maker.NewClass(null, List.<JCExpression>nil(), selfType, args.toList(), null);
        JCExpression identityCheck = maker.Binary(CTC_EQUAL, createFieldAccessor(maker, field, FieldAccess.ALWAYS_FIELD), maker.Ident(fieldDecl.name));
        JCConditional conditional = maker.Conditional(identityCheck, maker.Ident(field.toName("this")), newClass);
        JCReturn returnStatement = maker.Return(conditional);
        if (nonNulls.isEmpty()) {
            statements.append(returnStatement);
        } else {
            JCStatement nullCheck = generateNullCheck(maker, field, source);
            if (nullCheck != null)
                statements.append(nullCheck);
            statements.append(returnStatement);
        }
        methodBody = maker.Block(0, statements.toList());
    }
    List<JCTypeParameter> methodGenericParams = List.nil();
    List<JCVariableDecl> parameters = List.of(param);
    List<JCExpression> throwsClauses = List.nil();
    JCExpression annotationMethodDefaultValue = null;
    List<JCAnnotation> annsOnMethod = copyAnnotations(onMethod);
    if (isFieldDeprecated(field)) {
        annsOnMethod = annsOnMethod.prepend(maker.Annotation(genJavaLangTypeRef(field, "Deprecated"), List.<JCExpression>nil()));
    }
    if (makeAbstract)
        access = access | Flags.ABSTRACT;
    JCMethodDecl decl = recursiveSetGeneratedBy(maker.MethodDef(maker.Modifiers(access, annsOnMethod), methodName, returnType, methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source.get(), field.getContext());
    copyJavadoc(field, decl, CopyJavadoc.WITHER);
    return decl;
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCReturn(com.sun.tools.javac.tree.JCTree.JCReturn) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCConditional(com.sun.tools.javac.tree.JCTree.JCConditional) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) 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) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 2 with JCNewClass

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

the class PrettyPrinter method printClassMembers.

private void printClassMembers(List<JCTree> members, boolean isEnum, boolean isInterface) {
    Class<?> prefType = null;
    // 1 = normal, 2 = with body, 3 = no enum field yet.
    int typeOfPrevEnumMember = isEnum ? 3 : 0;
    boolean prevWasEnumMember = isEnum;
    for (JCTree member : members) {
        if (typeOfPrevEnumMember == 3 && member instanceof JCMethodDecl && (((JCMethodDecl) member).mods.flags & GENERATEDCONSTR) != 0)
            continue;
        boolean isEnumVar = isEnum && member instanceof JCVariableDecl && (((JCVariableDecl) member).mods.flags & ENUM) != 0;
        if (!isEnumVar && prevWasEnumMember) {
            prevWasEnumMember = false;
            if (typeOfPrevEnumMember == 3)
                align();
            println(";");
        }
        if (isEnumVar) {
            if (prefType != null && prefType != JCVariableDecl.class)
                println();
            switch(typeOfPrevEnumMember) {
                case 1:
                    print(", ");
                    break;
                case 2:
                    println(",");
                    align();
                    break;
            }
            print(member);
            JCTree init = ((JCVariableDecl) member).init;
            typeOfPrevEnumMember = init instanceof JCNewClass && ((JCNewClass) init).def != null ? 2 : 1;
        } else if (member instanceof JCVariableDecl) {
            if (prefType != null && prefType != JCVariableDecl.class)
                println();
            if (isInterface)
                flagMod = -1L & ~(PUBLIC | STATIC | FINAL);
            print(member);
        } else if (member instanceof JCMethodDecl) {
            if ((((JCMethodDecl) member).mods.flags & GENERATEDCONSTR) != 0)
                continue;
            if (prefType != null)
                println();
            if (isInterface)
                flagMod = -1L & ~(PUBLIC | ABSTRACT);
            print(member);
        } else if (member instanceof JCClassDecl) {
            if (prefType != null)
                println();
            if (isInterface)
                flagMod = -1L & ~(PUBLIC | STATIC);
            print(member);
        } else {
            if (prefType != null)
                println();
            print(member);
        }
        prefType = member.getClass();
    }
    if (prevWasEnumMember) {
        prevWasEnumMember = false;
        if (typeOfPrevEnumMember == 3)
            align();
        println(";");
    }
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JCTree(com.sun.tools.javac.tree.JCTree) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 3 with JCNewClass

use of com.sun.tools.javac.tree.JCTree.JCNewClass in project error-prone by google.

the class Matchers method selectedIsInstance.

/**
   * Returns true if the expression is a member access on an instance, rather than a static type.
   * Supports member method invocations and field accesses.
   */
public static Matcher<ExpressionTree> selectedIsInstance() {
    return new Matcher<ExpressionTree>() {

        @Override
        public boolean matches(ExpressionTree expr, VisitorState state) {
            if (!(expr instanceof JCFieldAccess)) {
                // TODO(cushon): throw IllegalArgumentException?
                return false;
            }
            JCExpression selected = ((JCFieldAccess) expr).getExpression();
            if (selected instanceof JCNewClass) {
                return true;
            }
            Symbol sym = ASTHelpers.getSymbol(selected);
            return sym instanceof VarSymbol;
        }
    };
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) InstanceMethodMatcher(com.google.errorprone.matchers.method.MethodMatchers.InstanceMethodMatcher) AnyMethodMatcher(com.google.errorprone.matchers.method.MethodMatchers.AnyMethodMatcher) StaticMethodMatcher(com.google.errorprone.matchers.method.MethodMatchers.StaticMethodMatcher) ConstructorMatcher(com.google.errorprone.matchers.method.MethodMatchers.ConstructorMatcher) VisitorState(com.google.errorprone.VisitorState) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Symbol(com.sun.tools.javac.code.Symbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) ExpressionTree(com.sun.source.tree.ExpressionTree) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 4 with JCNewClass

use of com.sun.tools.javac.tree.JCTree.JCNewClass 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 5 with JCNewClass

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

the class CeylonTransformer method transformAttributeGetter.

public JCExpression transformAttributeGetter(TypedDeclaration declarationModel, final JCExpression expression) {
    final String attrName = declarationModel.getName();
    final String attrClassName = Naming.getAttrClassName(declarationModel, 0);
    JCBlock getterBlock = makeGetterBlock(expression);
    // For everything else generate a getter/setter method
    AttributeDefinitionBuilder builder = AttributeDefinitionBuilder.indirect(this, attrClassName, attrName, declarationModel, declarationModel.isToplevel()).getterBlock(getterBlock).immutable();
    List<JCTree> attr = builder.build();
    JCNewClass newExpr = makeNewClass(attrClassName, false, null);
    JCExpression result = makeLetExpr(naming.temp(), List.<JCStatement>of((JCStatement) attr.get(0)), newExpr);
    return result;
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCTree(com.sun.tools.javac.tree.JCTree) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Aggregations

JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)13 JCTree (com.sun.tools.javac.tree.JCTree)7 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)7 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)5 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)4 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)4 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)3 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)3 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)3 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)3 JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)3 ListBuffer (com.sun.tools.javac.util.ListBuffer)3 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)2 Type (com.redhat.ceylon.model.typechecker.model.Type)2 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)2 Symbol (com.sun.tools.javac.code.Symbol)2 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)2 JCConditional (com.sun.tools.javac.tree.JCTree.JCConditional)2 JCExpressionStatement (com.sun.tools.javac.tree.JCTree.JCExpressionStatement)2 VisitorState (com.google.errorprone.VisitorState)1