Search in sources :

Example 31 with MethodDeclaration

use of org.eclipse.jdt.internal.compiler.ast.MethodDeclaration in project lombok by rzwitserloot.

the class EclipseHandlerUtil method methodExists.

/**
	 * Checks if there is a method with the provided name. In case of multiple methods (overloading), only
	 * the first method decides if EXISTS_BY_USER or EXISTS_BY_LOMBOK is returned.
	 * 
	 * @param methodName the method name to check for.
	 * @param node Any node that represents the Type (TypeDeclaration) to look in, or any child node thereof.
	 * @param caseSensitive If the search should be case sensitive.
	 * @param params The number of parameters the method should have; varargs count as 0-*. Set to -1 to find any method with the appropriate name regardless of parameter count.
	 */
public static MemberExistsResult methodExists(String methodName, EclipseNode node, boolean caseSensitive, int params) {
    while (node != null && !(node.get() instanceof TypeDeclaration)) {
        node = node.up();
    }
    if (node != null && node.get() instanceof TypeDeclaration) {
        TypeDeclaration typeDecl = (TypeDeclaration) node.get();
        if (typeDecl.methods != null)
            top: for (AbstractMethodDeclaration def : typeDecl.methods) {
                if (def instanceof MethodDeclaration) {
                    char[] mName = def.selector;
                    if (mName == null)
                        continue;
                    boolean nameEquals = caseSensitive ? methodName.equals(new String(mName)) : methodName.equalsIgnoreCase(new String(mName));
                    if (nameEquals) {
                        if (params > -1) {
                            int minArgs = 0;
                            int maxArgs = 0;
                            if (def.arguments != null && def.arguments.length > 0) {
                                minArgs = def.arguments.length;
                                if ((def.arguments[def.arguments.length - 1].type.bits & ASTNode.IsVarArgs) != 0) {
                                    minArgs--;
                                    maxArgs = Integer.MAX_VALUE;
                                } else {
                                    maxArgs = minArgs;
                                }
                            }
                            if (params < minArgs || params > maxArgs)
                                continue;
                        }
                        if (isTolerate(node, def))
                            continue top;
                        return getGeneratedBy(def) == null ? MemberExistsResult.EXISTS_BY_USER : MemberExistsResult.EXISTS_BY_LOMBOK;
                    }
                }
            }
    }
    return MemberExistsResult.NOT_EXISTS;
}
Also used : MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)

Example 32 with MethodDeclaration

use of org.eclipse.jdt.internal.compiler.ast.MethodDeclaration in project lombok by rzwitserloot.

the class EclipseGuavaSingularizer method generatePluralMethod.

void generatePluralMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType, boolean fluent) {
    MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
    md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    md.modifiers = ClassFileConstants.AccPublic;
    List<Statement> statements = new ArrayList<Statement>();
    statements.add(createConstructBuilderVarIfNeeded(data, builderType));
    FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L);
    thisDotField.receiver = new ThisReference(0, 0);
    MessageSend thisDotFieldDotAddAll = new MessageSend();
    thisDotFieldDotAddAll.arguments = new Expression[] { new SingleNameReference(data.getPluralName(), 0L) };
    thisDotFieldDotAddAll.receiver = thisDotField;
    thisDotFieldDotAddAll.selector = (getAddMethodName() + "All").toCharArray();
    statements.add(thisDotFieldDotAddAll);
    if (returnStatement != null)
        statements.add(returnStatement);
    md.statements = statements.toArray(new Statement[statements.size()]);
    TypeReference paramType;
    paramType = new QualifiedTypeReference(fromQualifiedName(getAddAllTypeName()), NULL_POSS);
    paramType = addTypeArgs(getTypeArgumentsCount(), true, builderType, paramType, data.getTypeArgs());
    Argument param = new Argument(data.getPluralName(), 0, paramType, 0);
    md.arguments = new Argument[] { param };
    md.returnType = returnType;
    md.selector = fluent ? data.getPluralName() : HandlerUtil.buildAccessorName(getAddMethodName() + "All", new String(data.getPluralName())).toCharArray();
    data.setGeneratedByRecursive(md);
    injectMethod(builderType, md);
}
Also used : MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) Argument(org.eclipse.jdt.internal.compiler.ast.Argument) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ArrayList(java.util.ArrayList) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference)

Example 33 with MethodDeclaration

use of org.eclipse.jdt.internal.compiler.ast.MethodDeclaration in project lombok by rzwitserloot.

the class EclipseJavaUtilListSetSingularizer method generateClearMethod.

private void generateClearMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType) {
    MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
    md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    md.modifiers = ClassFileConstants.AccPublic;
    FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L);
    thisDotField.receiver = new ThisReference(0, 0);
    FieldReference thisDotField2 = new FieldReference(data.getPluralName(), 0L);
    thisDotField2.receiver = new ThisReference(0, 0);
    md.selector = HandlerUtil.buildAccessorName("clear", new String(data.getPluralName())).toCharArray();
    MessageSend clearMsg = new MessageSend();
    clearMsg.receiver = thisDotField2;
    clearMsg.selector = "clear".toCharArray();
    Statement clearStatement = new IfStatement(new EqualExpression(thisDotField, new NullLiteral(0, 0), OperatorIds.NOT_EQUAL), clearMsg, 0, 0);
    md.statements = returnStatement != null ? new Statement[] { clearStatement, returnStatement } : new Statement[] { clearStatement };
    md.returnType = returnType;
    injectMethod(builderType, md);
}
Also used : MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral)

Example 34 with MethodDeclaration

use of org.eclipse.jdt.internal.compiler.ast.MethodDeclaration in project lombok by rzwitserloot.

the class EclipseJavaUtilMapSingularizer method generateClearMethod.

