Search in sources :

Example 1 with IdentifierName

use of lombok.core.configuration.IdentifierName in project lombok by rzwitserloot.

the class HandleFieldNameConstants method createInnerTypeFieldNameConstants.

private void createInnerTypeFieldNameConstants(JavacNode typeNode, JavacNode errorNode, AccessLevel level, java.util.List<JavacNode> fields, boolean asEnum, IdentifierName innerTypeName, boolean uppercase) {
    if (fields.isEmpty())
        return;
    JavacTreeMaker maker = typeNode.getTreeMaker();
    JCModifiers mods = maker.Modifiers(toJavacModifier(level) | (asEnum ? Flags.ENUM : Flags.STATIC | Flags.FINAL));
    Name fieldsName = typeNode.toName(innerTypeName.getName());
    JavacNode fieldsType = findInnerClass(typeNode, innerTypeName.getName());
    boolean genConstr = false;
    if (fieldsType == null) {
        JCClassDecl innerType = maker.ClassDef(mods, fieldsName, List.<JCTypeParameter>nil(), null, List.<JCExpression>nil(), List.<JCTree>nil());
        fieldsType = injectType(typeNode, innerType);
        recursiveSetGeneratedBy(innerType, errorNode);
        genConstr = true;
    } else {
        JCClassDecl builderTypeDeclaration = (JCClassDecl) fieldsType.get();
        long f = builderTypeDeclaration.getModifiers().flags;
        if (asEnum && (f & Flags.ENUM) == 0) {
            errorNode.addError("Existing " + innerTypeName + " must be declared as an 'enum'.");
            return;
        }
        if (!asEnum && (f & Flags.STATIC) == 0) {
            errorNode.addError("Existing " + innerTypeName + " must be declared as a 'static class'.");
            return;
        }
        genConstr = constructorExists(fieldsType) == MemberExistsResult.NOT_EXISTS;
    }
    if (genConstr) {
        JCModifiers genConstrMods = maker.Modifiers(Flags.GENERATEDCONSTR | (asEnum ? 0L : Flags.PRIVATE));
        JCBlock genConstrBody = maker.Block(0L, List.<JCStatement>of(maker.Exec(maker.Apply(List.<JCExpression>nil(), maker.Ident(typeNode.toName("super")), List.<JCExpression>nil()))));
        JCMethodDecl c = maker.MethodDef(genConstrMods, typeNode.toName("<init>"), null, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), genConstrBody, null);
        recursiveSetGeneratedBy(c, errorNode);
        injectMethod(fieldsType, c);
    }
    java.util.List<JCVariableDecl> generated = new ArrayList<JCVariableDecl>();
    for (JavacNode field : fields) {
        Name fName = ((JCVariableDecl) field.get()).name;
        if (uppercase)
            fName = typeNode.toName(HandlerUtil.camelCaseToConstant(fName.toString()));
        if (fieldExists(fName.toString(), fieldsType) != MemberExistsResult.NOT_EXISTS)
            continue;
        JCModifiers constantValueMods = maker.Modifiers(Flags.PUBLIC | Flags.STATIC | Flags.FINAL | (asEnum ? Flags.ENUM : 0L));
        JCExpression returnType;
        JCExpression init;
        if (asEnum) {
            returnType = maker.Ident(fieldsName);
            init = maker.NewClass(null, List.<JCExpression>nil(), maker.Ident(fieldsName), List.<JCExpression>nil(), null);
        } else {
            returnType = chainDots(field, "java", "lang", "String");
            init = maker.Literal(field.getName());
        }
        JCVariableDecl constantField = maker.VarDef(constantValueMods, fName, returnType, init);
        injectField(fieldsType, constantField, false, true);
        setGeneratedBy(constantField, errorNode);
        generated.add(constantField);
    }
    for (JCVariableDecl cf : generated) recursiveSetGeneratedBy(cf, errorNode);
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) ArrayList(java.util.ArrayList) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) IdentifierName(lombok.core.configuration.IdentifierName) Name(com.sun.tools.javac.util.Name) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers)

Example 2 with IdentifierName

use of lombok.core.configuration.IdentifierName in project lombok by rzwitserloot.

the class HandleFieldNameConstants method handle.

