Search in sources :

Example 61 with SingleVariableDeclaration

use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project flux by eclipse.

the class ASTNodes method isExplicitlyTypedLambda.

private static boolean isExplicitlyTypedLambda(Expression expression) {
    if (!(expression instanceof LambdaExpression))
        return false;
    LambdaExpression lambda = (LambdaExpression) expression;
    List<VariableDeclaration> parameters = lambda.parameters();
    if (parameters.isEmpty())
        return true;
    return parameters.get(0) instanceof SingleVariableDeclaration;
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 62 with SingleVariableDeclaration

use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project flux by eclipse.

the class StubUtility method getMethodComment.

/*
	 * Don't use this method directly, use CodeGeneration.
	 * This method should work with all AST levels.
	 * @see org.eclipse.jdt.ui.CodeGeneration#getMethodComment(ICompilationUnit, String, MethodDeclaration, boolean, String, String[], String)
	 */
public static String getMethodComment(ICompilationUnit cu, String typeName, MethodDeclaration decl, boolean isDeprecated, String targetName, String targetMethodDeclaringTypeName, String[] targetMethodParameterTypeNames, boolean delegate, String lineDelimiter) throws CoreException {
    boolean needsTarget = targetMethodDeclaringTypeName != null && targetMethodParameterTypeNames != null;
    String templateName = CodeTemplateContextType.METHODCOMMENT_ID;
    if (decl.isConstructor()) {
        templateName = CodeTemplateContextType.CONSTRUCTORCOMMENT_ID;
    } else if (needsTarget) {
        if (delegate)
            templateName = CodeTemplateContextType.DELEGATECOMMENT_ID;
        else
            templateName = CodeTemplateContextType.OVERRIDECOMMENT_ID;
    }
    Template template = getCodeTemplate(templateName, cu.getJavaProject());
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
    context.setCompilationUnitVariables(cu);
    context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, typeName);
    context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, decl.getName().getIdentifier());
    if (!decl.isConstructor()) {
        context.setVariable(CodeTemplateContextType.RETURN_TYPE, ASTNodes.asString(getReturnType(decl)));
    }
    if (needsTarget) {
        if (delegate)
            context.setVariable(CodeTemplateContextType.SEE_TO_TARGET_TAG, getSeeTag(targetMethodDeclaringTypeName, targetName, targetMethodParameterTypeNames));
        else
            context.setVariable(CodeTemplateContextType.SEE_TO_OVERRIDDEN_TAG, getSeeTag(targetMethodDeclaringTypeName, targetName, targetMethodParameterTypeNames));
    }
    TemplateBuffer buffer;
    try {
        buffer = context.evaluate(template);
    } catch (BadLocationException e) {
        throw new CoreException(Status.CANCEL_STATUS);
    } catch (TemplateException e) {
        throw new CoreException(Status.CANCEL_STATUS);
    }
    if (buffer == null)
        return null;
    String str = buffer.getString();
    if (Strings.containsOnlyWhitespaces(str)) {
        return null;
    }
    // look if Javadoc tags have to be added
    TemplateVariable position = findVariable(buffer, CodeTemplateContextType.TAGS);
    if (position == null) {
        return str;
    }
    IDocument textBuffer = new Document(str);
    List<TypeParameter> typeParams = shouldGenerateMethodTypeParameterTags(cu.getJavaProject()) ? decl.typeParameters() : Collections.emptyList();
    String[] typeParamNames = new String[typeParams.size()];
    for (int i = 0; i < typeParamNames.length; i++) {
        TypeParameter elem = typeParams.get(i);
        typeParamNames[i] = elem.getName().getIdentifier();
    }
    List<SingleVariableDeclaration> params = decl.parameters();
    String[] paramNames = new String[params.size()];
    for (int i = 0; i < paramNames.length; i++) {
        SingleVariableDeclaration elem = params.get(i);
        paramNames[i] = elem.getName().getIdentifier();
    }
    String[] exceptionNames = getExceptionNames(decl);
    String returnType = null;
    if (!decl.isConstructor()) {
        returnType = ASTNodes.asString(getReturnType(decl));
    }
    int[] tagOffsets = position.getOffsets();
    for (int i = tagOffsets.length - 1; i >= 0; i--) {
        // from last to first
        try {
            insertTag(textBuffer, tagOffsets[i], position.getLength(), paramNames, exceptionNames, returnType, typeParamNames, isDeprecated, lineDelimiter);
        } catch (BadLocationException e) {
            throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
        }
    }
    return textBuffer.get();
}
Also used : TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) ITypeParameter(org.eclipse.jdt.core.ITypeParameter) TemplateException(org.eclipse.jface.text.templates.TemplateException) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) Template(org.eclipse.jface.text.templates.Template) CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) CoreException(org.eclipse.core.runtime.CoreException) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) BadLocationException(org.eclipse.jface.text.BadLocationException) IDocument(org.eclipse.jface.text.IDocument)

Example 63 with SingleVariableDeclaration

use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project flux by eclipse.

the class ASTNodeFactory method newType.

