Example usage for org.eclipse.jdt.core.dom MethodDeclaration parameters

List of usage examples for org.eclipse.jdt.core.dom MethodDeclaration parameters

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom MethodDeclaration parameters.

Prototype

ASTNode.NodeList parameters

To view the source code for org.eclipse.jdt.core.dom MethodDeclaration parameters.

Click Source Link

Document

The parameter declarations (element type: SingleVariableDeclaration ).

Usage

From source file:at.bestsolution.fxide.jdt.corext.dom.ASTFlattener.java

License:Open Source License

@Override
public boolean visit(MethodDeclaration node) {
    if (node.getJavadoc() != null) {
        node.getJavadoc().accept(this);
    }//www  .  jav  a 2s.co m
    if (node.getAST().apiLevel() >= JLS3) {
        printModifiers(node.modifiers());
        if (!node.typeParameters().isEmpty()) {
            this.fBuffer.append("<");//$NON-NLS-1$
            for (Iterator<TypeParameter> it = node.typeParameters().iterator(); it.hasNext();) {
                TypeParameter t = it.next();
                t.accept(this);
                if (it.hasNext()) {
                    this.fBuffer.append(", ");//$NON-NLS-1$
                }
            }
            this.fBuffer.append("> ");//$NON-NLS-1$
        }
    }
    if (!node.isConstructor()) {
        if (node.getReturnType2() != null) {
            node.getReturnType2().accept(this);
        } else {
            // methods really ought to have a return type
            this.fBuffer.append("void");//$NON-NLS-1$
        }
        this.fBuffer.append(" ");//$NON-NLS-1$
    }
    node.getName().accept(this);
    this.fBuffer.append("(");//$NON-NLS-1$
    if (node.getAST().apiLevel() >= AST.JLS8) {
        Type receiverType = node.getReceiverType();
        if (receiverType != null) {
            receiverType.accept(this);
            this.fBuffer.append(' ');
            SimpleName qualifier = node.getReceiverQualifier();
            if (qualifier != null) {
                qualifier.accept(this);
                this.fBuffer.append('.');
            }
            this.fBuffer.append("this"); //$NON-NLS-1$
            if (node.parameters().size() > 0) {
                this.fBuffer.append(',');
            }
        }
    }
    for (Iterator<SingleVariableDeclaration> it = node.parameters().iterator(); it.hasNext();) {
        SingleVariableDeclaration v = it.next();
        v.accept(this);
        if (it.hasNext()) {
            this.fBuffer.append(", ");//$NON-NLS-1$
        }
    }
    this.fBuffer.append(")");//$NON-NLS-1$
    if (node.getAST().apiLevel() >= AST.JLS8) {
        List<Dimension> dimensions = node.extraDimensions();
        for (Iterator<Dimension> it = dimensions.iterator(); it.hasNext();) {
            Dimension e = it.next();
            e.accept(this);
        }
    } else {
        for (int i = 0; i < node.getExtraDimensions(); i++) {
            this.fBuffer.append("[]"); //$NON-NLS-1$
        }
    }
    List<? extends ASTNode> thrownExceptions = node.getAST().apiLevel() >= AST.JLS8
            ? node.thrownExceptionTypes()
            : getThrownExceptions(node);
    if (!thrownExceptions.isEmpty()) {
        this.fBuffer.append(" throws ");//$NON-NLS-1$
        for (Iterator<? extends ASTNode> it = thrownExceptions.iterator(); it.hasNext();) {
            ASTNode n = it.next();
            n.accept(this);
            if (it.hasNext()) {
                this.fBuffer.append(", ");//$NON-NLS-1$
            }
        }
        this.fBuffer.append(" ");//$NON-NLS-1$
    }
    if (node.getBody() == null) {
        this.fBuffer.append(";");//$NON-NLS-1$
    } else {
        node.getBody().accept(this);
    }
    return false;
}

From source file:boa.datagen.util.Java7Visitor.java

