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

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

Introduction

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

Prototype

ASTNode.NodeList thrownExceptions

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

Click Source Link

Document

The list of thrown exception names (element type: Name ).

Usage

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

License:Open Source License

/**
 * @param node node// w  w  w . j a  va  2s  .  c  o  m
 * @return thrown exception names
 * @deprecated to avoid deprecation warning
 */
@Deprecated
private static List<Name> getThrownExceptions(MethodDeclaration node) {
    return node.thrownExceptions();
}

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  a v  a2s .c  om
        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: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  ww  .  j  av  a 2  s  .c om*/
    }
    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/*  w  w w . j  a  va2  s. co m*/
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:coloredide.utils.CopiedNaiveASTFlattener.java

License:Open Source License

public boolean visit(MethodDeclaration node) {
    if (node.getJavadoc() != null) {
        node.getJavadoc().accept(this);
    }//  w  w w  . ja va2  s  .  c o  m
    printIndent();
    hook_beforeVisitMethodDeclaration(node);
    if (node.getAST().apiLevel() >= AST.JLS3) {
        printModifiers(node.modifiers());
        if (!node.typeParameters().isEmpty()) {
            this.buffer.append("<");//$NON-NLS-1$
            for (Iterator it = node.typeParameters().iterator(); it.hasNext();) {
                TypeParameter t = (TypeParameter) it.next();
                t.accept(this);
                if (it.hasNext()) {
                    this.buffer.append(",");//$NON-NLS-1$
                }
            }
            this.buffer.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.buffer.append("void");//$NON-NLS-1$
        }

        this.buffer.append(" ");//$NON-NLS-1$
    }
    node.getName().accept(this);
    this.buffer.append("(");//$NON-NLS-1$
    for (Iterator it = node.parameters().iterator(); it.hasNext();) {
        SingleVariableDeclaration v = (SingleVariableDeclaration) it.next();
        v.accept(this);
        if (it.hasNext()) {
            this.buffer.append(",");//$NON-NLS-1$
        }
    }
    this.buffer.append(")");//$NON-NLS-1$
    for (int i = 0; i < node.getExtraDimensions(); i++) {
        this.buffer.append("[]"); //$NON-NLS-1$
    }
    if (!node.thrownExceptions().isEmpty()) {
        this.buffer.append(" throws ");//$NON-NLS-1$
        for (Iterator it = node.thrownExceptions().iterator(); it.hasNext();) {
            Name n = (Name) it.next();
            n.accept(this);
            if (it.hasNext()) {
                this.buffer.append(", ");//$NON-NLS-1$
            }
        }
        this.buffer.append(" ");//$NON-NLS-1$
    }
    if (node.getBody() == null) {
        this.buffer.append(";\n");//$NON-NLS-1$
    } else {
        node.getBody().accept(this);
    }
    return false;
}

From source file:com.codenvy.ide.ext.java.server.dom.ASTFlattener.java

License:Open Source License

/**
 * @param node//from w w w .jav a  2  s.c  om
 *         node
 * @return thrown exception names
 * @deprecated to avoid deprecation warning
 */
private static List<Name> getThrownExceptions(MethodDeclaration node) {
    return node.thrownExceptions();
}

From source file:com.google.gdt.eclipse.designer.builders.GwtBuilder.java

License:Open Source License

/**
 * Generates Async type for given <code>RemoteService</code>.
 *//*from   ww  w . j a va 2 s  .co m*/