/**
 * Returns the new type node corresponding to the type of the given declaration
 * including the extra dimensions. If the type is a {@link UnionType}, use the LUB type.
 * If the <code>importRewrite</code> is <code>null</code>, the type may be fully-qualified.
 *
 * @param ast The AST to create the resulting type with.
 * @param declaration The variable declaration to get the type from
 * @param importRewrite the import rewrite to use, or <code>null</code>
 * @param context the import rewrite context, or <code>null</code>
 * @return a new type node created with the given AST.
 *
 * @since 3.7.1
 */
public static Type newType(AST ast, VariableDeclaration declaration, ImportRewrite importRewrite, ImportRewriteContext context) {
    if (declaration instanceof VariableDeclarationFragment && declaration.getParent() instanceof LambdaExpression) {
        return newType((LambdaExpression) declaration.getParent(), (VariableDeclarationFragment) declaration, ast, importRewrite, context);
    }
    Type type = ASTNodes.getType(declaration);
    if (declaration instanceof SingleVariableDeclaration) {
        Type type2 = ((SingleVariableDeclaration) declaration).getType();
        if (type2 instanceof UnionType) {
            ITypeBinding typeBinding = type2.resolveBinding();
            if (typeBinding != null) {
                if (importRewrite != null) {
                    type = importRewrite.addImport(typeBinding, ast, context);
                    return type;
                } else {
                    String qualifiedName = typeBinding.getQualifiedName();
                    if (qualifiedName.length() > 0) {
                        type = ast.newSimpleType(ast.newName(qualifiedName));
                        return type;
                    }
                }
            }
            // XXX: fallback for intersection types or unresolved types: take first type of union
            type = (Type) ((UnionType) type2).types().get(0);
            return type;
        }
    }
    type = (Type) ASTNode.copySubtree(ast, type);
    List<Dimension> extraDimensions = declaration.extraDimensions();
    if (!extraDimensions.isEmpty()) {
        ArrayType arrayType;
        if (type instanceof ArrayType) {
            arrayType = (ArrayType) type;
        } else {
            arrayType = ast.newArrayType(type, 0);
            type = arrayType;
        }
        arrayType.dimensions().addAll(ASTNode.copySubtrees(ast, extraDimensions));
    }
    return type;
}
Also used : ArrayType(org.eclipse.jdt.core.dom.ArrayType) UnionType(org.eclipse.jdt.core.dom.UnionType) Type(org.eclipse.jdt.core.dom.Type) UnionType(org.eclipse.jdt.core.dom.UnionType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) Dimension(org.eclipse.jdt.core.dom.Dimension) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 64 with SingleVariableDeclaration

use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project generator by mybatis.

the class MethodSignatureStringifier method visitParameters.

@SuppressWarnings("unchecked")
private void visitParameters(MethodDeclaration node) {
    for (Iterator<SingleVariableDeclaration> it = node.parameters().iterator(); it.hasNext(); ) {
        SingleVariableDeclaration v = it.next();
        v.accept(this);
        if (it.hasNext()) {
            buffer.append(',');
        }
    }
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration)

Example 65 with SingleVariableDeclaration

use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project xtext-xtend by eclipse.

the class JavaASTFlattener method visit.