License:Apache License

@Override
public boolean visit(MethodDeclaration node) {
    List<boa.types.Ast.Method> list = methods.peek();
    Method.Builder b = Method.newBuilder();
    //      b.setPosition(pos.build());
    if (node.isConstructor())
        b.setName("<init>");
    else//w  w w. j  av a  2 s  . co m
        b.setName(node.getName().getFullyQualifiedName());
    for (Object m : node.modifiers()) {
        if (((IExtendedModifier) m).isAnnotation())
            ((Annotation) m).accept(this);
        else
            ((org.eclipse.jdt.core.dom.Modifier) m).accept(this);
        b.addModifiers(modifiers.pop());
    }
    boa.types.Ast.Type.Builder tb = boa.types.Ast.Type.newBuilder();
    if (node.getReturnType2() != null) {
        String name = typeName(node.getReturnType2());
        for (int i = 0; i < node.getExtraDimensions(); i++)
            name += "[]";
        tb.setName(getIndex(name));
        tb.setKind(boa.types.Ast.TypeKind.OTHER);
        b.setReturnType(tb.build());
    } else {
        tb.setName(getIndex("void"));
        tb.setKind(boa.types.Ast.TypeKind.OTHER);
        b.setReturnType(tb.build());
    }
    for (Object t : node.typeParameters()) {
        boa.types.Ast.Type.Builder tp = boa.types.Ast.Type.newBuilder();
        String name = ((TypeParameter) t).getName().getFullyQualifiedName();
        String bounds = "";
        for (Object o : ((TypeParameter) t).typeBounds()) {
            if (bounds.length() > 0)
                bounds += " & ";
            bounds += typeName((org.eclipse.jdt.core.dom.Type) o);
        }
        if (bounds.length() > 0)
            name = name + " extends " + bounds;
        tp.setName(getIndex(name));
        tp.setKind(boa.types.Ast.TypeKind.GENERIC);
        b.addGenericParameters(tp.build());
    }
    for (Object o : node.parameters()) {
        SingleVariableDeclaration ex = (SingleVariableDeclaration) o;
        Variable.Builder vb = Variable.newBuilder();
        //         vb.setPosition(pos.build()); // FIXME
        vb.setName(ex.getName().getFullyQualifiedName());
        for (Object m : ex.modifiers()) {
            if (((IExtendedModifier) m).isAnnotation())
                ((Annotation) m).accept(this);
            else
                ((org.eclipse.jdt.core.dom.Modifier) m).accept(this);
            vb.addModifiers(modifiers.pop());
        }
        boa.types.Ast.Type.Builder tp = boa.types.Ast.Type.newBuilder();
        String name = typeName(ex.getType());
        for (int i = 0; i < ex.getExtraDimensions(); i++)
            name += "[]";
        if (ex.isVarargs())
            name += "...";
        tp.setName(getIndex(name));
        tp.setKind(boa.types.Ast.TypeKind.OTHER);
        vb.setVariableType(tp.build());
        if (ex.getInitializer() != null) {
            ex.getInitializer().accept(this);
            vb.setInitializer(expressions.pop());
        }
        b.addArguments(vb.build());
    }
    for (Object o : node.thrownExceptions()) {
        boa.types.Ast.Type.Builder tp = boa.types.Ast.Type.newBuilder();
        tp.setName(getIndex(((Name) o).getFullyQualifiedName()));
        tp.setKind(boa.types.Ast.TypeKind.CLASS);
        b.addExceptionTypes(tp.build());
    }
    if (node.getBody() != null) {
        statements.push(new ArrayList<boa.types.Ast.Statement>());
        node.getBody().accept(this);
        for (boa.types.Ast.Statement s : statements.pop())
            b.addStatements(s);
    }
    list.add(b.build());
    return false;
}

From source file:boa.datagen.util.Java8Visitor.java

License:Apache License

