Search in sources :

Example 21 with Statement

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

the class EclipseGuavaSingularizer method generateMethods.

@Override
public void generateMethods(SingularData data, boolean deprecate, EclipseNode builderType, boolean fluent, boolean chain) {
    TypeReference returnType = chain ? cloneSelfType(builderType) : TypeReference.baseTypeReference(TypeIds.T_void, 0);
    Statement returnStatement = chain ? new ReturnStatement(new ThisReference(0, 0), 0, 0) : null;
    generateSingularMethod(deprecate, returnType, returnStatement, data, builderType, fluent);
    returnType = chain ? cloneSelfType(builderType) : TypeReference.baseTypeReference(TypeIds.T_void, 0);
    returnStatement = chain ? new ReturnStatement(new ThisReference(0, 0), 0, 0) : null;
    generatePluralMethod(deprecate, returnType, returnStatement, data, builderType, fluent);
    returnType = chain ? cloneSelfType(builderType) : TypeReference.baseTypeReference(TypeIds.T_void, 0);
    returnStatement = chain ? new ReturnStatement(new ThisReference(0, 0), 0, 0) : null;
    generateClearMethod(deprecate, returnType, returnStatement, data, builderType);
}
Also used : ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference)

Example 22 with Statement

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

the class EclipseGuavaSingularizer method generateClearMethod.

void generateClearMethod(boolean deprecate, 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);
    Assignment a = new Assignment(thisDotField, new NullLiteral(0, 0), 0);
    md.selector = HandlerUtil.buildAccessorName("clear", new String(data.getPluralName())).toCharArray();
    md.statements = returnStatement != null ? new Statement[] { a, returnStatement } : new Statement[] { a };
    md.returnType = returnType;
    md.annotations = deprecate ? new Annotation[] { generateDeprecatedAnnotation(data.getSource()) } : null;
    injectMethod(builderType, md);
}
Also used : Assignment(org.eclipse.jdt.internal.compiler.ast.Assignment) 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) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation)

Example 23 with Statement

use of org.eclipse.jdt.internal.compiler.ast.Statement in project che by eclipse.

the class SourceTypeConverter method convert.

/*
	 * Convert a method source element into a parsed method/constructor declaration
	 */
