Example usage for org.eclipse.jdt.core.dom MemberValuePair toString

List of usage examples for org.eclipse.jdt.core.dom MemberValuePair toString

Introduction

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

Prototype

@Override
public final String toString() 

Source Link

Document

Returns a string representation of this node suitable for debugging purposes only.

Usage

From source file:es.bsc.servicess.ide.editors.ImplementationFormPage.java

License:Apache License

private void modifyConstraintInCompilationUnit(ICompilationUnit cu, String methodName, String constraintName,
        String constraintValue, String localtypeName)
        throws JavaModelException, MalformedTreeException, BadLocationException {
    if (cu != null) {
        Document document = new Document(cu.getSource());
        // IDocument document = textFileBuffer.getDocument();
        log.debug("Loading document for modify constraint " + constraintName + " in method " + methodName);
        ASTParser parser = ASTParser.newParser(AST.JLS3); // handles JDK
        // 1.0, 1.1,
        // 1.2, 1.3,
        // 1.4, 1.5, 1.6
        parser.setSource(cu);//  w  w w. j  a va  2 s  .  c  o m
        // In order to parse 1.5 code, some compiler options need to be set
        // to 1.5
        // Map options = JavaCore.getOptions();
        // JavaCore.setComplianceOptions(JavaCore.VERSION_1_6, options);
        // parser.setCompilerOptions(options);
        CompilationUnit result = (CompilationUnit) parser.createAST(null);
        if (result != null) {
            result.recordModifications();
            AST ast = result.getAST();
            java.util.List<AbstractTypeDeclaration> types = result.types();
            log.debug("pack: " + result.getPackage().toString() + " types: " + result.types().size());
            if (result.types().size() > 0) {
                boolean find = false;
                for (AbstractTypeDeclaration type : types) {
                    log.debug("Type: " + type.getName().getIdentifier() + "("
                            + type.getName().getFullyQualifiedName() + ")");
                    if (type.getName().getIdentifier().equals(localtypeName)) {
                        MethodDeclaration[] methods = ((TypeDeclaration) type).getMethods();
                        for (MethodDeclaration m : methods) {
                            log.debug("method FQDN: " + m.getName().getFullyQualifiedName() + " identifier: "
                                    + m.getName().getIdentifier());
                            if (m.getName().getIdentifier().equals(methodName)) {
                                java.util.List<IExtendedModifier> mods = m.modifiers();
                                for (IExtendedModifier mod : mods) {
                                    log.debug("modifier: " + mod.getClass().toString());
                                    if (mod.isAnnotation()) {
                                        if (((Annotation) mod).isNormalAnnotation()) {
                                            log.debug("annotation: "
                                                    + ((NormalAnnotation) mod).getTypeName().toString());
                                            if (((NormalAnnotation) mod).getTypeName().toString()
                                                    .equals("Constraints")) {
                                                java.util.List<MemberValuePair> vals = ((NormalAnnotation) mod)
                                                        .values();
                                                MemberValuePair value = null;
                                                for (MemberValuePair v : vals) {
                                                    log.debug("member: " + v.getName().getIdentifier());
                                                    if (v.getName().getIdentifier().equals(constraintName)) {
                                                        value = v;
                                                        break;
                                                    }
                                                }
                                                Expression qn = ConstraintsUtils.convertValueToExpression(
                                                        constraintName, constraintValue, ast);
                                                if (value == null) {
                                                    value = ast.newMemberValuePair();
                                                    value.setName(ast.newSimpleName(constraintName));
                                                    value.setValue(qn);
                                                    log.debug("Adding property to annotation: "
                                                            + value.toString());
                                                    vals.add(value);
                                                } else {
                                                    value.setValue(qn);
                                                    log.debug("Changing direction: " + value.toString());
                                                }
                                                find = true;
                                                break;
                                            }
                                        }
                                    }
                                }
                                if (find)
                                    break;
                                else {
                                    ImportDeclaration imp = ast.newImportDeclaration();
                                    imp.setName(ast.newName("integratedtoolkit.types.annotations.Constraints"));
                                    if (!result.imports().contains(imp))
                                        result.imports().add(imp);
                                    NormalAnnotation annotation = ast.newNormalAnnotation();
                                    annotation.setTypeName(ast.newSimpleName("Constraints"));
                                    java.util.List<MemberValuePair> vals = annotation.values();
                                    MemberValuePair value = ast.newMemberValuePair();
                                    value.setName(ast.newSimpleName(constraintName));
                                    value.setValue(ConstraintsUtils.convertValueToExpression(constraintName,
                                            constraintValue, ast));
                                    log.debug("Adding property to annotation: " + value.toString());
                                    vals.add(value);
                                    m.modifiers().add(0, annotation);
                                    find = true;
                                }
                            }
                        }
                        if (find)
                            break;
                    }

                }
                if (find) {

                    TextEdit edits = result.rewrite(document, cu.getJavaProject().getOptions(true));
                    edits.apply(document);
                    String newSource = document.get();
                    cu.getBuffer().setContents(newSource);
                    cu.save(null, true);
                    log.debug("writting modifications " + newSource);

                } else {
                    log.warn("Varaible and annotation not found");
                }

            } else {
                log.warn("No types found in the Compilation unit from AST");
            }

        } else {
            log.error("Error parsing Compilation unit with AST");
        }
    } else {
        log.error("Error getting Interface Compilation Unit");
    }
}