public void handle(AnnotationValues<FieldNameConstants> annotation, JCAnnotation ast, JavacNode annotationNode) {
    handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.FIELD_NAME_CONSTANTS_FLAG_USAGE, "@FieldNameConstants");
    deleteAnnotationIfNeccessary(annotationNode, FieldNameConstants.class);
    deleteImportFromCompilationUnit(annotationNode, "lombok.AccessLevel");
    JavacNode node = annotationNode.up();
    FieldNameConstants annotationInstance = annotation.getInstance();
    AccessLevel level = annotationInstance.level();
    boolean asEnum = annotationInstance.asEnum();
    boolean usingLombokv1_18_2 = annotation.isExplicit("prefix") || annotation.isExplicit("suffix") || node.getKind() == Kind.FIELD;
    if (usingLombokv1_18_2) {
        annotationNode.addError("@FieldNameConstants has been redesigned in lombok v1.18.4; please upgrade your project dependency on lombok. See https://projectlombok.org/features/experimental/FieldNameConstants for more information.");
        return;
    }
    if (level == AccessLevel.NONE) {
        annotationNode.addWarning("AccessLevel.NONE is not compatible with @FieldNameConstants. If you don't want the inner type, simply remove @FieldNameConstants.");
        return;
    }
    IdentifierName innerTypeName;
    try {
        innerTypeName = IdentifierName.valueOf(annotationInstance.innerTypeName());
    } catch (IllegalArgumentException e) {
        annotationNode.addError("InnerTypeName " + annotationInstance.innerTypeName() + " is not a valid Java identifier.");
        return;
    }
    if (innerTypeName == null)
        innerTypeName = annotationNode.getAst().readConfiguration(ConfigurationKeys.FIELD_NAME_CONSTANTS_INNER_TYPE_NAME);
    if (innerTypeName == null)
        innerTypeName = FIELDS;
    Boolean uppercase = annotationNode.getAst().readConfiguration(ConfigurationKeys.FIELD_NAME_CONSTANTS_UPPERCASE);
    if (uppercase == null)
        uppercase = false;
    generateFieldNameConstantsForType(node, annotationNode, level, asEnum, innerTypeName, annotationInstance.onlyExplicitlyIncluded(), uppercase);
}
Also used : JavacNode(lombok.javac.JavacNode) FieldNameConstants(lombok.experimental.FieldNameConstants) AccessLevel(lombok.AccessLevel) IdentifierName(lombok.core.configuration.IdentifierName)

Example 3 with IdentifierName

use of lombok.core.configuration.IdentifierName in project lombok by rzwitserloot.

the class HandleLog method processAnnotation.

public static void processAnnotation(LoggingFramework framework, AnnotationValues<?> annotation, JavacNode annotationNode) {
    deleteAnnotationIfNeccessary(annotationNode, framework.getAnnotationClass());
    JavacNode typeNode = annotationNode.up();
    switch(typeNode.getKind()) {
        case TYPE:
            IdentifierName 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(framework.getAnnotationAsString() + " is legal only on classes and enums.");
                return;
            }
            if (fieldExists(logFieldName.getName(), typeNode) != MemberExistsResult.NOT_EXISTS) {
                annotationNode.addWarning("Field '" + logFieldName + "' already exists.");
                return;
            }
            if (isRecord(typeNode) && !useStatic) {
                annotationNode.addError("Logger fields must be static in records.");
                return;
            }
            if (useStatic && !isStaticAllowed(typeNode)) {
                annotationNode.addError(framework.getAnnotationAsString() + " is not supported on non-static nested classes.");
                return;
            }
            Object valueGuess = annotation.getValueGuess("topic");
            JCExpression loggerTopic = (JCExpression) annotation.getActualExpression("topic");
            if (valueGuess instanceof String && ((String) valueGuess).trim().isEmpty())
                loggerTopic = null;
            if (framework.getDeclaration().getParametersWithTopic() == null && loggerTopic != null) {
                annotationNode.addError(framework.getAnnotationAsString() + " does not allow a topic.");
                loggerTopic = null;
            }
            if (framework.getDeclaration().getParametersWithoutTopic() == null && loggerTopic == null) {
                annotationNode.addError(framework.getAnnotationAsString() + " requires a topic.");
                loggerTopic = typeNode.getTreeMaker().Literal("");
            }
            JCFieldAccess loggingType = selfType(typeNode);
            createField(framework, typeNode, loggingType, annotationNode, logFieldName.getName(), useStatic, loggerTopic);
            break;
        default:
            annotationNode.addError("@Log is legal only on types.");
            break;
    }
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) IdentifierName(lombok.core.configuration.IdentifierName)

Example 4 with IdentifierName

use of lombok.core.configuration.IdentifierName in project lombok by rzwitserloot.

the class HandleLog method processAnnotation.