private AbstractMethodDeclaration convert(SourceMethod methodHandle, SourceMethodElementInfo methodInfo, CompilationResult compilationResult) throws JavaModelException {
    AbstractMethodDeclaration method;
    /* only source positions available */
    int start = methodInfo.getNameSourceStart();
    int end = methodInfo.getNameSourceEnd();
    /* https://bugs.eclipse.org/bugs/show_bug.cgi?id=324850, Even when this type is being constructed
		   on behalf of a 1.4 project we must internalize type variables properly in order to be able to
		   recognize usages of them in the method signature, to apply substitutions and thus to be able to
		   detect overriding in the presence of generics. If we simply drop them, when the method signature
		   refers to the type parameter, we won't know it should be bound to the type parameter and perform
		   incorrect lookup and may mistakenly end up with missing types
		 */
    TypeParameter[] typeParams = null;
    char[][] typeParameterNames = methodInfo.getTypeParameterNames();
    if (typeParameterNames != null) {
        int parameterCount = typeParameterNames.length;
        if (parameterCount > 0) {
            // method's type parameters must be null if no type parameter
            char[][][] typeParameterBounds = methodInfo.getTypeParameterBounds();
            typeParams = new TypeParameter[parameterCount];
            for (int i = 0; i < parameterCount; i++) {
                typeParams[i] = createTypeParameter(typeParameterNames[i], typeParameterBounds[i], start, end);
            }
        }
    }
    int modifiers = methodInfo.getModifiers();
    if (methodInfo.isConstructor()) {
        ConstructorDeclaration decl = new ConstructorDeclaration(compilationResult);
        decl.bits &= ~ASTNode.IsDefaultConstructor;
        method = decl;
        decl.typeParameters = typeParams;
    } else {
        MethodDeclaration decl;
        if (methodInfo.isAnnotationMethod()) {
            AnnotationMethodDeclaration annotationMethodDeclaration = new AnnotationMethodDeclaration(compilationResult);
            /* conversion of default value */
            SourceAnnotationMethodInfo annotationMethodInfo = (SourceAnnotationMethodInfo) methodInfo;
            boolean hasDefaultValue = annotationMethodInfo.defaultValueStart != -1 || annotationMethodInfo.defaultValueEnd != -1;
            if ((this.flags & FIELD_INITIALIZATION) != 0) {
                if (hasDefaultValue) {
                    char[] defaultValueSource = CharOperation.subarray(getSource(), annotationMethodInfo.defaultValueStart, annotationMethodInfo.defaultValueEnd + 1);
                    if (defaultValueSource != null) {
                        Expression expression = parseMemberValue(defaultValueSource);
                        if (expression != null) {
                            annotationMethodDeclaration.defaultValue = expression;
                        }
                    } else {
                        // could not retrieve the default value
                        hasDefaultValue = false;
                    }
                }
            }
            if (hasDefaultValue)
                modifiers |= ClassFileConstants.AccAnnotationDefault;
            decl = annotationMethodDeclaration;
        } else {
            decl = new MethodDeclaration(compilationResult);
        }
        // convert return type
        decl.returnType = createTypeReference(methodInfo.getReturnTypeName(), start, end);
        // type parameters
        decl.typeParameters = typeParams;
        method = decl;
    }
    method.selector = methodHandle.getElementName().toCharArray();
    boolean isVarargs = (modifiers & ClassFileConstants.AccVarargs) != 0;
    method.modifiers = modifiers & ~ClassFileConstants.AccVarargs;
    method.sourceStart = start;
    method.sourceEnd = end;
    method.declarationSourceStart = methodInfo.getDeclarationSourceStart();
    method.declarationSourceEnd = methodInfo.getDeclarationSourceEnd();
    // convert 1.5 specific constructs only if compliance is 1.5 or above
    if (this.has1_5Compliance) {
        /* convert annotations */
        method.annotations = convertAnnotations(methodHandle);
    }
    /* convert arguments */
    String[] argumentTypeSignatures = methodHandle.getParameterTypes();
    char[][] argumentNames = methodInfo.getArgumentNames();
    int argumentCount = argumentTypeSignatures == null ? 0 : argumentTypeSignatures.length;
    if (argumentCount > 0) {
        ILocalVariable[] parameters = methodHandle.getParameters();
        long position = ((long) start << 32) + end;
        method.arguments = new Argument[argumentCount];
        for (int i = 0; i < argumentCount; i++) {
            TypeReference typeReference = createTypeReference(argumentTypeSignatures[i], start, end);
            if (isVarargs && i == argumentCount - 1) {
                typeReference.bits |= ASTNode.IsVarArgs;
            }
            method.arguments[i] = new Argument(argumentNames[i], position, typeReference, ClassFileConstants.AccDefault);
            // convert 1.5 specific constructs only if compliance is 1.5 or above
            if (this.has1_5Compliance) {
                /* convert annotations */
                method.arguments[i].annotations = convertAnnotations(parameters[i]);
            }
        }
    }
    /* convert thrown exceptions */
    char[][] exceptionTypeNames = methodInfo.getExceptionTypeNames();
    int exceptionCount = exceptionTypeNames == null ? 0 : exceptionTypeNames.length;
    if (exceptionCount > 0) {
        method.thrownExceptions = new TypeReference[exceptionCount];
        for (int i = 0; i < exceptionCount; i++) {
            method.thrownExceptions[i] = createTypeReference(exceptionTypeNames[i], start, end);
        }
    }
    /* convert local and anonymous types */
    if ((this.flags & LOCAL_TYPE) != 0) {
        IJavaElement[] children = methodInfo.getChildren();
        int typesLength = children.length;
        if (typesLength != 0) {
            Statement[] statements = new Statement[typesLength];
            for (int i = 0; i < typesLength; i++) {
                SourceType type = (SourceType) children[i];
                TypeDeclaration localType = convert(type, compilationResult);
                if ((localType.bits & ASTNode.IsAnonymousType) != 0) {
                    QualifiedAllocationExpression expression = new QualifiedAllocationExpression(localType);
                    expression.type = localType.superclass;
                    localType.superclass = null;
                    localType.superInterfaces = null;
                    localType.allocation = expression;
                    statements[i] = expression;
                } else {
                    statements[i] = localType;
                }
            }
            method.statements = statements;
        }
    }
    return method;
}
Also used : TypeParameter(org.eclipse.jdt.internal.compiler.ast.TypeParameter) Argument(org.eclipse.jdt.internal.compiler.ast.Argument) ISourceType(org.eclipse.jdt.internal.compiler.env.ISourceType) SourceType(org.eclipse.jdt.internal.core.SourceType) ILocalVariable(org.eclipse.jdt.core.ILocalVariable) ConstructorDeclaration(org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) IJavaElement(org.eclipse.jdt.core.IJavaElement) AnnotationMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) AnnotationMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) QualifiedAllocationExpression(org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression) SourceAnnotationMethodInfo(org.eclipse.jdt.internal.core.SourceAnnotationMethodInfo) QualifiedAllocationExpression(org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Example 24 with Statement

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

the class HandleGetter method createGetter.

public MethodDeclaration createGetter(TypeDeclaration parent, EclipseNode fieldNode, String name, int modifier, ASTNode source, boolean lazy, List<Annotation> onMethod) {
    FieldDeclaration field = (FieldDeclaration) fieldNode.get();
    // Remember the type; lazy will change it;
    TypeReference returnType = copyType(((FieldDeclaration) fieldNode.get()).type, source);
    Statement[] statements;
    if (lazy) {
        statements = createLazyGetterBody(source, fieldNode);
    } else {
        statements = createSimpleGetterBody(source, fieldNode);
    }
    MethodDeclaration method = new MethodDeclaration(parent.compilationResult);
    method.modifiers = modifier;
    method.returnType = returnType;
    method.annotations = null;
    method.arguments = null;
    method.selector = name.toCharArray();
    method.binding = null;
    method.thrownExceptions = null;
    method.typeParameters = null;
    method.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart;
    method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd;
    method.statements = statements;
    EclipseHandlerUtil.registerCreatedLazyGetter((FieldDeclaration) fieldNode.get(), method.selector, returnType);
    /* Generate annotations that must  be put on the generated method, and attach them. */
    {
        Annotation[] deprecated = null;
        if (isFieldDeprecated(fieldNode)) {
            deprecated = new Annotation[] { generateDeprecatedAnnotation(source) };
        }
        method.annotations = copyAnnotations(source, onMethod.toArray(new Annotation[0]), findAnnotations(field, NON_NULL_PATTERN), findAnnotations(field, NULLABLE_PATTERN), findDelegatesAndMarkAsHandled(fieldNode), deprecated);
    }
    method.traverse(new SetGeneratedByVisitor(source), parent.scope);
    return method;
}
Also used : ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) SynchronizedStatement(org.eclipse.jdt.internal.compiler.ast.SynchronizedStatement) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) ArrayTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation)