From source file:es.bsc.servicess.ide.editors.ImplementationFormPage.java

License:Apache License

/**
 * Modify parameter element/*from   w ww. ja  v  a  2s  .  co m*/
 * @param serviceClass Orchestration class name
 * @param methodName Element method name
 * @param p Parameter
 * @throws PartInitException
 * @throws JavaModelException
 * @throws MalformedTreeException
 * @throws BadLocationException
 * @throws SAXException
 * @throws IOException
 * @throws ParserConfigurationException
 */
private void modifyDirection(String serviceClass, String methodName, CoreElementParameter p)
        throws PartInitException, JavaModelException, MalformedTreeException, BadLocationException,
        SAXException, IOException, ParserConfigurationException {
    log.debug("Modifying direction for core element" + serviceClass + "." + methodName + " parameter "
            + p.getName() + "(" + p.getDirection() + ")");
    ICompilationUnit cu = getCEInterface(serviceClass, ((ServiceFormEditor) getEditor()).getProject(),
            ((ServiceFormEditor) getEditor()).getProjectMetadata());
    if (cu != null) {
        Document document = new Document(cu.getSource());
        log.debug(document.get());
        String localtypeName = serviceClass.subSequence(serviceClass.lastIndexOf(".") + 1,
                serviceClass.length()) + "Itf";
        ASTParser parser = ASTParser.newParser(AST.JLS3); // handles JDK
        // 1.0, 1.1,
        // 1.2, 1.3,
        // 1.4, 1.5, 1.6
        parser.setSource(cu);
        CompilationUnit result = (CompilationUnit) parser.createAST(null);
        if (result != null) {
            result.recordModifications();
            log.debug(result.toString());
            AST ast = result.getAST();
            java.util.List<AbstractTypeDeclaration> types = result.types();
            log.debug("pack: " + result.getPackage().toString() + " types: " + result.types().size());
            if (result.types().size() > 0) {
                boolean find = false;
                for (AbstractTypeDeclaration type : types) {
                    log.debug("Type: " + type.getName().getIdentifier() + "("
                            + type.getName().getFullyQualifiedName() + ")");
                    if (type.getName().getIdentifier().equals(localtypeName)) {
                        MethodDeclaration[] methods = ((TypeDeclaration) type).getMethods();
                        for (MethodDeclaration m : methods) {
                            log.debug("method FQDN: " + m.getName().getFullyQualifiedName() + " identifier: "
                                    + m.getName().getIdentifier());
                            if (m.getName().getIdentifier().equals(methodName)) {
                                java.util.List<SingleVariableDeclaration> pars = m.parameters();
                                for (SingleVariableDeclaration var : pars) {
                                    log.debug("var FQDN: " + var.getName().getFullyQualifiedName()
                                            + " identifier: " + var.getName().getIdentifier());
                                    if (var.getName().getIdentifier().equals(p.getName())) {
                                        java.util.List<IExtendedModifier> mods = var.modifiers();
                                        for (IExtendedModifier mod : mods) {
                                            log.debug("modifier: " + mod.getClass().toString());
                                            if (mod.isAnnotation()) {
                                                if (((Annotation) mod).isNormalAnnotation()) {
                                                    log.debug("annotation: " + ((NormalAnnotation) mod)
                                                            .getTypeName().toString());
                                                    if (((NormalAnnotation) mod).getTypeName().toString()
                                                            .equals("Parameter")) {
                                                        java.util.List<MemberValuePair> vals = ((NormalAnnotation) mod)
                                                                .values();
                                                        MemberValuePair dir_value = null;
                                                        for (MemberValuePair v : vals) {
                                                            log.debug("member: " + v.getName().getIdentifier());
                                                            if (v.getName().getIdentifier()
                                                                    .equals("direction")) {
                                                                dir_value = v;
                                                                break;
                                                            }
                                                        }

                                                        if (dir_value == null) {
                                                            dir_value = ast.newMemberValuePair();
                                                            dir_value.setName(ast.newSimpleName("direction"));
                                                            QualifiedName qn = ast.newQualifiedName(
                                                                    ast.newSimpleName("Direction"),
                                                                    ast.newSimpleName(p.getDirection()));
                                                            dir_value.setValue(qn);
                                                            log.debug("Adding property to annotation: "
                                                                    + dir_value.toString());
                                                            vals.add(dir_value);
                                                        } else {
                                                            QualifiedName ex = (QualifiedName) dir_value
                                                                    .getValue();
                                                            log.debug("ValueClass: " + ex.getClass());
                                                            ex.setName(ast.newSimpleName(p.getDirection()));
                                                            log.debug("Changing direction: "
                                                                    + dir_value.toString());
                                                        }
                                                        find = true;
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                        if (find)
                                            break;
                                    }
                                }
                                if (find)
                                    break;
                            }
                        }
                        if (find)
                            break;
                    }
                }
                if (find) {
                    TextEdit edits = result.rewrite(document, cu.getJavaProject().getOptions(true));
                    edits.apply(document);
                    String newSource = document.get();
                    cu.getBuffer().setContents(newSource);
                    cu.save(null, true);
                    log.debug("writting modifications " + newSource);
                } else {
                    log.warn("Varaible and annotation not found");
                }

            } else {
                log.warn("No types found in the Compilation unit from AST");
            }
        } else {
            log.error("Error parsing Compilation unit with AST");
        }
    } else {
        log.error("Error getting Interface Compilation Unit");
    }
}