Search in sources :

Example 6 with JCModifiers

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

the class HandleEqualsAndHashCode method createCanEqual.

public JCMethodDecl createCanEqual(JavacNode typeNode, JCTree source, List<JCAnnotation> onParam) {
    /* protected boolean canEqual(final java.lang.Object other) {
		 *     return other instanceof Outer.Inner.MyType;
		 * }
		 */
    JavacTreeMaker maker = typeNode.getTreeMaker();
    JCModifiers mods = maker.Modifiers(Flags.PROTECTED, List.<JCAnnotation>nil());
    JCExpression returnType = maker.TypeIdent(CTC_BOOLEAN);
    Name canEqualName = typeNode.toName("canEqual");
    JCExpression objectType = genJavaLangTypeRef(typeNode, "Object");
    Name otherName = typeNode.toName("other");
    long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext());
    List<JCVariableDecl> params = List.of(maker.VarDef(maker.Modifiers(flags, onParam), otherName, objectType, null));
    JCBlock body = maker.Block(0, List.<JCStatement>of(maker.Return(maker.TypeTest(maker.Ident(otherName), createTypeReference(typeNode, false)))));
    return recursiveSetGeneratedBy(maker.MethodDef(mods, canEqualName, returnType, List.<JCTypeParameter>nil(), params, List.<JCExpression>nil(), body, null), source, typeNode.getContext());
}
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) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name)

Example 7 with JCModifiers

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

the class HandleUtilityClass method createPrivateDefaultConstructor.

private void createPrivateDefaultConstructor(JavacNode typeNode) {
    JavacTreeMaker maker = typeNode.getTreeMaker();
    JCModifiers mods = maker.Modifiers(Flags.PRIVATE, List.<JCAnnotation>nil());
    Name name = typeNode.toName("<init>");
    JCBlock block = maker.Block(0L, createThrowStatement(typeNode, maker));
    JCMethodDecl methodDef = maker.MethodDef(mods, name, null, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), block, null);
    JCMethodDecl constructor = recursiveSetGeneratedBy(methodDef, typeNode.get(), typeNode.getContext());
    JavacHandlerUtil.injectMethod(typeNode, constructor, List.<Type>nil(), Javac.createVoidType(typeNode.getSymbolTable(), CTC_VOID));
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) Name(com.sun.tools.javac.util.Name)

Example 8 with JCModifiers

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

the class HandleValue method handle.

@Override
public void handle(AnnotationValues<Value> annotation, JCAnnotation ast, JavacNode annotationNode) {
    @SuppressWarnings("deprecation") Class<? extends Annotation> oldExperimentalValue = lombok.experimental.Value.class;
    handleFlagUsage(annotationNode, ConfigurationKeys.VALUE_FLAG_USAGE, "@Value");
    deleteAnnotationIfNeccessary(annotationNode, Value.class, oldExperimentalValue);
    JavacNode typeNode = annotationNode.up();
    boolean notAClass = !isClass(typeNode);
    if (notAClass) {
        annotationNode.addError("@Value is only supported on a class.");
        return;
    }
    String staticConstructorName = annotation.getInstance().staticConstructor();
    if (!hasAnnotationAndDeleteIfNeccessary(NonFinal.class, typeNode)) {
        JCModifiers jcm = ((JCClassDecl) typeNode.get()).mods;
        if ((jcm.flags & Flags.FINAL) == 0) {
            jcm.flags |= Flags.FINAL;
            typeNode.rebuild();
        }
    }
    new HandleFieldDefaults().generateFieldDefaultsForType(typeNode, annotationNode, AccessLevel.PRIVATE, true, true);
    // TODO move this to the end OR move it to the top in eclipse.
    new HandleConstructor().generateAllArgsConstructor(typeNode, AccessLevel.PUBLIC, staticConstructorName, SkipIfConstructorExists.YES, annotationNode);
    new HandleGetter().generateGetterForType(typeNode, annotationNode, AccessLevel.PUBLIC, true);
    new HandleEqualsAndHashCode().generateEqualsAndHashCodeForType(typeNode, annotationNode);
    new HandleToString().generateToStringForType(typeNode, annotationNode);
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacNode(lombok.javac.JavacNode) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) Value(lombok.Value) NonFinal(lombok.experimental.NonFinal)

Example 9 with JCModifiers

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

the class JavacGuavaSingularizer method generatePluralMethod.