@Override
public boolean visit(MethodDeclaration node) {
    List<boa.types.Ast.Method> list = methods.peek();
    Method.Builder b = Method.newBuilder();
    //      b.setPosition(pos.build());
    if (node.isConstructor())
        b.setName("<init>");
    else//from w  w  w. j  av  a2 s  .  c o m
        b.setName(node.getName().getFullyQualifiedName());
    for (Object m : node.modifiers()) {
        if (((IExtendedModifier) m).isAnnotation())
            ((Annotation) m).accept(this);
        else
            ((org.eclipse.jdt.core.dom.Modifier) m).accept(this);
        b.addModifiers(modifiers.pop());
    }
    boa.types.Ast.Type.Builder tb = boa.types.Ast.Type.newBuilder();
    if (node.getReturnType2() != null) {
        String name = typeName(node.getReturnType2());
        // FIXME JLS8: Deprecated getExtraDimensions() and added extraDimensions()
        for (int i = 0; i < node.getExtraDimensions(); i++)
            name += "[]";
        tb.setName(getIndex(name));
        tb.setKind(boa.types.Ast.TypeKind.OTHER);
        b.setReturnType(tb.build());
    } else {
        tb.setName(getIndex("void"));
        tb.setKind(boa.types.Ast.TypeKind.OTHER);
        b.setReturnType(tb.build());
    }
    for (Object t : node.typeParameters()) {
        boa.types.Ast.Type.Builder tp = boa.types.Ast.Type.newBuilder();
        String name = ((TypeParameter) t).getName().getFullyQualifiedName();
        String bounds = "";
        for (Object o : ((TypeParameter) t).typeBounds()) {
            if (bounds.length() > 0)
                bounds += " & ";
            bounds += typeName((org.eclipse.jdt.core.dom.Type) o);
        }
        if (bounds.length() > 0)
            name = name + " extends " + bounds;
        tp.setName(getIndex(name));
        tp.setKind(boa.types.Ast.TypeKind.GENERIC);
        b.addGenericParameters(tp.build());
    }
    if (node.getReceiverType() != null) {
        Variable.Builder vb = Variable.newBuilder();
        //         vb.setPosition(pos.build()); // FIXME
        vb.setName("this");
        boa.types.Ast.Type.Builder tp = boa.types.Ast.Type.newBuilder();
        String name = typeName(node.getReceiverType());
        if (node.getReceiverQualifier() != null)
            name = node.getReceiverQualifier().getFullyQualifiedName() + "." + name;
        tp.setName(getIndex(name));
        tp.setKind(boa.types.Ast.TypeKind.OTHER); // FIXME change to receiver? or something?
        vb.setVariableType(tp.build());
        b.addArguments(vb.build());
    }
    for (Object o : node.parameters()) {
        SingleVariableDeclaration ex = (SingleVariableDeclaration) o;
        Variable.Builder vb = Variable.newBuilder();
        //         vb.setPosition(pos.build()); // FIXME
        vb.setName(ex.getName().getFullyQualifiedName());
        for (Object m : ex.modifiers()) {
            if (((IExtendedModifier) m).isAnnotation())
                ((Annotation) m).accept(this);
            else
                ((org.eclipse.jdt.core.dom.Modifier) m).accept(this);
            vb.addModifiers(modifiers.pop());
        }
        boa.types.Ast.Type.Builder tp = boa.types.Ast.Type.newBuilder();
        String name = typeName(ex.getType());
        // FIXME JLS8: Deprecated getExtraDimensions() and added extraDimensions()
        for (int i = 0; i < ex.getExtraDimensions(); i++)
            name += "[]";
        if (ex.isVarargs())
            name += "...";
        tp.setName(getIndex(name));
        tp.setKind(boa.types.Ast.TypeKind.OTHER);
        vb.setVariableType(tp.build());
        if (ex.getInitializer() != null) {
            ex.getInitializer().accept(this);
            vb.setInitializer(expressions.pop());
        }
        b.addArguments(vb.build());
    }
    for (Object o : node.thrownExceptionTypes()) {
        boa.types.Ast.Type.Builder tp = boa.types.Ast.Type.newBuilder();
        tb.setName(getIndex(typeName((org.eclipse.jdt.core.dom.Type) o)));
        tp.setKind(boa.types.Ast.TypeKind.CLASS);
        b.addExceptionTypes(tp.build());
    }
    if (node.getBody() != null) {
        statements.push(new ArrayList<boa.types.Ast.Statement>());
        node.getBody().accept(this);
        for (boa.types.Ast.Statement s : statements.pop())
            b.addStatements(s);
    }
    list.add(b.build());
    return false;
}