private void generateAsync(IPackageFragment servicePackage, ICompilationUnit serviceUnit) throws Exception {
    IJavaProject javaProject = serviceUnit.getJavaProject();
    // parse service unit
    CompilationUnit serviceRoot = Utils.parseUnit(serviceUnit);
    // prepare AST and start modifications recording
    AST ast = serviceRoot.getAST();
    serviceRoot.recordModifications();
    // modify imports (-com.google.gwt.*, -*Exception, +AsyncCallback) 
    {
        List<ImportDeclaration> imports = DomGenerics.imports(serviceRoot);
        // remove useless imports
        for (Iterator<ImportDeclaration> I = imports.iterator(); I.hasNext();) {
            ImportDeclaration importDeclaration = I.next();
            String importName = importDeclaration.getName().getFullyQualifiedName();
            if (importName.startsWith("com.google.gwt.user.client.rpc.")
                    || importName.equals("com.google.gwt.core.client.GWT")
                    || importName.endsWith("Exception")) {
                I.remove();
            }
        }
    }
    // add Async to the name
    TypeDeclaration serviceType = (TypeDeclaration) serviceRoot.types().get(0);
    String remoteServiceAsyncName = serviceType.getName().getIdentifier() + "Async";
    serviceType.setName(serviceRoot.getAST().newSimpleName(remoteServiceAsyncName));
    // update interfaces
    updateInterfacesOfAsync(javaProject, serviceRoot, serviceType);
    // change methods, fields and inner classes
    {
        List<BodyDeclaration> bodyDeclarations = DomGenerics.bodyDeclarations(serviceType);
        for (Iterator<BodyDeclaration> I = bodyDeclarations.iterator(); I.hasNext();) {
            BodyDeclaration bodyDeclaration = I.next();
            if (bodyDeclaration instanceof MethodDeclaration) {
                MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDeclaration;
                // make return type void
                Type returnType;
                {
                    returnType = methodDeclaration.getReturnType2();
                    methodDeclaration.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));
                }
                // process JavaDoc
                {
                    Javadoc javadoc = methodDeclaration.getJavadoc();
                    if (javadoc != null) {
                        List<TagElement> tags = DomGenerics.tags(javadoc);
                        for (Iterator<TagElement> tagIter = tags.iterator(); tagIter.hasNext();) {
                            TagElement tag = tagIter.next();
                            if ("@gwt.typeArgs".equals(tag.getTagName())) {
                                tagIter.remove();
                            } else if ("@return".equals(tag.getTagName())) {
                                if (!tag.fragments().isEmpty()) {
                                    tag.setTagName("@param callback the callback to return");
                                } else {
                                    tagIter.remove();
                                }
                            } else if ("@wbp.gwt.Request".equals(tag.getTagName())) {
                                tagIter.remove();
                                addImport(serviceRoot, "com.google.gwt.http.client.Request");
                                methodDeclaration.setReturnType2(ast.newSimpleType(ast.newName("Request")));
                            }
                        }
                        // remove empty JavaDoc
                        if (tags.isEmpty()) {
                            methodDeclaration.setJavadoc(null);
                        }
                    }
                }
                // add AsyncCallback parameter
                {
                    addImport(serviceRoot, "com.google.gwt.user.client.rpc.AsyncCallback");
                    // prepare "callback" type
                    Type callbackType;
                    {
                        callbackType = ast.newSimpleType(ast.newName("AsyncCallback"));
                        Type objectReturnType = getObjectType(returnType);
                        ParameterizedType parameterizedType = ast.newParameterizedType(callbackType);
                        DomGenerics.typeArguments(parameterizedType).add(objectReturnType);
                        callbackType = parameterizedType;
                    }
                    // prepare "callback" parameter
                    SingleVariableDeclaration asyncCallback = ast.newSingleVariableDeclaration();
                    asyncCallback.setType(callbackType);
                    asyncCallback.setName(ast.newSimpleName("callback"));
                    // add "callback" parameter
                    DomGenerics.parameters(methodDeclaration).add(asyncCallback);
                }
                // remove throws
                methodDeclaration.thrownExceptions().clear();
            } else if (bodyDeclaration instanceof FieldDeclaration
                    || bodyDeclaration instanceof TypeDeclaration) {
                // remove the fields and inner classes
                I.remove();
            }
        }
    }
    // apply modifications to prepare new source code
    String newSource;
    {
        String source = serviceUnit.getBuffer().getContents();
        Document document = new Document(source);
        // prepare text edits
        MultiTextEdit edits = (MultiTextEdit) serviceRoot.rewrite(document, javaProject.getOptions(true));
        removeAnnotations(serviceType, source, edits);
        // prepare new source code
        edits.apply(document);
        newSource = document.get();
    }
    // update compilation unit
    {
        ICompilationUnit unit = servicePackage.createCompilationUnit(remoteServiceAsyncName + ".java",
                newSource, true, null);
        unit.getBuffer().save(null, true);
    }
}

From source file:com.idega.eclipse.ejbwizards.BeanCreator.java

License:Open Source License

protected MethodDeclaration getMethodDeclaration(AST ast, IMethod method, String methodName, String returnType,
        Set imports, boolean addJavadoc) throws JavaModelException {
    String[] exceptions = method.getExceptionTypes();
    String[] parameterTypes = method.getParameterTypes();
    String[] parameterNames = method.getParameterNames();

    MethodDeclaration methodConstructor = ast.newMethodDeclaration();
    methodConstructor.setConstructor(false);
    methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
    methodConstructor.setReturnType2(getType(ast, returnType));
    methodConstructor.setName(ast.newSimpleName(methodName));
    if (returnType != null) {
        imports.add(getImportSignature(returnType));
    }/*from w  w  w  .j a  v a2  s.  c o  m*/

    for (int i = 0; i < exceptions.length; i++) {
        methodConstructor.thrownExceptions()
                .add(ast.newSimpleName(Signature.getSignatureSimpleName(exceptions[i])));
        imports.add(getImportSignature(Signature.toString(exceptions[i])));
    }

    for (int i = 0; i < parameterTypes.length; i++) {
        String parameterType = getReturnType(parameterTypes[i]);

        SingleVariableDeclaration variableDeclaration = ast.newSingleVariableDeclaration();
        variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE));
        variableDeclaration.setType(getType(ast, parameterType));
        variableDeclaration.setName(ast.newSimpleName(parameterNames[i]));
        methodConstructor.parameters().add(variableDeclaration);

        imports.add(getImportSignature(Signature.toString(parameterTypes[i])));
    }

    if (addJavadoc) {
        methodConstructor.setJavadoc(getJavadoc(ast, method));
    }

    return methodConstructor;
}