Example 25 with Statement

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

the class HandleHelper method handle.

@Override
public void handle(AnnotationValues<Helper> annotation, Annotation ast, EclipseNode annotationNode) {
    handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.HELPER_FLAG_USAGE, "@Helper");
    EclipseNode annotatedType = annotationNode.up();
    EclipseNode containingBlock = annotatedType == null ? null : annotatedType.directUp();
    Statement[] origStatements = getStatementsFromAstNode(containingBlock == null ? null : containingBlock.get());
    if (annotatedType == null || annotatedType.getKind() != Kind.TYPE || origStatements == null) {
        annotationNode.addError("@Helper is legal only on method-local classes.");
        return;
    }
    TypeDeclaration annotatedType_ = (TypeDeclaration) annotatedType.get();
    int indexOfType = -1;
    for (int i = 0; i < origStatements.length; i++) {
        if (origStatements[i] == annotatedType_) {
            indexOfType = i;
            break;
        }
    }
    final List<String> knownMethodNames = new ArrayList<String>();
    for (AbstractMethodDeclaration methodOfHelper : annotatedType_.methods) {
        if (!(methodOfHelper instanceof MethodDeclaration))
            continue;
        char[] name = methodOfHelper.selector;
        if (name != null && name.length > 0 && name[0] != '<')
            knownMethodNames.add(new String(name));
    }
    Collections.sort(knownMethodNames);
    final String[] knownMethodNames_ = knownMethodNames.toArray(new String[knownMethodNames.size()]);
    final char[] helperName = new char[annotatedType_.name.length + 1];
    final boolean[] helperUsed = new boolean[1];
    helperName[0] = '$';
    System.arraycopy(annotatedType_.name, 0, helperName, 1, helperName.length - 1);
    ASTVisitor visitor = new ASTVisitor() {

        @Override
        public boolean visit(MessageSend messageSend, BlockScope scope) {
            if (messageSend.receiver instanceof ThisReference) {
                if ((((ThisReference) messageSend.receiver).bits & ASTNode.IsImplicitThis) == 0)
                    return true;
            } else if (messageSend.receiver != null)
                return true;
            char[] name = messageSend.selector;
            if (name == null || name.length == 0 || name[0] == '<')
                return true;
            String n = new String(name);
            if (Arrays.binarySearch(knownMethodNames_, n) < 0)
                return true;
            messageSend.receiver = new SingleNameReference(helperName, messageSend.nameSourcePosition);
            helperUsed[0] = true;
            return true;
        }
    };
    for (int i = indexOfType + 1; i < origStatements.length; i++) {
        origStatements[i].traverse(visitor, null);
    }
    if (!helperUsed[0]) {
        annotationNode.addWarning("No methods of this helper class are ever used.");
        return;
    }
    Statement[] newStatements = new Statement[origStatements.length + 1];
    System.arraycopy(origStatements, 0, newStatements, 0, indexOfType + 1);
    System.arraycopy(origStatements, indexOfType + 1, newStatements, indexOfType + 2, origStatements.length - indexOfType - 1);
    LocalDeclaration decl = new LocalDeclaration(helperName, 0, 0);
    decl.modifiers |= ClassFileConstants.AccFinal;
    AllocationExpression alloc = new AllocationExpression();
    alloc.type = new SingleTypeReference(annotatedType_.name, 0L);
    decl.initialization = alloc;
    decl.type = new SingleTypeReference(annotatedType_.name, 0L);
    SetGeneratedByVisitor sgbvVisitor = new SetGeneratedByVisitor(annotationNode.get());
    decl.traverse(sgbvVisitor, null);
    newStatements[indexOfType + 1] = decl;
    setStatementsOfAstNode(containingBlock.get(), newStatements);
}
Also used : LocalDeclaration(org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) SwitchStatement(org.eclipse.jdt.internal.compiler.ast.SwitchStatement) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) ArrayList(java.util.ArrayList) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) ASTVisitor(org.eclipse.jdt.internal.compiler.ASTVisitor) MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) BlockScope(org.eclipse.jdt.internal.compiler.lookup.BlockScope) EclipseNode(lombok.eclipse.EclipseNode) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)

Aggregations

Statement (org.eclipse.jdt.internal.compiler.ast.Statement)38 IfStatement (org.eclipse.jdt.internal.compiler.ast.IfStatement)27 ReturnStatement (org.eclipse.jdt.internal.compiler.ast.ReturnStatement)27 ThisReference (org.eclipse.jdt.internal.compiler.ast.ThisReference)27 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)23 MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)21 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)21 FieldReference (org.eclipse.jdt.internal.compiler.ast.FieldReference)19 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)19 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)18 ArrayList (java.util.ArrayList)17 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)15 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)15 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)14 EqualExpression (org.eclipse.jdt.internal.compiler.ast.EqualExpression)14 Assignment (org.eclipse.jdt.internal.compiler.ast.Assignment)12 NullLiteral (org.eclipse.jdt.internal.compiler.ast.NullLiteral)12 AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)10 ConditionalExpression (org.eclipse.jdt.internal.compiler.ast.ConditionalExpression)9 LocalDeclaration (org.eclipse.jdt.internal.compiler.ast.LocalDeclaration)9