From source file:br.com.objectos.way.core.code.jdt.ASTTest.java

License:Apache License

@SuppressWarnings("unchecked")
public void stackoverflow_answer() {
    AST ast = AST.newAST(AST.JLS8);/*from w ww  .jav  a  2s .co m*/
    CompilationUnit cu = ast.newCompilationUnit();

    PackageDeclaration p1 = ast.newPackageDeclaration();
    p1.setName(ast.newSimpleName("foo"));
    cu.setPackage(p1);

    ImportDeclaration id = ast.newImportDeclaration();
    id.setName(ast.newName(new String[] { "java", "util", "Set" }));
    cu.imports().add(id);

    TypeDeclaration td = ast.newTypeDeclaration();
    td.setName(ast.newSimpleName("Foo"));
    TypeParameter tp = ast.newTypeParameter();
    tp.setName(ast.newSimpleName("X"));
    td.typeParameters().add(tp);
    cu.types().add(td);

    MethodDeclaration md = ast.newMethodDeclaration();
    md.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
    md.setName(ast.newSimpleName("bar"));

    SingleVariableDeclaration var = ast.newSingleVariableDeclaration();
    var.setType(ast.newSimpleType(ast.newSimpleName("String")));
    var.setName(ast.newSimpleName("a"));
    md.parameters().add(var);
    td.bodyDeclarations().add(md);

    Block block = ast.newBlock();
    md.setBody(block);

    MethodInvocation mi = ast.newMethodInvocation();
    mi.setName(ast.newSimpleName("x"));

    ExpressionStatement e = ast.newExpressionStatement(mi);
    block.statements().add(e);

    System.out.println(cu);
}

From source file:ca.ecliptical.pde.internal.ds.AnnotationProcessor.java

License:Open Source License

private boolean hasDefaultConstructor(TypeDeclaration type) {
    boolean hasConstructor = false;
    for (MethodDeclaration method : type.getMethods()) {
        if (method.isConstructor()) {
            hasConstructor = true;//from   w w w . j a v  a2  s.com
            if (Modifier.isPublic(method.getModifiers()) && method.parameters().isEmpty())
                return true;
        }
    }

    return !hasConstructor;
}

From source file:cc.kave.eclipse.namefactory.NodeFactory.java

License:Apache License

private static String[] createParameterNames(MethodDeclaration methodDecl, IMethodBinding method) {
    ITypeBinding[] parameterTypes = method.getParameterTypes();
    String[] parameters = new String[parameterTypes.length];

    for (int i = 0; i < parameterTypes.length; i++) {
        StringBuilder param = new StringBuilder();

        if (i == parameterTypes.length - 1 && method.isVarargs()) {
            param.append("params ");
        }// w  ww  .j av a  2  s  .c  o m

        param.append("[");
        param.append(BindingFactory.getBindingName(parameterTypes[i]));
        param.append("] ");

        if (methodDecl != null) {
            param.append(
                    ((SingleVariableDeclaration) methodDecl.parameters().get(i)).getName().getIdentifier());
        } else {
            param.append("?");
        }
        parameters[i] = CsParameterName.newParameterName(param.toString()).getIdentifier();
    }

    return parameters;
}

From source file:cc.kave.eclipse.namefactory.visitors.MethodDeclarationVisitor.java

License:Apache License

