Example usage for org.eclipse.jdt.core.dom PrefixExpression getParent

List of usage examples for org.eclipse.jdt.core.dom PrefixExpression getParent

Introduction

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

Prototype

public final ASTNode getParent() 

Source Link

Document

Returns this node's parent node, or null if this is the root node.

Usage

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  ava  2  s .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:org.autorefactor.refactoring.ASTSemanticMatcher.java

License:Open Source License

@Override
public boolean match(final PrefixExpression node, final Object other) {
    if (node.getParent() instanceof Statement) {
        if (other instanceof Assignment) {
            return match0(node, (Assignment) other);
        } else if (other instanceof PostfixExpression) {
            return match0(node, (PostfixExpression) other);
        }/*w w w. j av  a 2s.co  m*/
    }

    return super.match(node, other);
}

From source file:org.eclipselabs.javainterpreter.ExpressionVisitor.java

License:Open Source License

@Override
public boolean visit(NumberLiteral node) {
    String token = node.getToken();
    ASTNode parent = node.getParent();//www .  j a v a2 s  .c om

    if (node.getParent() instanceof PrefixExpression) {
        PrefixExpression exp = (PrefixExpression) node.getParent();
        if (exp.getOperator() == PrefixExpression.Operator.MINUS) {
            token = "-" + token;
            parent = exp.getParent();
        }
    }

    Number number = null;
    if (token.indexOf('.') == -1)
        number = new Integer(token);
    else
        number = new Double(token);

    if (argsTable.containsKey(parent))
        argsTable.get(parent).add(number);

    return true;
}