@Override
public boolean visit(final MethodDeclaration it) {
    Javadoc _javadoc = it.getJavadoc();
    boolean _tripleNotEquals = (_javadoc != null);
    if (_tripleNotEquals) {
        it.getJavadoc().accept(this);
    }
    final Function1<ASTNode, StringBuffer> _function = (ASTNode node) -> {
        StringBuffer _xifexpression = null;
        if ((node instanceof MethodDeclaration)) {
            StringBuffer _xifexpression_1 = null;
            boolean _isConstructor = ((MethodDeclaration) node).isConstructor();
            boolean _not = (!_isConstructor);
            if (_not) {
                StringBuffer _xifexpression_2 = null;
                boolean _isOverrideMethod = this._aSTFlattenerUtils.isOverrideMethod(((MethodDeclaration) node));
                if (_isOverrideMethod) {
                    _xifexpression_2 = this.appendToBuffer("override ");
                } else {
                    _xifexpression_2 = this.appendToBuffer("def ");
                }
                _xifexpression_1 = _xifexpression_2;
            }
            _xifexpression = _xifexpression_1;
        }
        return _xifexpression;
    };
    final Function1<ASTNode, StringBuffer> afterAnnotationProcessingCallback = _function;
    this.appendModifiers(it, it.modifiers(), afterAnnotationProcessingCallback);
    boolean _isPackageVisibility = this._aSTFlattenerUtils.isPackageVisibility(Iterables.<Modifier>filter(it.modifiers(), Modifier.class));
    if (_isPackageVisibility) {
        ASTNode _parent = it.getParent();
        if ((_parent instanceof TypeDeclaration)) {
            ASTNode _parent_1 = it.getParent();
            boolean _isInterface = ((TypeDeclaration) _parent_1).isInterface();
            boolean _not = (!_isInterface);
            if (_not) {
                this.appendToBuffer("package ");
            }
        }
    }
    boolean _isConstructor = it.isConstructor();
    if (_isConstructor) {
        this.appendToBuffer(" new");
    }
    boolean _isEmpty = it.typeParameters().isEmpty();
    boolean _not_1 = (!_isEmpty);
    if (_not_1) {
        boolean _isConstructor_1 = it.isConstructor();
        if (_isConstructor_1) {
            this.addProblem(it, "Type parameters for constructors are not supported");
        }
        this.appendTypeParameters(it.typeParameters());
    }
    boolean _isConstructor_2 = it.isConstructor();
    boolean _not_2 = (!_isConstructor_2);
    if (_not_2) {
        Type _returnType2 = it.getReturnType2();
        boolean _tripleNotEquals_1 = (_returnType2 != null);
        if (_tripleNotEquals_1) {
            it.getReturnType2().accept(this);
        } else {
            this.appendToBuffer("void");
        }
        this.appendSpaceToBuffer();
        it.getName().accept(this);
    }
    this.appendToBuffer("(");
    final Consumer<SingleVariableDeclaration> _function_1 = (SingleVariableDeclaration p) -> {
        Boolean _isAssignedInBody = this._aSTFlattenerUtils.isAssignedInBody(it.getBody(), p.getName());
        if ((_isAssignedInBody).booleanValue()) {
            if ((it.isConstructor() && (!it.getBody().statements().isEmpty()))) {
                final Expression firstInBody = IterableExtensions.<Expression>head(this._aSTFlattenerUtils.findAssignmentsInBlock(it.getBody(), p));
                if ((firstInBody != null)) {
                    ConstructorInvocation _findParentOfType = this._aSTFlattenerUtils.<ConstructorInvocation>findParentOfType(firstInBody, ConstructorInvocation.class);
                    boolean _tripleNotEquals_2 = (_findParentOfType != null);
                    if (_tripleNotEquals_2) {
                        this.addProblem(p, "Final parameter modified in constructor call");
                    } else {
                        SuperConstructorInvocation _findParentOfType_1 = this._aSTFlattenerUtils.<SuperConstructorInvocation>findParentOfType(firstInBody, SuperConstructorInvocation.class);
                        boolean _tripleNotEquals_3 = (_findParentOfType_1 != null);
                        if (_tripleNotEquals_3) {
                            this.addProblem(p, "Final parameter modified in super constructor call");
                        }
                    }
                }
            }
            final VariableDeclarationFragment varFrag = p.getAST().newVariableDeclarationFragment();
            varFrag.setName(p.getAST().newSimpleName(p.getName().toString()));
            AST _aST = p.getAST();
            SimpleName _name = p.getName();
            String _plus = (_name + "_finalParam_");
            p.setName(_aST.newSimpleName(_plus));
            varFrag.setInitializer(p.getAST().newSimpleName(p.getName().toString()));
            final VariableDeclarationStatement varDecl = p.getAST().newVariableDeclarationStatement(varFrag);
            ASTNode _createInstance = p.getAST().createInstance(SimpleType.class);
            final Type typeCopy = ((Type) _createInstance);
            varDecl.setType(typeCopy);
            it.getBody().statements().add(0, varDecl);
        }
    };
    ListExtensions.<SingleVariableDeclaration>reverseView(it.parameters()).forEach(_function_1);
    this.visitAllSeparatedByComma(it.parameters());
    this.appendToBuffer(")");
    this.appendExtraDimensions(it.getExtraDimensions());
    List<? extends ASTNode> throwsTypes = CollectionLiterals.<ASTNode>newArrayList();
    boolean _java8orHigher = this.java8orHigher();
    boolean _not_3 = (!_java8orHigher);
    if (_not_3) {
        throwsTypes = it.thrownExceptions();
    } else {
        throwsTypes = this._aSTFlattenerUtils.genericChildListProperty(it, "thrownExceptionTypes");
    }
    boolean _isEmpty_1 = throwsTypes.isEmpty();
    boolean _not_4 = (!_isEmpty_1);
    if (_not_4) {
        this.appendToBuffer(" throws ");
        this.visitAllSeparatedByComma(throwsTypes);
    }
    this.appendSpaceToBuffer();
    Block _body = it.getBody();
    boolean _tripleNotEquals_2 = (_body != null);
    if (_tripleNotEquals_2) {
        it.getBody().accept(this);
    } else {
        this.appendLineWrapToBuffer();
    }
    return false;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Javadoc(org.eclipse.jdt.core.dom.Javadoc) SimpleType(org.eclipse.jdt.core.dom.SimpleType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) WildcardType(org.eclipse.jdt.core.dom.WildcardType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration)

Aggregations

SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)124 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)41 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)39 Type (org.eclipse.jdt.core.dom.Type)37 Block (org.eclipse.jdt.core.dom.Block)35 ASTNode (org.eclipse.jdt.core.dom.ASTNode)33 AST (org.eclipse.jdt.core.dom.AST)32 ArrayList (java.util.ArrayList)29 List (java.util.List)27 SimpleName (org.eclipse.jdt.core.dom.SimpleName)27 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)27 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)26 Expression (org.eclipse.jdt.core.dom.Expression)22 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)20 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)18 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)18 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)17 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)16 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)16 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)16