protected void generatePluralMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) {
    List<JCTypeParameter> typeParams = List.nil();
    List<JCExpression> thrown = List.nil();
    JCModifiers mods = maker.Modifiers(Flags.PUBLIC);
    ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
    statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, source));
    JCExpression thisDotFieldDotAddAll = chainDots(builderType, "this", data.getPluralName().toString(), getAddMethodName() + "All");
    JCExpression invokeAddAll = maker.Apply(List.<JCExpression>nil(), thisDotFieldDotAddAll, List.<JCExpression>of(maker.Ident(data.getPluralName())));
    statements.append(maker.Exec(invokeAddAll));
    if (returnStatement != null)
        statements.append(returnStatement);
    JCBlock body = maker.Block(0, statements.toList());
    Name methodName = data.getPluralName();
    long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext());
    if (!fluent)
        methodName = builderType.toName(HandlerUtil.buildAccessorName(getAddMethodName() + "All", methodName.toString()));
    JCExpression paramType;
    String aaTypeName = getAddAllTypeName();
    if (aaTypeName.startsWith("java.lang.") && aaTypeName.indexOf('.', 11) == -1) {
        paramType = genJavaLangTypeRef(builderType, aaTypeName.substring(10));
    } else {
        paramType = chainDotsString(builderType, aaTypeName);
    }
    paramType = addTypeArgs(getTypeArgumentsCount(), true, builderType, paramType, data.getTypeArgs(), source);
    JCVariableDecl param = maker.VarDef(maker.Modifiers(paramFlags), data.getPluralName(), paramType, null);
    JCMethodDecl method = maker.MethodDef(mods, methodName, returnType, typeParams, List.of(param), thrown, body, null);
    injectMethod(builderType, method);
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) ListBuffer(com.sun.tools.javac.util.ListBuffer) 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) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers)

Example 10 with JCModifiers

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

the class JavacGuavaSingularizer method generateSingularMethod.

void generateSingularMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) {
    List<JCTypeParameter> typeParams = List.nil();
    List<JCExpression> thrown = List.nil();
    LombokImmutableList<String> suffixes = getArgumentSuffixes();
    Name[] names = new Name[suffixes.size()];
    for (int i = 0; i < suffixes.size(); i++) {
        String s = suffixes.get(i);
        Name n = data.getSingularName();
        names[i] = s.isEmpty() ? n : builderType.toName(s);
    }
    JCModifiers mods = maker.Modifiers(Flags.PUBLIC);
    ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
    statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, source));
    JCExpression thisDotFieldDotAdd = chainDots(builderType, "this", data.getPluralName().toString(), getAddMethodName());
    ListBuffer<JCExpression> invokeAddExprBuilder = new ListBuffer<JCExpression>();
    for (int i = 0; i < suffixes.size(); i++) {
        invokeAddExprBuilder.append(maker.Ident(names[i]));
    }
    List<JCExpression> invokeAddExpr = invokeAddExprBuilder.toList();
    JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotFieldDotAdd, invokeAddExpr);
    statements.append(maker.Exec(invokeAdd));
    if (returnStatement != null)
        statements.append(returnStatement);
    JCBlock body = maker.Block(0, statements.toList());
    Name methodName = data.getSingularName();
    long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext());
    if (!fluent)
        methodName = builderType.toName(HandlerUtil.buildAccessorName(getAddMethodName(), methodName.toString()));
    ListBuffer<JCVariableDecl> params = new ListBuffer<JCVariableDecl>();
    for (int i = 0; i < suffixes.size(); i++) {
        JCExpression pt = cloneParamType(i, maker, data.getTypeArgs(), builderType, source);
        JCVariableDecl p = maker.VarDef(maker.Modifiers(paramFlags), names[i], pt, null);
        params.append(p);
    }
    JCMethodDecl method = maker.MethodDef(mods, methodName, returnType, typeParams, params.toList(), thrown, body, null);
    injectMethod(builderType, method);
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) ListBuffer(com.sun.tools.javac.util.ListBuffer) 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) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers)

Aggregations

JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)25 Name (com.sun.tools.javac.util.Name)20 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)18 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)17 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)17 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)17 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)14 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)13 ListBuffer (com.sun.tools.javac.util.ListBuffer)11 JavacTreeMaker (lombok.javac.JavacTreeMaker)10 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)9 JavacNode (lombok.javac.JavacNode)7 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)5 JCArrayTypeTree (com.sun.tools.javac.tree.JCTree.JCArrayTypeTree)4 JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)4 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)4 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)2 JCTree (com.sun.tools.javac.tree.JCTree)2 JCAssign (com.sun.tools.javac.tree.JCTree.JCAssign)2 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)2