From source file:com.idega.eclipse.ejbwizards.IBOEntityCreator.java

License:Open Source License

private void createInterface(IProgressMonitor monitor, ICompilationUnit iUnit, String typePackage, String name,
        String[] interfaces) throws JavaModelException, MalformedTreeException, BadLocationException {
    String source = iUnit.getBuffer().getContents();
    Document document = new Document(source);

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(iUnit);/*from  ww  w.  j  a va2  s  .c o  m*/

    CompilationUnit unit = (CompilationUnit) parser.createAST(monitor);
    unit.recordModifications();

    AST ast = unit.getAST();

    // Package statement
    unit.setPackage(getPackageDeclaration(ast, typePackage));

    // class declaration
    String superInterface = null;
    if (!this.isSessionBean) {
        superInterface = "IBOService";
        addInterfaceImport("com.idega.business.IBOService");
    } else {
        superInterface = "IBOSession";
        addInterfaceImport("com.idega.business.IBOSession");
    }

    TypeDeclaration classType = getTypeDeclaration(ast, name, true, superInterface, interfaces,
            getInterfaceImports());
    unit.types().add(classType);

    MethodFilter[] nonValidFilter = {
            new MethodFilter(getType().getTypeQualifiedName(), MethodFilter.TYPE_WHOLE),
            new MethodFilter(WizardConstants.INITIALIZE_ATTRIBUTES, MethodFilter.TYPE_WHOLE),
            new MethodFilter(WizardConstants.SET_DEFAULT_VALUES, MethodFilter.TYPE_WHOLE),
            new MethodFilter(WizardConstants.INSERT_START_DATA, MethodFilter.TYPE_WHOLE),
            new MethodFilter(WizardConstants.GET_ENTITY_NAME, MethodFilter.TYPE_WHOLE),
            new MethodFilter(WizardConstants.UPDATE, MethodFilter.TYPE_WHOLE),
            new MethodFilter(WizardConstants.DELETE, MethodFilter.TYPE_WHOLE),
            new MethodFilter(WizardConstants.INSERT, MethodFilter.TYPE_WHOLE),
            new MethodFilter(WizardConstants.REMOVE, MethodFilter.TYPE_WHOLE),
            new MethodFilter(WizardConstants.GET_NAME_OF_MIDDLE_TABLE, MethodFilter.TYPE_WHOLE),
            new MethodFilter(WizardConstants.GET_ID_COLUMN_NAME, MethodFilter.TYPE_WHOLE),
            new MethodFilter(WizardConstants.EJB_START, MethodFilter.TYPE_PREFIX) };

    List methods = filterMethods(getType().getMethods(), null, nonValidFilter);
    for (Iterator iter = methods.iterator(); iter.hasNext();) {
        IMethod method = (IMethod) iter.next();
        MethodDeclaration methodConstructor = getMethodDeclaration(ast, method, getInterfaceImports());
        classType.bodyDeclarations().add(methodConstructor);

        methodConstructor.thrownExceptions().add(ast.newSimpleName("RemoteException"));
        addInterfaceImport("java.rmi.RemoteException");
    }

    writeImports(ast, unit, getInterfaceImports());
    commitChanges(iUnit, unit, document);
}

From source file:com.idega.eclipse.ejbwizards.IBOEntityCreator.java

License:Open Source License

private void createHomeInterface(IProgressMonitor monitor, ICompilationUnit iUnit, String typePackage,
        String name) throws JavaModelException, MalformedTreeException, BadLocationException {
    iUnit.getBuffer().setContents("");
    String source = iUnit.getBuffer().getContents();
    Document document = new Document(source);

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(iUnit);//from   www  .j  av  a2  s  .c om

    CompilationUnit unit = (CompilationUnit) parser.createAST(null);
    unit.recordModifications();

    AST ast = unit.getAST();

    // Package statement
    unit.setPackage(getPackageDeclaration(ast, typePackage));

    // class declaration
    TypeDeclaration classType = getTypeDeclaration(ast, name + "Home", true, "IBOHome", null,
            getHomeInterfaceImports());
    addHomeInterfaceImport("com.idega.business.IBOHome");
    unit.types().add(classType);

    // create() method
    MethodDeclaration methodConstructor = ast.newMethodDeclaration();
    methodConstructor.setConstructor(false);
    methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
    methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name)));
    methodConstructor.setName(ast.newSimpleName("create"));
    methodConstructor.thrownExceptions().add(ast.newName("CreateException"));
    addHomeInterfaceImport("javax.ejb.CreateException");
    methodConstructor.thrownExceptions().add(ast.newName("RemoteException"));
    addHomeInterfaceImport("java.rmi.RemoteException");
    classType.bodyDeclarations().add(methodConstructor);

    writeImports(ast, unit, getHomeInterfaceImports());
    commitChanges(iUnit, unit, document);
}