Search in sources :

Example 1 with Singular

use of lombok.Singular in project lombok by rzwitserloot.

the class HandleBuilder method getSingularData.

/**
	 * Returns the explicitly requested singular annotation on this node (field
	 * or parameter), or null if there's no {@code @Singular} annotation on it.
	 * 
	 * @param node The node (field or method param) to inspect for its name and potential {@code @Singular} annotation.
	 */
private SingularData getSingularData(JavacNode node) {
    for (JavacNode child : node.down()) {
        if (!annotationTypeMatches(Singular.class, child))
            continue;
        Name pluralName = node.getKind() == Kind.FIELD ? removePrefixFromField(node) : ((JCVariableDecl) node.get()).name;
        AnnotationValues<Singular> ann = createAnnotation(Singular.class, child);
        deleteAnnotationIfNeccessary(child, Singular.class);
        String explicitSingular = ann.getInstance().value();
        if (explicitSingular.isEmpty()) {
            if (Boolean.FALSE.equals(node.getAst().readConfiguration(ConfigurationKeys.SINGULAR_AUTO))) {
                node.addError("The singular must be specified explicitly (e.g. @Singular(\"task\")) because auto singularization is disabled.");
                explicitSingular = pluralName.toString();
            } else {
                explicitSingular = autoSingularize(pluralName.toString());
                if (explicitSingular == null) {
                    node.addError("Can't singularize this name; please specify the singular explicitly (i.e. @Singular(\"sheep\"))");
                    explicitSingular = pluralName.toString();
                }
            }
        }
        Name singularName = node.toName(explicitSingular);
        JCExpression type = null;
        if (node.get() instanceof JCVariableDecl) {
            type = ((JCVariableDecl) node.get()).vartype;
        }
        String name = null;
        List<JCExpression> typeArgs = List.nil();
        if (type instanceof JCTypeApply) {
            typeArgs = ((JCTypeApply) type).arguments;
            type = ((JCTypeApply) type).clazz;
        }
        name = type.toString();
        String targetFqn = JavacSingularsRecipes.get().toQualified(name);
        JavacSingularizer singularizer = JavacSingularsRecipes.get().getSingularizer(targetFqn);
        if (singularizer == null) {
            node.addError("Lombok does not know how to create the singular-form builder methods for type '" + name + "'; they won't be generated.");
            return null;
        }
        return new SingularData(child, singularName, pluralName, typeArgs, targetFqn, singularizer);
    }
    return null;
}
Also used : Singular(lombok.Singular) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode) SingularData(lombok.javac.handlers.JavacSingularsRecipes.SingularData) JCTypeApply(com.sun.tools.javac.tree.JCTree.JCTypeApply) JavacSingularizer(lombok.javac.handlers.JavacSingularsRecipes.JavacSingularizer) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name)

Example 2 with Singular

use of lombok.Singular in project lombok by rzwitserloot.

the class HandleBuilder method getSingularData.

/**
	 * Returns the explicitly requested singular annotation on this node (field
	 * or parameter), or null if there's no {@code @Singular} annotation on it.
	 * 
	 * @param node The node (field or method param) to inspect for its name and potential {@code @Singular} annotation.
	 */
private SingularData getSingularData(EclipseNode node, ASTNode source) {
    for (EclipseNode child : node.down()) {
        if (!annotationTypeMatches(Singular.class, child))
            continue;
        char[] pluralName = node.getKind() == Kind.FIELD ? removePrefixFromField(node) : ((AbstractVariableDeclaration) node.get()).name;
        AnnotationValues<Singular> ann = createAnnotation(Singular.class, child);
        String explicitSingular = ann.getInstance().value();
        if (explicitSingular.isEmpty()) {
            if (Boolean.FALSE.equals(node.getAst().readConfiguration(ConfigurationKeys.SINGULAR_AUTO))) {
                node.addError("The singular must be specified explicitly (e.g. @Singular(\"task\")) because auto singularization is disabled.");
                explicitSingular = new String(pluralName);
            } else {
                explicitSingular = autoSingularize(new String(pluralName));
                if (explicitSingular == null) {
                    node.addError("Can't singularize this name; please specify the singular explicitly (i.e. @Singular(\"sheep\"))");
                    explicitSingular = new String(pluralName);
                }
            }
        }
        char[] singularName = explicitSingular.toCharArray();
        TypeReference type = ((AbstractVariableDeclaration) node.get()).type;
        TypeReference[] typeArgs = null;
        String typeName;
        if (type instanceof ParameterizedSingleTypeReference) {
            typeArgs = ((ParameterizedSingleTypeReference) type).typeArguments;
            typeName = new String(((ParameterizedSingleTypeReference) type).token);
        } else if (type instanceof ParameterizedQualifiedTypeReference) {
            TypeReference[][] tr = ((ParameterizedQualifiedTypeReference) type).typeArguments;
            if (tr != null)
                typeArgs = tr[tr.length - 1];
            char[][] tokens = ((ParameterizedQualifiedTypeReference) type).tokens;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < tokens.length; i++) {
                if (i > 0)
                    sb.append(".");
                sb.append(tokens[i]);
            }
            typeName = sb.toString();
        } else {
            typeName = type.toString();
        }
        String targetFqn = EclipseSingularsRecipes.get().toQualified(typeName);
        EclipseSingularizer singularizer = EclipseSingularsRecipes.get().getSingularizer(targetFqn);
        if (singularizer == null) {
            node.addError("Lombok does not know how to create the singular-form builder methods for type '" + typeName + "'; they won't be generated.");
            return null;
        }
        return new SingularData(child, singularName, pluralName, typeArgs == null ? Collections.<TypeReference>emptyList() : Arrays.asList(typeArgs), targetFqn, singularizer, source);
    }
    return null;
}
Also used : ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) AbstractVariableDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration) Singular(lombok.Singular) SingularData(lombok.eclipse.handlers.EclipseSingularsRecipes.SingularData) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) EclipseSingularizer(lombok.eclipse.handlers.EclipseSingularsRecipes.EclipseSingularizer) EclipseNode(lombok.eclipse.EclipseNode) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)

Aggregations

Singular (lombok.Singular)2 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)1 JCTypeApply (com.sun.tools.javac.tree.JCTree.JCTypeApply)1 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)1 Name (com.sun.tools.javac.util.Name)1 EclipseNode (lombok.eclipse.EclipseNode)1 EclipseSingularizer (lombok.eclipse.handlers.EclipseSingularsRecipes.EclipseSingularizer)1 SingularData (lombok.eclipse.handlers.EclipseSingularsRecipes.SingularData)1 JavacNode (lombok.javac.JavacNode)1 JavacSingularizer (lombok.javac.handlers.JavacSingularsRecipes.JavacSingularizer)1 SingularData (lombok.javac.handlers.JavacSingularsRecipes.SingularData)1 AbstractVariableDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration)1 ParameterizedQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference)1 ParameterizedSingleTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference)1 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)1 SingleTypeReference (org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)1 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)1