private String getMethodSignature(MethodDeclaration declaration) {
    StringBuilder sb = new StringBuilder();
    sb.append(declaration.getName().getIdentifier());
    sb.append("(");

    for (Object variable : declaration.parameters()) {
        SingleVariableDeclaration v = (SingleVariableDeclaration) variable;
        sb.append(v.resolveBinding().getType().getName());
        sb.append(" ");
        sb.append(v.getName().getIdentifier());
        sb.append(", ");
    }/*from   w w w . j a  v a 2 s  .co m*/

    if (sb.toString().endsWith(", ")) {
        sb.replace(sb.length() - 2, sb.length(), ")");
    } else {
        sb.append(")");
    }

    return sb.toString();
}

From source file:changetypes.ASTVisitorAtomicChange.java

License:Open Source License

public boolean visit(MethodDeclaration node) {
    IMethodBinding mtb = node.resolveBinding();
    this.mtbStack.push(mtb);
    String nodeStr = node.toString();

    String modifier = "protected";
    int dex = nodeStr.indexOf(' ');
    if (dex >= 0) {
        String temp = nodeStr.substring(0, dex);
        if (temp.equals("public")) {
            modifier = "public";
        } else if (temp.equals("private")) {
            modifier = "private";
        }//from  w w  w . ja va  2 s  . c o m
    }
    try {
        String visibility = getModifier(mtb);
        this.facts.add(Fact.makeMethodFact(getQualifiedName(mtb), getSimpleName(mtb),
                getQualifiedName(mtb.getDeclaringClass()), visibility));
    } catch (Exception localException1) {
        System.err.println("Cannot resolve return method bindings for method " + node.getName().toString());
    }
    try {
        String returntype = getQualifiedName(mtb.getReturnType());
        this.facts.add(Fact.makeReturnsFact(getQualifiedName(mtb), returntype));
    } catch (Exception localException2) {
        System.err.println("Cannot resolve return type bindings for method " + node.getName().toString());
    }
    try {
        this.facts.add(Fact.makeModifierMethodFact(getQualifiedName(mtb), modifier));
    } catch (Exception localException3) {
        System.err.println(
                "Cannot resolve return type bindings for method modifier " + node.getName().toString());
    }
    try {
        String bodystring = node.getBody() != null ? node.getBody().toString() : "";
        bodystring = bodystring.replace('\n', ' ');

        bodystring = bodystring.replace('"', ' ');
        bodystring = bodystring.replace('"', ' ');
        bodystring = bodystring.replace('\\', ' ');

        this.facts.add(Fact.makeMethodBodyFact(getQualifiedName(mtb), bodystring));
    } catch (Exception localException4) {
        System.err.println("Cannot resolve bindings for body");
    }
    SingleVariableDeclaration param;
    try {
        List<SingleVariableDeclaration> parameters = node.parameters();

        StringBuilder sb = new StringBuilder();
        for (Iterator localIterator = parameters.iterator(); localIterator.hasNext();) {
            param = (SingleVariableDeclaration) localIterator.next();
            if (sb.length() != 0) {
                sb.append(", ");
            }
            sb.append(param.getType().toString());
            sb.append(":");
            sb.append(param.getName().toString());
        }
        this.facts.add(Fact.makeParameterFact(getQualifiedName(mtb), sb.toString(), ""));
    } catch (Exception localException5) {
        System.err.println("Cannot resolve bindings for parameters");
    }
    try {
        List<Name> thrownTypes = node.thrownExceptions();
        for (Name n : thrownTypes) {
            this.facts.add(Fact.makeThrownExceptionFact(getQualifiedName(mtb),
                    getQualifiedName(n.resolveTypeBinding())));
        }
    } catch (Exception localException6) {
        System.err.println("Cannot resolve bindings for exceptions");
    }
    return true;
}