private void generateClearMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType) {
    MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
    md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    md.modifiers = ClassFileConstants.AccPublic;
    String pN = new String(data.getPluralName());
    char[] keyFieldName = (pN + "$key").toCharArray();
    char[] valueFieldName = (pN + "$value").toCharArray();
    FieldReference thisDotField = new FieldReference(keyFieldName, 0L);
    thisDotField.receiver = new ThisReference(0, 0);
    FieldReference thisDotField2 = new FieldReference(keyFieldName, 0L);
    thisDotField2.receiver = new ThisReference(0, 0);
    FieldReference thisDotField3 = new FieldReference(valueFieldName, 0L);
    thisDotField3.receiver = new ThisReference(0, 0);
    md.selector = HandlerUtil.buildAccessorName("clear", new String(data.getPluralName())).toCharArray();
    MessageSend clearMsg1 = new MessageSend();
    clearMsg1.receiver = thisDotField2;
    clearMsg1.selector = "clear".toCharArray();
    MessageSend clearMsg2 = new MessageSend();
    clearMsg2.receiver = thisDotField3;
    clearMsg2.selector = "clear".toCharArray();
    Block clearMsgs = new Block(2);
    clearMsgs.statements = new Statement[] { clearMsg1, clearMsg2 };
    Statement clearStatement = new IfStatement(new EqualExpression(thisDotField, new NullLiteral(0, 0), OperatorIds.NOT_EQUAL), clearMsgs, 0, 0);
    md.statements = returnStatement != null ? new Statement[] { clearStatement, returnStatement } : new Statement[] { clearStatement };
    md.returnType = returnType;
    injectMethod(builderType, md);
}
Also used : MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) ForeachStatement(org.eclipse.jdt.internal.compiler.ast.ForeachStatement) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) Block(org.eclipse.jdt.internal.compiler.ast.Block) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral)

Example 35 with MethodDeclaration

use of org.eclipse.jdt.internal.compiler.ast.MethodDeclaration in project lombok by rzwitserloot.

the class EclipseJavaUtilMapSingularizer method generateSingularMethod.

private void generateSingularMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType, boolean fluent) {
    MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
    md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    md.modifiers = ClassFileConstants.AccPublic;
    List<Statement> statements = new ArrayList<Statement>();
    statements.add(createConstructBuilderVarIfNeeded(data, builderType, true));
    String sN = new String(data.getSingularName());
    String pN = new String(data.getPluralName());
    char[] keyParamName = (sN + "Key").toCharArray();
    char[] valueParamName = (sN + "Value").toCharArray();
    char[] keyFieldName = (pN + "$key").toCharArray();
    char[] valueFieldName = (pN + "$value").toCharArray();
    /* this.pluralname$key.add(singularnameKey); */
    {
        FieldReference thisDotKeyField = new FieldReference(keyFieldName, 0L);
        thisDotKeyField.receiver = new ThisReference(0, 0);
        MessageSend thisDotKeyFieldDotAdd = new MessageSend();
        thisDotKeyFieldDotAdd.arguments = new Expression[] { new SingleNameReference(keyParamName, 0L) };
        thisDotKeyFieldDotAdd.receiver = thisDotKeyField;
        thisDotKeyFieldDotAdd.selector = "add".toCharArray();
        statements.add(thisDotKeyFieldDotAdd);
    }
    /* this.pluralname$value.add(singularnameValue); */
    {
        FieldReference thisDotValueField = new FieldReference(valueFieldName, 0L);
        thisDotValueField.receiver = new ThisReference(0, 0);
        MessageSend thisDotValueFieldDotAdd = new MessageSend();
        thisDotValueFieldDotAdd.arguments = new Expression[] { new SingleNameReference(valueParamName, 0L) };
        thisDotValueFieldDotAdd.receiver = thisDotValueField;
        thisDotValueFieldDotAdd.selector = "add".toCharArray();
        statements.add(thisDotValueFieldDotAdd);
    }
    if (returnStatement != null)
        statements.add(returnStatement);
    md.statements = statements.toArray(new Statement[statements.size()]);
    TypeReference keyParamType = cloneParamType(0, data.getTypeArgs(), builderType);
    Argument keyParam = new Argument(keyParamName, 0, keyParamType, 0);
    TypeReference valueParamType = cloneParamType(1, data.getTypeArgs(), builderType);
    Argument valueParam = new Argument(valueParamName, 0, valueParamType, 0);
    md.arguments = new Argument[] { keyParam, valueParam };
    md.returnType = returnType;
    md.selector = fluent ? data.getSingularName() : HandlerUtil.buildAccessorName("put", new String(data.getSingularName())).toCharArray();
    data.setGeneratedByRecursive(md);
    injectMethod(builderType, md);
}
Also used : FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) Argument(org.eclipse.jdt.internal.compiler.ast.Argument) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) ForeachStatement(org.eclipse.jdt.internal.compiler.ast.ForeachStatement) ArrayList(java.util.ArrayList) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)

Aggregations

MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)40 ReturnStatement (org.eclipse.jdt.internal.compiler.ast.ReturnStatement)22 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)19 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)19 ArrayList (java.util.ArrayList)18 ThisReference (org.eclipse.jdt.internal.compiler.ast.ThisReference)17 EclipseNode (lombok.eclipse.EclipseNode)16 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)16 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)15 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)15 MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)15 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)15 FieldDeclaration (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)14 IfStatement (org.eclipse.jdt.internal.compiler.ast.IfStatement)14 FieldReference (org.eclipse.jdt.internal.compiler.ast.FieldReference)13 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)12 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)11 SingleTypeReference (org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)10 ParameterizedQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference)8 ASTNode (org.eclipse.jdt.internal.compiler.ast.ASTNode)6