Example usage for org.eclipse.jdt.core.dom Name accept

List of usage examples for org.eclipse.jdt.core.dom Name accept

Introduction

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

Prototype

public final void accept(ASTVisitor visitor) 

Source Link

Document

Accepts the given visitor on a visit of the current node.

Usage

From source file:coloredide.utils.CopiedNaiveASTFlattener.java

License:Open Source License

public boolean visit(MethodDeclaration node) {
    if (node.getJavadoc() != null) {
        node.getJavadoc().accept(this);
    }/*from  www . ja va 2s .  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.windowtester.eclipse.ui.convert.WTConvertAPIContext.java

License:Open Source License

/**
 * Construct a new simple or qualified name
 * // w w  w . ja  v a 2 s. co m
 * @param text the simple or qualified name as text (not <code>null</code>, not empty)
 * @param startPosition the starting position relative to the new AST nodes being
 *            created. For example, when instantiating a new name node to replace an
 *            existing name node, pass zero. When instantiation a new name node to be
 *            used as part of a new import declaration, pass the offset of the name
 *            node within the new import declaration.
 * @return a new name (not <code>null</code>)
 */
public Name newName(String text, final int startPosition) {
    Name newName = compUnit.getAST().newName(text);
    newName.accept(new ASTVisitor() {
        int index = startPosition;

        public boolean visit(QualifiedName node) {
            node.setSourceRange(startPosition, node.toString().length());
            return true;
        }

        public boolean visit(SimpleName node) {
            int length = node.getIdentifier().length();
            node.setSourceRange(index, length);
            index += length + 1;
            return true;
        }
    });
    return newName;
}

From source file:de.ovgu.cide.export.physical.ahead.FSTComposerJavaPrettyPrinter.java

License:Open Source License

public boolean visit(MethodDeclaration node) {
    currentMethod.push(node);/*from   w w w .java2 s.c o m*/
    if (!node.isConstructor() || currentRefinement.size() == 0
            || !currentRefinement.peek().refinements().contains(node))
        return super.visit(node);

    // only special treatment for constructor refinements!
    if (node.getJavadoc() != null) {
        node.getJavadoc().accept(this);
    }
    printIndent();
    // buffer.append("refines ");
    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:de.ovgu.cide.export.physical.ahead.JakPrettyPrinter.java

License:Open Source License

public boolean visit(MethodDeclaration node) {
    currentMethod.push(node);/*from w  ww.jav a  2  s  .co m*/
    if (!node.isConstructor() || currentRefinement.size() == 0
            || !currentRefinement.peek().refinements().contains(node))
        return super.visit(node);

    // only special treatment for constructor refinements!
    if (node.getJavadoc() != null) {
        node.getJavadoc().accept(this);
    }
    printIndent();
    buffer.append("refines ");
    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:net.sf.j2s.core.astvisitors.ASTKeywordVisitor.java

License:Open Source License

public boolean visit(Assignment node) {
    Expression left = node.getLeftHandSide();
    Expression right = node.getRightHandSide();
    IVariableBinding varBinding = null;//www. j a v  a 2  s  .c  om
    if (left instanceof Name) {
        Name leftName = (Name) left;
        IBinding nameBinding = leftName.resolveBinding();
        if (nameBinding instanceof IVariableBinding) {
            varBinding = (IVariableBinding) nameBinding;
        }
    } else if (left instanceof FieldAccess) {
        FieldAccess leftAccess = (FieldAccess) left;
        varBinding = leftAccess.resolveFieldBinding();
    }
    String op = node.getOperator().toString();
    ITypeBinding declaring = null;
    String qName = null;
    if (varBinding != null && (varBinding.getModifiers() & Modifier.STATIC) != 0
            && (declaring = varBinding.getDeclaringClass()) != null
            && !(qName = declaring.getQualifiedName()).startsWith("org.eclipse.swt.internal.xhtml.")
            && !qName.startsWith("net.sf.j2s.html.")) {
        boolean directStaticAccess = left instanceof SimpleName
                || (left instanceof QualifiedName
                        && ((QualifiedName) left).getQualifier() instanceof SimpleName)
                || (left instanceof FieldAccess
                        && ((FieldAccess) left).getExpression() instanceof ThisExpression);
        ASTNode parent = node.getParent();
        boolean needParenthesis = (supportsObjectStaticFields || !directStaticAccess)
                && !(parent instanceof Statement);
        if (needParenthesis) {
            buffer.append("(");
        }
        if (left instanceof QualifiedName) {
            QualifiedName leftName = (QualifiedName) left;
            if (!(leftName.getQualifier() instanceof SimpleName)) {
                leftName.getQualifier().accept(this);
                buffer.append(", ");
            }
        } else if (left instanceof FieldAccess) {
            FieldAccess leftAccess = (FieldAccess) left;
            if (!(leftAccess.getExpression() instanceof ThisExpression)) {
                leftAccess.getExpression().accept(this);
                buffer.append(", ");
            }
        }
        if (supportsObjectStaticFields) {
            buffer.append(assureQualifiedName(
                    shortenQualifiedName(varBinding.getDeclaringClass().getQualifiedName())));
            buffer.append(".prototype.");
            if (left instanceof QualifiedName) {
                QualifiedName leftName = (QualifiedName) left;
                leftName.getName().accept(this);
            } else if (left instanceof FieldAccess) {
                FieldAccess leftAccess = (FieldAccess) left;
                leftAccess.getName().accept(this);
            } else {
                Name leftName = (Name) left;
                leftName.accept(this);
            }
            buffer.append(" = ");
        }

        buffer.append(
                assureQualifiedName(shortenQualifiedName(varBinding.getDeclaringClass().getQualifiedName())));
        buffer.append('.');
        if (left instanceof QualifiedName) {
            QualifiedName leftName = (QualifiedName) left;
            leftName.getName().accept(this);
        } else if (left instanceof FieldAccess) {
            FieldAccess leftAccess = (FieldAccess) left;
            leftAccess.getName().accept(this);
        } else {
            Name leftName = (Name) left;
            leftName.accept(this);
        }
        buffer.append(' ');
        boolean isMixedOp = op.trim().length() > 1;
        ITypeBinding leftTypeBinding = left.resolveTypeBinding();
        if (leftTypeBinding != null && "char".equals(leftTypeBinding.getName())) {
            ITypeBinding rightTypeBinding = right.resolveTypeBinding();
            if (!isMixedOp) { // =
                buffer.append(op);
                buffer.append(' ');
                if (rightTypeBinding != null && "char".equals(rightTypeBinding.getName())) {
                    boxingNode(right);
                } else {
                    buffer.append("String.fromCharCode (");
                    boxingNode(right);
                    buffer.append(')');
                }
            } else {
                buffer.append("= String.fromCharCode (");
                buffer.append(assureQualifiedName(
                        shortenQualifiedName(varBinding.getDeclaringClass().getQualifiedName())));
                buffer.append('.');
                if (left instanceof QualifiedName) {
                    QualifiedName leftName = (QualifiedName) left;
                    leftName.getName().accept(this);
                } else if (left instanceof FieldAccess) {
                    FieldAccess leftAccess = (FieldAccess) left;
                    leftAccess.getName().accept(this);
                } else {
                    Name leftName = (Name) left;
                    leftName.accept(this);
                }
                buffer.append(".charCodeAt (0) ");
                buffer.append(op.charAt(0));
                buffer.append(' ');
                if (rightTypeBinding != null && "char".equals(rightTypeBinding.getName())) {
                    Object constValue = right.resolveConstantExpressionValue();
                    if (constValue != null && constValue instanceof Character) {
                        buffer.append(((Character) constValue).charValue() + 0);
                    } else {
                        boxingNode(right);
                        buffer.append(".charCodeAt (0)");
                    }
                } else {
                    boxingNode(right);
                }
                buffer.append(")");
            }
        } else {
            buffer.append(op);
            buffer.append(' ');
            boxingNode(right);
        }

        if (needParenthesis) {
            buffer.append(")");
        }
        return false;
    }
    ITypeBinding typeBinding = left.resolveTypeBinding();
    if (typeBinding != null && typeBinding.isPrimitive()) {
        if ("boolean".equals(typeBinding.getName())) {
            if (op.startsWith("^") || op.startsWith("|") || op.startsWith("&")
            /*|| op.startsWith("!")*/) {
                left.accept(this);
                buffer.append(" = new Boolean (");
                left.accept(this);
                buffer.append(' ');
                buffer.append(op.charAt(0));
                if (right instanceof InfixExpression) {
                    buffer.append(" (");
                    right.accept(this);
                    buffer.append("))");
                } else {
                    buffer.append(' ');
                    right.accept(this);
                    buffer.append(')');
                }
                buffer.append(".valueOf ()");
                return false;
            }
        } else if (typeBinding != null && "char".equals(typeBinding.getName())) {
            boolean isMixedOp = op.trim().length() > 1;
            if (!isMixedOp) {
                if (right instanceof Name || right instanceof CharacterLiteral || right instanceof ArrayAccess
                        || right instanceof FieldAccess || right instanceof MethodInvocation
                        || right instanceof ParenthesizedExpression || right instanceof SuperFieldAccess
                        || right instanceof SuperMethodInvocation || right instanceof ThisExpression
                        || right instanceof CastExpression) {
                    left.accept(this);
                    buffer.append(" = ");
                    right.accept(this);
                    return false;
                }
            }
            ITypeBinding rightTypeBinding = right.resolveTypeBinding();
            /*
             * FIXME: Bug here!: 
             * v[count++] += 'a';
             * v[count++] = String.fromCharCode ((v[count++]).charCodeAt (0) + 97);
             */
            left.accept(this);
            if (rightTypeBinding != null && "char".equals(rightTypeBinding.getName()) && !isMixedOp) {
                buffer.append(' ');
                buffer.append(op);
                buffer.append(' ');
                right.accept(this);
            } else {
                buffer.append(" = String.fromCharCode (");
                if (isMixedOp) {
                    if (left instanceof SimpleName || left instanceof QualifiedName) {
                        left.accept(this);
                    } else {
                        buffer.append("(");
                        left.accept(this);
                        buffer.append(")");
                    }
                    buffer.append(".charCodeAt (0) ");
                    buffer.append(op.charAt(0));
                }
                buffer.append(' ');
                if (right instanceof InfixExpression) {
                    String constValue = checkConstantValue(right);
                    if (constValue != null) {
                        buffer.append(constValue);
                    } else {
                        buffer.append("(");
                        right.accept(this);
                        buffer.append(")");
                    }
                    if ("char".equals(rightTypeBinding.getName())) {
                        buffer.append(".charCodeAt (0)");
                    }
                } else {
                    if ("char".equals(rightTypeBinding.getName())) {
                        Object constValue = right.resolveConstantExpressionValue();
                        if (constValue != null && constValue instanceof Character) {
                            buffer.append(((Character) constValue).charValue() + 0);
                        } else {
                            boolean needParenthesis = !(right instanceof ParenthesizedExpression
                                    || right instanceof PrefixExpression || right instanceof PostfixExpression);
                            if (needParenthesis) {
                                buffer.append("(");
                            }
                            right.accept(this);
                            if (needParenthesis) {
                                buffer.append(")");
                            }
                            buffer.append(".charCodeAt (0)");
                        }
                    } else {
                        right.accept(this);
                    }
                }
                buffer.append(')');
            }
            return false;
        }
    }
    left.accept(this);
    buffer.append(' ');
    buffer.append(op);
    buffer.append(' ');
    ITypeBinding binding = right.resolveTypeBinding();
    if (binding != null && "char".equals(binding.getName())) {
        String typeBindingName = (typeBinding != null) ? typeBinding.getName() : null;
        if (right instanceof CharacterLiteral) {
            CharacterLiteral cl = (CharacterLiteral) right;
            if ("char".equals(typeBindingName) || typeBindingName.indexOf("String") != -1) {
                String constValue = checkConstantValue(right);
                buffer.append(constValue);
            } else {
                buffer.append(0 + cl.charValue());
            }
        } else {
            if (typeBindingName != null
                    && ("char".equals(typeBindingName) || typeBindingName.indexOf("String") != -1)) {
                right.accept(this);
            } else {
                int idx1 = buffer.length();
                buffer.append('(');
                right.accept(this);
                buffer.append(")");

                boolean appendingCode = true;
                int length = buffer.length();
                if (right instanceof MethodInvocation) {
                    MethodInvocation m = (MethodInvocation) right;
                    if ("charAt".equals(m.getName().toString())) {
                        int idx2 = buffer.indexOf(".charAt ", idx1);
                        if (idx2 != -1) {
                            StringBuffer newMethodBuffer = new StringBuffer();
                            newMethodBuffer.append(buffer.substring(idx1 + 1, idx2));
                            newMethodBuffer.append(".charCodeAt ");
                            newMethodBuffer.append(buffer.substring(idx2 + 8, length - 1));
                            buffer.delete(idx1, length);
                            buffer.append(newMethodBuffer.toString());
                            appendingCode = false;
                        }
                    }
                }
                if (appendingCode) {
                    buffer.append(".charCodeAt (0)");
                }
            }
        }
    } else {
        boxingNode(right);
    }
    return false;
}

From source file:net.sf.j2s.core.astvisitors.ASTKeywordVisitor.java

License:Open Source License

public boolean visit(PostfixExpression node) {
    Expression left = node.getOperand();
    IVariableBinding varBinding = null;//  w  w  w .  j  av a  2s.  c  om
    if (left instanceof Name) {
        Name leftName = (Name) left;
        IBinding nameBinding = leftName.resolveBinding();
        if (nameBinding instanceof IVariableBinding) {
            varBinding = (IVariableBinding) nameBinding;
        }
    } else if (left instanceof FieldAccess) {
        FieldAccess leftAccess = (FieldAccess) left;
        varBinding = leftAccess.resolveFieldBinding();
    }
    ITypeBinding typeBinding = left.resolveTypeBinding();
    ITypeBinding declaring = null;
    String qName = null;
    if (varBinding != null && (varBinding.getModifiers() & Modifier.STATIC) != 0
            && (declaring = varBinding.getDeclaringClass()) != null
            && !(qName = declaring.getQualifiedName()).startsWith("org.eclipse.swt.internal.xhtml.")
            && !qName.startsWith("net.sf.j2s.html.")) {
        boolean directStaticAccess = left instanceof SimpleName
                || (left instanceof QualifiedName
                        && ((QualifiedName) left).getQualifier() instanceof SimpleName)
                || (left instanceof FieldAccess
                        && ((FieldAccess) left).getExpression() instanceof ThisExpression);
        ASTNode parent = node.getParent();
        boolean staticCharType = typeBinding.isPrimitive() && "char".equals(typeBinding.getName());
        boolean needParenthesis = (supportsObjectStaticFields || !directStaticAccess
                || (typeBinding != null && staticCharType))
                && !(parent instanceof Statement || parent instanceof ParenthesizedExpression);
        if (needParenthesis) {
            buffer.append("(");
        }
        if (left instanceof QualifiedName) {
            QualifiedName leftName = (QualifiedName) left;
            if (!(leftName.getQualifier() instanceof SimpleName)) {
                leftName.getQualifier().accept(this);
                buffer.append(", ");
            }
        } else if (left instanceof FieldAccess) {
            FieldAccess leftAccess = (FieldAccess) left;
            if (!(leftAccess.getExpression() instanceof ThisExpression)) {
                leftAccess.getExpression().accept(this);
                buffer.append(", ");
            }
        }
        if ((supportsObjectStaticFields || staticCharType) && !(parent instanceof Statement)) {
            buffer.append("$t$ = ");
        }
        String op = node.getOperator().toString();
        if (staticCharType) {
            if (!(parent instanceof Statement)) {
                buffer.append(assureQualifiedName(
                        shortenQualifiedName(varBinding.getDeclaringClass().getQualifiedName())));
                buffer.append('.');
                if (left instanceof QualifiedName) {
                    QualifiedName leftName = (QualifiedName) left;
                    leftName.getName().accept(this);
                } else if (left instanceof FieldAccess) {
                    FieldAccess leftAccess = (FieldAccess) left;
                    leftAccess.getName().accept(this);
                } else {
                    Name leftName = (Name) left;
                    leftName.accept(this);
                }
                buffer.append(", ");
            }
            buffer.append(assureQualifiedName(
                    shortenQualifiedName(varBinding.getDeclaringClass().getQualifiedName())));
            buffer.append('.');
            if (left instanceof QualifiedName) {
                QualifiedName leftName = (QualifiedName) left;
                leftName.getName().accept(this);
            } else if (left instanceof FieldAccess) {
                FieldAccess leftAccess = (FieldAccess) left;
                leftAccess.getName().accept(this);
            } else {
                Name leftName = (Name) left;
                leftName.accept(this);
            }
            buffer.append(" = String.fromCharCode (");
            buffer.append(assureQualifiedName(
                    shortenQualifiedName(varBinding.getDeclaringClass().getQualifiedName())));
            buffer.append('.');
            if (left instanceof QualifiedName) {
                QualifiedName leftName = (QualifiedName) left;
                leftName.getName().accept(this);
            } else if (left instanceof FieldAccess) {
                FieldAccess leftAccess = (FieldAccess) left;
                leftAccess.getName().accept(this);
            } else {
                Name leftName = (Name) left;
                leftName.accept(this);
            }
            if ("++".equals(op)) {
                buffer.append(".charCodeAt (0) + 1)");
            } else {
                buffer.append(".charCodeAt (0) - 1)");
            }
        } else {
            buffer.append(assureQualifiedName(
                    shortenQualifiedName(varBinding.getDeclaringClass().getQualifiedName())));
            buffer.append('.');
            if (left instanceof QualifiedName) {
                QualifiedName leftName = (QualifiedName) left;
                leftName.getName().accept(this);
            } else if (left instanceof FieldAccess) {
                FieldAccess leftAccess = (FieldAccess) left;
                leftAccess.getName().accept(this);
            } else {
                Name leftName = (Name) left;
                leftName.accept(this);
            }
            //buffer.append(' ');
            buffer.append(op);
        }

        if (supportsObjectStaticFields) {
            buffer.append(", ");
            buffer.append(assureQualifiedName(
                    shortenQualifiedName(varBinding.getDeclaringClass().getQualifiedName())));
            buffer.append(".prototype.");
            if (left instanceof QualifiedName) {
                QualifiedName leftName = (QualifiedName) left;
                leftName.getName().accept(this);
            } else if (left instanceof FieldAccess) {
                FieldAccess leftAccess = (FieldAccess) left;
                leftAccess.getName().accept(this);
            } else {
                Name leftName = (Name) left;
                leftName.accept(this);
            }
            buffer.append(" = ");
            buffer.append(assureQualifiedName(
                    shortenQualifiedName(varBinding.getDeclaringClass().getQualifiedName())));
            buffer.append('.');
            if (left instanceof QualifiedName) {
                QualifiedName leftName = (QualifiedName) left;
                leftName.getName().accept(this);
            } else if (left instanceof FieldAccess) {
                FieldAccess leftAccess = (FieldAccess) left;
                leftAccess.getName().accept(this);
            } else {
                Name leftName = (Name) left;
                leftName.accept(this);
            }
        }
        if ((supportsObjectStaticFields || staticCharType) && !(parent instanceof Statement)) {
            buffer.append(", $t$");
        }
        if (needParenthesis) {
            buffer.append(")");
        }
        return false;
    }
    if (typeBinding != null && typeBinding.isPrimitive()) {
        if ("char".equals(typeBinding.getName())) {
            ASTNode parent = node.getParent();
            if (!(parent instanceof Statement)) {
                if (!(parent instanceof ParenthesizedExpression)) {
                    buffer.append("(");
                }
                buffer.append("$c$ = ");
                left.accept(this);
                buffer.append(", ");
            }
            left.accept(this);
            buffer.append(" = String.fromCharCode (");
            left.accept(this);
            String op = node.getOperator().toString();
            if ("++".equals(op)) {
                buffer.append(".charCodeAt (0) + 1)");
            } else {
                buffer.append(".charCodeAt (0) - 1)");
            }
            if (!(parent instanceof Statement)) {
                buffer.append(", $c$");
                if (!(parent instanceof ParenthesizedExpression)) {
                    buffer.append(")");
                }
            }
            return false;
        }
    }
    boxingNode(left);
    return false;
    //return super.visit(node);
}

From source file:net.sf.j2s.core.astvisitors.ASTKeywordVisitor.java

License:Open Source License

public boolean visit(PrefixExpression node) {
    String constValue = checkConstantValue(node);
    if (constValue != null) {
        buffer.append(constValue);/*ww  w .  j  ava2s.  c o m*/
        return false;
    }
    String op = node.getOperator().toString();
    if ("~".equals(op) || "!".equals(op)) {
        buffer.append(op);
        return super.visit(node);
    }
    Expression left = node.getOperand();
    IVariableBinding varBinding = null;
    if (left instanceof Name) {
        Name leftName = (Name) left;
        IBinding nameBinding = leftName.resolveBinding();
        if (nameBinding instanceof IVariableBinding) {
            varBinding = (IVariableBinding) nameBinding;
        }
    } else if (left instanceof FieldAccess) {
        FieldAccess leftAccess = (FieldAccess) left;
        varBinding = leftAccess.resolveFieldBinding();
    }
    ITypeBinding typeBinding = left.resolveTypeBinding();
    ITypeBinding declaring = null;
    String qName = null;
    if (varBinding != null && (varBinding.getModifiers() & Modifier.STATIC) != 0
            && (declaring = varBinding.getDeclaringClass()) != null
            && !(qName = declaring.getQualifiedName()).startsWith("org.eclipse.swt.internal.xhtml.")
            && !qName.startsWith("net.sf.j2s.html.")) {
        boolean directStaticAccess = left instanceof SimpleName
                || (left instanceof QualifiedName
                        && ((QualifiedName) left).getQualifier() instanceof SimpleName)
                || (left instanceof FieldAccess
                        && ((FieldAccess) left).getExpression() instanceof ThisExpression);
        ASTNode parent = node.getParent();
        boolean needParenthesis = (supportsObjectStaticFields || !directStaticAccess
                || (typeBinding != null && typeBinding.isPrimitive() && "char".equals(typeBinding.getName())))
                && !(parent instanceof Statement || parent instanceof ParenthesizedExpression);
        if (needParenthesis) {
            buffer.append("(");
        }
        if (left instanceof QualifiedName) {
            QualifiedName leftName = (QualifiedName) left;
            if (!(leftName.getQualifier() instanceof SimpleName)) {
                leftName.getQualifier().accept(this);
                buffer.append(", ");
            }
        } else if (left instanceof FieldAccess) {
            FieldAccess leftAccess = (FieldAccess) left;
            if (!(leftAccess
                    .getExpression() instanceof ThisExpression/* 
                                                              || leftAccess.getExpression() instanceof SimpleName*/)) {
                leftAccess.getExpression().accept(this);
                buffer.append(", ");
            }
        }
        if (supportsObjectStaticFields) {
            buffer.append(assureQualifiedName(
                    shortenQualifiedName(varBinding.getDeclaringClass().getQualifiedName())));
            buffer.append(".prototype.");
            if (left instanceof QualifiedName) {
                QualifiedName leftName = (QualifiedName) left;
                leftName.getName().accept(this);
            } else if (left instanceof FieldAccess) {
                FieldAccess leftAccess = (FieldAccess) left;
                leftAccess.getName().accept(this);
            } else {
                Name leftName = (Name) left;
                leftName.accept(this);
            }
            buffer.append(" = ");
        }
        if (typeBinding.isPrimitive() && "char".equals(typeBinding.getName())) {
            buffer.append(assureQualifiedName(
                    shortenQualifiedName(varBinding.getDeclaringClass().getQualifiedName())));
            buffer.append('.');
            if (left instanceof QualifiedName) {
                QualifiedName leftName = (QualifiedName) left;
                leftName.getName().accept(this);
            } else if (left instanceof FieldAccess) {
                FieldAccess leftAccess = (FieldAccess) left;
                leftAccess.getName().accept(this);
            } else {
                Name leftName = (Name) left;
                leftName.accept(this);
            }
            buffer.append(" = String.fromCharCode (");
            buffer.append(assureQualifiedName(
                    shortenQualifiedName(varBinding.getDeclaringClass().getQualifiedName())));
            buffer.append('.');
            if (left instanceof QualifiedName) {
                QualifiedName leftName = (QualifiedName) left;
                leftName.getName().accept(this);
            } else if (left instanceof FieldAccess) {
                FieldAccess leftAccess = (FieldAccess) left;
                leftAccess.getName().accept(this);
            } else {
                Name leftName = (Name) left;
                leftName.accept(this);
            }
            if ("++".equals(op)) {
                buffer.append(".charCodeAt (0) + 1)");
            } else {
                buffer.append(".charCodeAt (0) - 1)");
            }
        } else {
            buffer.append(op);
            //buffer.append(' ');
            buffer.append(assureQualifiedName(
                    shortenQualifiedName(varBinding.getDeclaringClass().getQualifiedName())));
            buffer.append('.');
            if (left instanceof QualifiedName) {
                QualifiedName leftName = (QualifiedName) left;
                leftName.getName().accept(this);
            } else if (left instanceof FieldAccess) {
                FieldAccess leftAccess = (FieldAccess) left;
                leftAccess.getName().accept(this);
            } else {
                Name leftName = (Name) left;
                leftName.accept(this);
            }
        }
        if (needParenthesis) {
            buffer.append(")");
        }
        return false;
    }
    if (typeBinding.isPrimitive()) {
        if ("char".equals(typeBinding.getName())) {
            ASTNode parent = node.getParent();
            if (!(parent instanceof Statement || parent instanceof ParenthesizedExpression)) {
                buffer.append("(");
            }
            left.accept(this);
            buffer.append(" = String.fromCharCode (");
            left.accept(this);
            if ("++".equals(op)) {
                buffer.append(".charCodeAt (0) + 1)");
            } else {
                buffer.append(".charCodeAt (0) - 1)");
            }
            if (!(parent instanceof Statement || parent instanceof ParenthesizedExpression)) {
                buffer.append(")");
            }
            return false;
        }
    }
    buffer.append(node.getOperator());
    boxingNode(left);
    return false;
}

From source file:net.sf.j2s.core.astvisitors.ASTKeywordVisitor.java

License:Open Source License

public boolean visit(QualifiedName node) {
    if (isSimpleQualified(node)) {
        String constValue = checkConstantValue(node);
        if (constValue != null) {
            buffer.append(constValue);//  w  w w. ja  va2 s . co  m
            return false;
        }
    }
    boolean staticFields = false;
    IVariableBinding varBinding = null;
    IBinding nameBinding = node.resolveBinding();
    if (nameBinding instanceof IVariableBinding) {
        varBinding = (IVariableBinding) nameBinding;
    }
    ITypeBinding declaring = null;
    String qdName = null;
    if (!supportsObjectStaticFields && varBinding != null && (varBinding.getModifiers() & Modifier.STATIC) != 0
            && (declaring = varBinding.getDeclaringClass()) != null
            && !(qdName = declaring.getQualifiedName()).startsWith("org.eclipse.swt.internal.xhtml.")
            && !qdName.startsWith("net.sf.j2s.html.")) {
        IBinding qBinding = node.getQualifier().resolveBinding();
        if (!(qBinding != null && qBinding instanceof ITypeBinding)) {
            staticFields = true;
        }
    }
    ASTNode parent = node.getParent();
    boolean qualifierVisited = false;
    if (parent != null && !(parent instanceof QualifiedName)) {
        Name qualifier = node.getQualifier();
        while (qualifier instanceof QualifiedName) {
            IBinding binding = qualifier.resolveBinding();
            if (binding != null && !(binding instanceof IVariableBinding)) {
                Name xqualifier = ((QualifiedName) qualifier).getQualifier();
                if (xqualifier instanceof QualifiedName) {
                    IBinding xbinding = qualifier.resolveBinding();
                    if (xbinding != null && !(xbinding instanceof IVariableBinding)) {
                        qualifier = xqualifier;
                        continue;
                    }
                }
            }
            break;
        }
        IBinding binding = qualifier.resolveBinding();
        if (binding != null) {
            if (!(binding instanceof IVariableBinding)) {
                ITypeBinding typeBinding = qualifier.resolveTypeBinding();
                if (typeBinding != null) {
                    // Compiling inner Class or enum type, like:
                    // RadiusData.EnumType e = RadiusData.EnumType.THREE;
                    // avoid generate duplicated RadiusData
                    String name = typeBinding.getQualifiedName();
                    //                  ITypeBinding declaringClass = typeBinding.getDeclaringClass();
                    //                  if (declaringClass != null) {
                    //                     name = declaringClass.getQualifiedName();
                    //                  } else {
                    //                     IPackageBinding pkg = typeBinding.getPackage();
                    //                     if (pkg != null) {
                    //                        name = pkg.getName();
                    //                     } else {
                    //                        name = "";
                    //                     }
                    //                  }
                    String xhtml = "net.sf.j2s.html.";
                    if (name.indexOf(xhtml) == 0) {
                        name = name.substring(xhtml.length());
                    }
                    if (name.indexOf("java.lang.") == 0) {
                        name = name.substring(10);
                    }
                    if (name.length() != 0) {
                        if (staticFields) {
                            if (qualifier instanceof SimpleName) {
                                buffer.append(assureQualifiedName(shortenQualifiedName(
                                        varBinding.getDeclaringClass().getQualifiedName())));
                            } else {
                                buffer.append('(');
                                buffer.append(name);
                                buffer.append(", ");
                                buffer.append(assureQualifiedName(shortenQualifiedName(
                                        varBinding.getDeclaringClass().getQualifiedName())));
                                buffer.append(')');
                            }
                        } else {
                            buffer.append(name);
                        }
                        buffer.append('.');
                        qualifierVisited = true;
                    }
                }
            }
        }
    }
    Name qName = node.getQualifier();
    String nodeStr = qName.toString();
    if (nodeStr.equals("net.sf.j2s.html") || nodeStr.equals("org.eclipse.swt.internal.xhtml")) {
        node.getName().accept(this);
        return false;
    }
    if (!qualifierVisited) {
        if (staticFields) {
            if (qName instanceof SimpleName) {
                buffer.append(assureQualifiedName(
                        shortenQualifiedName(varBinding.getDeclaringClass().getQualifiedName())));
            } else {
                buffer.append('(');
                qName.accept(this);
                buffer.append(", ");
                buffer.append(assureQualifiedName(
                        shortenQualifiedName(varBinding.getDeclaringClass().getQualifiedName())));
                buffer.append(')');
            }
        } else {
            qName.accept(this);
        }
        buffer.append('.');
    }
    node.getName().accept(this);
    return false;
}

From source file:net.sf.j2s.core.astvisitors.ASTScriptVisitor.java

License:Open Source License

public boolean visit(ThisExpression node) {
    Name qualifier = node.getQualifier();
    if (qualifier != null) {
        ASTNode xparent = node.getParent();
        while (xparent != null && !(xparent instanceof AbstractTypeDeclaration)
                && !(xparent instanceof AnonymousClassDeclaration)) {
            xparent = xparent.getParent();
        }/* ww  w.jav  a  2s.  c  om*/
        if (xparent == null || xparent.getParent() == null // CompilationUnit
                || xparent.getParent().getParent() == null) {
            buffer.append("this");
        } else {
            /*
             * only need callbacks wrapper in inner classes
             * or anonymous classes.
             */
            buffer.append("this.callbacks[\"");
            qualifier.accept(this);
            buffer.append("\"]");
        }
    } else {
        buffer.append("this");
    }
    return false;
}

From source file:org.codemucker.jmutate.ast.JAstFlattener.java

License:Open Source License

public boolean visit(MethodDeclaration node) {
    if (node.getJavadoc() != null) {
        node.getJavadoc().accept(this);
    }/*from   ww w.  j a va 2 s . c  o  m*/
    printIndent();
    if (node.getAST().apiLevel() == JLS2) {
        printModifiers(node.getModifiers());
    }
    if (node.getAST().apiLevel() >= 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.getAST().apiLevel() == JLS2) {
            getReturnType(node).accept(this);
        } else {
            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$
    if (node.getAST().apiLevel() >= AST.JLS8) {
        Type receiverType = node.getReceiverType();
        if (receiverType != null) {
            receiverType.accept(this);
            this.buffer.append(' ');
            SimpleName qualifier = node.getReceiverQualifier();
            if (qualifier != null) {
                qualifier.accept(this);
                this.buffer.append('.');
            }
            this.buffer.append("this"); //$NON-NLS-1$
            if (node.parameters().size() > 0) {
                this.buffer.append(',');
            }
        }
    }
    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$
    int size = node.getExtraDimensions();
    if (node.getAST().apiLevel() >= AST.JLS8) {
        List dimensions = node.extraDimensions();
        for (int i = 0; i < size; i++) {
            visit((Dimension) dimensions.get(i));
        }
    } else {
        for (int i = 0; i < size; i++) {
            this.buffer.append("[]"); //$NON-NLS-1$
        }
    }
    if (node.getAST().apiLevel() < AST.JLS8) {
        if (!thrownExceptions(node).isEmpty()) {
            this.buffer.append(" throws ");//$NON-NLS-1$
            for (Iterator it = thrownExceptions(node).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$
        }
    } else {
        if (!node.thrownExceptionTypes().isEmpty()) {
            this.buffer.append(" throws ");//$NON-NLS-1$
            for (Iterator it = node.thrownExceptionTypes().iterator(); it.hasNext();) {
                Type n = (Type) 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;
}