public static void processAnnotation(LoggingFramework framework, AnnotationValues<? extends java.lang.annotation.Annotation> annotation, Annotation source, EclipseNode annotationNode) {
    EclipseNode owner = annotationNode.up();
    switch(owner.getKind()) {
        case TYPE:
            IdentifierName 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));
            TypeDeclaration typeDecl = null;
            if (owner.get() instanceof TypeDeclaration)
                typeDecl = (TypeDeclaration) owner.get();
            int modifiers = typeDecl == null ? 0 : typeDecl.modifiers;
            boolean notAClass = (modifiers & (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation)) != 0;
            if (typeDecl == null || notAClass) {
                annotationNode.addError(framework.getAnnotationAsString() + " is legal only on classes and enums.");
                return;
            }
            if (fieldExists(logFieldName.getName(), owner) != MemberExistsResult.NOT_EXISTS) {
                annotationNode.addWarning("Field '" + logFieldName + "' already exists.");
                return;
            }
            if (isRecord(owner) && !useStatic) {
                annotationNode.addError("Logger fields must be static in records.");
                return;
            }
            if (useStatic && !isStaticAllowed(owner)) {
                annotationNode.addError(framework.getAnnotationAsString() + " is not supported on non-static nested classes.");
                return;
            }
            Object valueGuess = annotation.getValueGuess("topic");
            Expression loggerTopic = (Expression) annotation.getActualExpression("topic");
            if (valueGuess instanceof String && ((String) valueGuess).trim().isEmpty())
                loggerTopic = null;
            if (framework.getDeclaration().getParametersWithTopic() == null && loggerTopic != null) {
                annotationNode.addError(framework.getAnnotationAsString() + " does not allow a topic.");
                loggerTopic = null;
            }
            if (framework.getDeclaration().getParametersWithoutTopic() == null && loggerTopic == null) {
                annotationNode.addError(framework.getAnnotationAsString() + " requires a topic.");
                loggerTopic = new StringLiteral(new char[] {}, 0, 0, 0);
            }
            ClassLiteralAccess loggingType = selfType(owner, source);
            FieldDeclaration fieldDeclaration = createField(framework, source, loggingType, logFieldName.getName(), useStatic, loggerTopic);
            fieldDeclaration.traverse(new SetGeneratedByVisitor(source), typeDecl.staticInitializerScope);
            // TODO temporary workaround for issue 290. https://github.com/projectlombok/lombok/issues/290
            // injectFieldSuppressWarnings(owner, fieldDeclaration);
            injectField(owner, fieldDeclaration);
            owner.rebuild();
            break;
        default:
            break;
    }
}
Also used : StringLiteral(org.eclipse.jdt.internal.compiler.ast.StringLiteral) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) EclipseNode(lombok.eclipse.EclipseNode) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) ClassLiteralAccess(org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration) IdentifierName(lombok.core.configuration.IdentifierName)

Example 5 with IdentifierName

use of lombok.core.configuration.IdentifierName in project lombok by rzwitserloot.

the class HandleFieldNameConstants method handle.

@Override
public void handle(AnnotationValues<FieldNameConstants> annotation, Annotation ast, EclipseNode annotationNode) {
    handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.FIELD_NAME_CONSTANTS_FLAG_USAGE, "@FieldNameConstants");
    EclipseNode node = annotationNode.up();
    FieldNameConstants annotationInstance = annotation.getInstance();
    AccessLevel level = annotationInstance.level();
    boolean asEnum = annotationInstance.asEnum();
    boolean usingLombokv1_18_2 = annotation.isExplicit("prefix") || annotation.isExplicit("suffix") || node.getKind() == Kind.FIELD;
    if (usingLombokv1_18_2) {
        annotationNode.addError("@FieldNameConstants has been redesigned in lombok v1.18.4; please upgrade your project dependency on lombok. See https://projectlombok.org/features/experimental/FieldNameConstants for more information.");
        return;
    }
    if (level == AccessLevel.NONE) {
        annotationNode.addWarning("AccessLevel.NONE is not compatible with @FieldNameConstants. If you don't want the inner type, simply remove FieldNameConstants.");
        return;
    }
    IdentifierName innerTypeName;
    try {
        innerTypeName = IdentifierName.valueOf(annotationInstance.innerTypeName());
    } catch (IllegalArgumentException e) {
        annotationNode.addError("InnerTypeName " + annotationInstance.innerTypeName() + " is not a valid Java identifier.");
        return;
    }
    if (innerTypeName == null)
        innerTypeName = annotationNode.getAst().readConfiguration(ConfigurationKeys.FIELD_NAME_CONSTANTS_INNER_TYPE_NAME);
    if (innerTypeName == null)
        innerTypeName = FIELDS;
    Boolean uppercase = annotationNode.getAst().readConfiguration(ConfigurationKeys.FIELD_NAME_CONSTANTS_UPPERCASE);
    if (uppercase == null)
        uppercase = false;
    generateFieldNameConstantsForType(node, annotationNode, level, asEnum, innerTypeName, annotationInstance.onlyExplicitlyIncluded(), uppercase);
}
Also used : FieldNameConstants(lombok.experimental.FieldNameConstants) EclipseNode(lombok.eclipse.EclipseNode) AccessLevel(lombok.AccessLevel) IdentifierName(lombok.core.configuration.IdentifierName)

Aggregations

IdentifierName (lombok.core.configuration.IdentifierName)5 JavacNode (lombok.javac.JavacNode)3 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)2 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)2 AccessLevel (lombok.AccessLevel)2 EclipseNode (lombok.eclipse.EclipseNode)2 FieldNameConstants (lombok.experimental.FieldNameConstants)2 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)1 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)1 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)1 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)1 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)1 Name (com.sun.tools.javac.util.Name)1 ArrayList (java.util.ArrayList)1 JavacTreeMaker (lombok.javac.JavacTreeMaker)1 ClassLiteralAccess (org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess)1 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)1 FieldDeclaration (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)1 StringLiteral (org.eclipse.jdt.internal.compiler.ast.StringLiteral)1 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)1