From source file:chibi.gumtreediff.gen.jdt.cd.CdJdtVisitor.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from   ww  w  .j av  a 2 s . com
public boolean visit(MethodDeclaration node) {
    if (node.getJavadoc() != null) {
        node.getJavadoc().accept(this);
    }
    fInMethodDeclaration = true;

    // @Inria
    pushNode(node, node.getName().toString());
    //

    visitListAsNode(EntityType.MODIFIERS, node.modifiers());
    if (node.getReturnType2() != null) {
        node.getReturnType2().accept(this);
    }
    visitListAsNode(EntityType.TYPE_ARGUMENTS, node.typeParameters());
    visitListAsNode(EntityType.PARAMETERS, node.parameters());
    visitListAsNode(EntityType.THROW, node.thrownExceptions());

    // @Inria
    // The body can be null when the method declaration is from a interface
    if (node.getBody() != null) {
        node.getBody().accept(this);
    }
    return false;

}

From source file:cn.ieclipse.adt.ext.jdt.SourceGenerator.java

License:Apache License

private static void genInnerSQLiteOpenHelper(AnonymousClassDeclaration acd, AST ast,
        List<String> tableCreators) {
    MethodDeclaration md = ast.newMethodDeclaration();
    md.modifiers().addAll(ast.newModifiers((Modifier.PUBLIC)));
    md.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));
    md.setName(ast.newSimpleName("onCreate"));
    SingleVariableDeclaration svd = ast.newSingleVariableDeclaration();
    svd.setName(ast.newSimpleName("db"));
    svd.setType(ast.newSimpleType(ast.newSimpleName("SQLiteDatabase")));
    md.parameters().add(svd);
    Block innerBlock = ast.newBlock();//from   w  ww . j a v  a  2  s.  c  o m
    md.setBody(innerBlock);
    VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
    vdf.setName(ast.newSimpleName("sql"));
    StringLiteral sl = ast.newStringLiteral();
    sl.setLiteralValue("");
    vdf.setInitializer(sl);
    VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
    vds.setType(ast.newSimpleType(ast.newSimpleName("String")));
    innerBlock.statements().add(vds);
    for (String creator : tableCreators) {
        String[] lines = creator.split(SourceAnalysis.LF);
        for (String line : lines) {
            Assignment a = ast.newAssignment();
            a.setOperator(Assignment.Operator.PLUS_ASSIGN);
            a.setLeftHandSide(ast.newSimpleName("sql"));
            StringLiteral temp = ast.newStringLiteral();
            temp.setLiteralValue(line);
            a.setRightHandSide(temp);
            innerBlock.statements().add(ast.newExpressionStatement(a));
        }

        MethodInvocation mi = ast.newMethodInvocation();
        mi.setName(ast.newSimpleName("execSQL"));
        mi.setExpression(ast.newSimpleName("db"));
        mi.arguments().add(ast.newSimpleName("sql"));
        innerBlock.statements().add(ast.newExpressionStatement(mi));
    }

    acd.bodyDeclarations().add(md);
    // onUpgrade
    md = ast.newMethodDeclaration();
    md.modifiers().addAll(ast.newModifiers((Modifier.PUBLIC)));
    md.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));
    md.setName(ast.newSimpleName("onUpgrade"));
    svd = ast.newSingleVariableDeclaration();
    svd.setName(ast.newSimpleName("db"));
    svd.setType(ast.newSimpleType(ast.newSimpleName("SQLiteDatabase")));
    md.parameters().add(svd);

    svd = ast.newSingleVariableDeclaration();
    svd.setName(ast.newSimpleName("oldVersion"));
    svd.setType(ast.newPrimitiveType(PrimitiveType.INT));
    md.parameters().add(svd);

    svd = ast.newSingleVariableDeclaration();
    svd.setName(ast.newSimpleName("newVersion"));
    svd.setType(ast.newPrimitiveType(PrimitiveType.INT));
    md.parameters().add(svd);

    innerBlock = ast.newBlock();
    md.setBody(innerBlock);
    acd.bodyDeclarations().add(md);
}