Search in sources :

Example 31 with SingleNameReference

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

the class EclipseJavaUtilMapSingularizer method generateSingularMethod.

private void generateSingularMethod(boolean deprecate, 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();
    md.annotations = deprecate ? new Annotation[] { generateDeprecatedAnnotation(data.getSource()) } : null;
    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) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) 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)

Example 32 with SingleNameReference

use of org.eclipse.jdt.internal.compiler.ast.SingleNameReference in project spoon by INRIA.

the class ReferenceBuilder method getExecutableReference.

<T> CtExecutableReference<T> getExecutableReference(MessageSend messageSend) {
    if (messageSend.binding != null) {
        return getExecutableReference(messageSend.binding);
    }
    CtExecutableReference<T> ref = jdtTreeBuilder.getFactory().Core().createExecutableReference();
    ref.setSimpleName(CharOperation.charToString(messageSend.selector));
    ref.setType(this.<T>getTypeReference(messageSend.expectedType()));
    if (messageSend.receiver.resolvedType == null) {
        // It is crisis dude! static context, we don't have much more information.
        if (messageSend.receiver instanceof SingleNameReference) {
            ref.setDeclaringType(jdtTreeBuilder.getHelper().createTypeAccessNoClasspath((SingleNameReference) messageSend.receiver).getAccessedType());
        } else if (messageSend.receiver instanceof QualifiedNameReference) {
            ref.setDeclaringType(jdtTreeBuilder.getHelper().createTypeAccessNoClasspath((QualifiedNameReference) messageSend.receiver).getAccessedType());
        }
    } else {
        ref.setDeclaringType(getTypeReference(messageSend.receiver.resolvedType));
    }
    if (messageSend.arguments != null) {
        final List<CtTypeReference<?>> parameters = new ArrayList<>();
        for (Expression expression : messageSend.arguments) {
            parameters.add(getTypeReference(expression.resolvedType));
        }
        ref.setParameters(parameters);
    }
    return ref;
}
Also used : Expression(org.eclipse.jdt.internal.compiler.ast.Expression) LambdaExpression(org.eclipse.jdt.internal.compiler.ast.LambdaExpression) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) CtTypeReference(spoon.reflect.reference.CtTypeReference) ArrayList(java.util.ArrayList) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) QualifiedNameReference(org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference)

Aggregations

SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)32 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)21 MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)20 ArrayList (java.util.ArrayList)18 ReturnStatement (org.eclipse.jdt.internal.compiler.ast.ReturnStatement)18 ThisReference (org.eclipse.jdt.internal.compiler.ast.ThisReference)18 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)17 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)17 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)16 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)15 AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)14 IfStatement (org.eclipse.jdt.internal.compiler.ast.IfStatement)14 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)14 FieldReference (org.eclipse.jdt.internal.compiler.ast.FieldReference)13 EqualExpression (org.eclipse.jdt.internal.compiler.ast.EqualExpression)11 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)10 QualifiedNameReference (org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference)10 Assignment (org.eclipse.jdt.internal.compiler.ast.Assignment)9 LocalDeclaration (org.eclipse.jdt.internal.compiler.ast.LocalDeclaration)9 SingleTypeReference (org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)9