Example usage for org.eclipse.jdt.core.dom AST newInstanceofExpression

List of usage examples for org.eclipse.jdt.core.dom AST newInstanceofExpression

Introduction

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

Prototype

public InstanceofExpression newInstanceofExpression() 

Source Link

Document

Creates and returns a new unparented instanceof expression node owned by this AST.

Usage

From source file:com.google.devtools.j2objc.translate.ASTFactory.java

License:Apache License

public static InstanceofExpression newInstanceofExpression(AST ast, Expression lhs, ITypeBinding type) {
    InstanceofExpression expr = ast.newInstanceofExpression();
    expr.setLeftOperand(lhs);//from  w  w w .ja v  a2  s. c om
    expr.setRightOperand(newType(ast, type));
    Types.addBinding(expr, ast.resolveWellKnownType("boolean"));
    return expr;
}

From source file:org.jboss.forge.roaster.model.impl.expressions.InstanceofImpl.java

License:Open Source License

@Override
public org.eclipse.jdt.core.dom.InstanceofExpression materialize(AST ast) {
    if (isMaterialized()) {
        return isA;
    }//w w  w .  j  a  va  2 s.  co m
    isA = ast.newInstanceofExpression();
    isA.setRightOperand(JDTHelper.getType(typeName, ast));
    if (expression != null) {
        org.eclipse.jdt.core.dom.Expression expr = wireAndGetExpression(expression, this, ast);
        isA.setLeftOperand(expr);
    }
    return isA;
}

From source file:refactorer.Refactorer.java

License:Apache License

@SuppressWarnings("unchecked")
protected void processMethodInvocation(MethodInvocation methodInvocation) {
    if (methodInvocation == null)
        return;/*from  w w  w .j  a v a2 s.c  om*/

    Expression methodExpression = methodInvocation.getExpression();
    if (methodExpression != null) {
        ITypeBinding type = methodExpression.resolveTypeBinding();
        String methodName = methodInvocation.getName().getFullyQualifiedName();
        if (type != null && methodName != null) {
            AST ast = methodExpression.getAST();
            String typeName = type.getQualifiedName();
            if (("javax.media.opengl.GLContext".equals(typeName)
                    || "javax.media.opengl.GLAutoDrawable".equals(type.getQualifiedName()))
                    && "getGL".equals(methodName)) {
                Expression newExpression = ast.newName("IGNORED");
                newExpression = (Expression) ASTNode.copySubtree(newExpression.getAST(),
                        methodInvocation.getExpression());
                MethodInvocation newInvocation = ast.newMethodInvocation();
                newInvocation.setExpression(newExpression);
                newInvocation.setName(ast.newSimpleName("getGL"));
                methodInvocation.setExpression(newInvocation);
                methodInvocation.setName(ast.newSimpleName("getGL2"));
            } else if ("javax.media.opengl.GL".equals(typeName)) {
                if (methodName.endsWith("EXT") || methodName.endsWith("ARB")) {
                    methodInvocation
                            .setName(ast.newSimpleName(methodName.substring(0, methodName.length() - 3)));
                }
            } else if ("com.sun.opengl.util.BufferUtil".equals(typeName)) {
                methodInvocation.setExpression(ast.newSimpleName("Buffers"));
                if ("newByteBuffer".equals(methodName)) {
                    methodInvocation.setName(ast.newSimpleName("newDirectByteBuffer"));
                } else if ("newIntBuffer".equals(methodName)) {
                    methodInvocation.setName(ast.newSimpleName("newDirectIntBuffer"));
                } else if ("newFloatBuffer".equals(methodName)) {
                    methodInvocation.setName(ast.newSimpleName("newDirectFloatBuffer"));
                } else if ("newDoubleBuffer".equals(methodName)) {
                    methodInvocation.setName(ast.newSimpleName("newDirectDoubleBuffer"));
                }
            } else if ("com.sun.opengl.util.texture.TextureIO".equals(typeName)) {
                boolean newTexture = "newTexture".equals(methodName);
                boolean newTextureData = "newTextureData".equals(methodName);
                if (newTexture || newTextureData) {
                    boolean addGLProfile = newTextureData;
                    if (methodInvocation.arguments().size() == 2) {
                        Expression secondArgument = (Expression) methodInvocation.arguments().get(0);
                        if ("java.awt.image.BufferedImage"
                                .equals(secondArgument.resolveTypeBinding().getQualifiedName())) {
                            methodInvocation.setExpression(ast.newSimpleName("AWTTextureIO"));
                            addImportIfRequired("com.jogamp.opengl.util.texture.awt.AWTTextureIO");
                            addGLProfile = true;
                        }
                    }

                    if (addGLProfile) {
                        MethodInvocation newMethodInvocation = ast.newMethodInvocation();
                        newMethodInvocation.setExpression(ast.newSimpleName("GLProfile"));
                        newMethodInvocation.setName(ast.newSimpleName("get"));
                        newMethodInvocation.arguments()
                                .add(ast.newQualifiedName(ast.newName("GLProfile"), ast.newSimpleName("GL2")));
                        methodInvocation.arguments().add(0, newMethodInvocation);
                        addImportIfRequired("javax.media.opengl.GLProfile");
                    }
                }
            } else if ("com.sun.opengl.util.texture.Texture".equals(typeName)
                    && ("bind".equals(methodName) || "getTextureObject".equals(methodName)
                            || "setTexParameteri".equals(methodName) || "dispose".equals(methodName)
                            || "enable".equals(methodName) || "disable".equals(methodName)
                            || "updateSubImage".equals(methodName) || "updateImage".equals(methodName))) {
                List<LocalVariable> locals = variables.getLocalVariablesMatchingType("javax.media.opengl.GL");
                if (!locals.isEmpty()) {
                    Name name = ast.newName(locals.get(0).name);
                    methodInvocation.arguments().add(name);
                } else {
                    locals = variables.getLocalVariablesMatchingType("gov.nasa.worldwind.render.DrawContext");
                    if (!locals.isEmpty()) {
                        MethodInvocation newMethodInvocation = ast.newMethodInvocation();
                        newMethodInvocation.setExpression(ast.newSimpleName(locals.get(0).name));
                        newMethodInvocation.setName(ast.newSimpleName("getGL"));
                        methodInvocation.arguments().add(0, newMethodInvocation);
                    } else {
                        //GLContext.getCurrent().getGL().getGL2()
                        MethodInvocation mi1 = ast.newMethodInvocation();
                        mi1.setExpression(ast.newName("GLContext"));
                        mi1.setName(ast.newSimpleName("getCurrent"));

                        MethodInvocation mi2 = ast.newMethodInvocation();
                        mi2.setExpression(mi1);
                        mi2.setName(ast.newSimpleName("getGL"));

                        MethodInvocation mi3 = ast.newMethodInvocation();
                        mi3.setExpression(mi2);
                        mi3.setName(ast.newSimpleName("getGL2"));

                        methodInvocation.arguments().add(0, mi3);

                        addImportIfRequired("javax.media.opengl.GLContext");
                    }
                }

                if ("dispose".equals(methodName)) {
                    methodInvocation.setName(ast.newSimpleName("destroy"));
                }
            } else if ("javax.media.opengl.GLAutoDrawable".equals(typeName) && "repaint".equals(methodName)) {
                ASTNode parent = methodInvocation.getParent().getParent();
                if (parent.getNodeType() == ASTNode.BLOCK || parent.getNodeType() == ASTNode.IF_STATEMENT) {
                    IfStatement ifStatement = ast.newIfStatement();
                    InstanceofExpression instanceofExpression = ast.newInstanceofExpression();
                    ifStatement.setExpression(instanceofExpression);

                    Expression leftOperation = ast.newName("IGNORED");
                    leftOperation = (Expression) ASTNode.copySubtree(leftOperation.getAST(),
                            methodInvocation.getExpression());
                    instanceofExpression.setLeftOperand(leftOperation);
                    instanceofExpression.setRightOperand(ast.newSimpleType(ast.newName("Component")));

                    Expression castExpressionExpression = ast.newName("IGNORED");
                    castExpressionExpression = (Expression) ASTNode
                            .copySubtree(castExpressionExpression.getAST(), methodInvocation.getExpression());

                    CastExpression castExpression = ast.newCastExpression();
                    castExpression.setExpression(castExpressionExpression);
                    castExpression.setType(ast.newSimpleType(ast.newName("Component")));

                    ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression();
                    parenthesizedExpression.setExpression(castExpression);
                    methodInvocation.setExpression(parenthesizedExpression);

                    if (parent.getNodeType() == ASTNode.BLOCK) {
                        Block parentBlock = (Block) parent;
                        parentBlock.statements().remove(methodInvocation.getParent());
                        parentBlock.statements().add(ifStatement);
                    } else if (parent.getNodeType() == ASTNode.IF_STATEMENT) {
                        IfStatement parentIfStatement = (IfStatement) parent;
                        if (parentIfStatement.getThenStatement() == methodInvocation.getParent()) {
                            parentIfStatement.setThenStatement(ifStatement);
                        } else if (parentIfStatement.getElseStatement() == methodInvocation.getParent()) {
                            parentIfStatement.setElseStatement(ifStatement);
                        }
                    }
                    ifStatement.setThenStatement((Statement) methodInvocation.getParent());

                    addImportIfRequired("java.awt.Component");
                }
            } else if ("com.sun.opengl.util.Screenshot".equals(typeName)) {
                methodInvocation.setExpression(ast.newSimpleName("Screenshot"));
                addImportIfRequired("com.jogamp.opengl.util.awt.Screenshot");
            }
        }
    }

    processExpression(methodInvocation.getExpression());
    for (Expression e : (List<Expression>) methodInvocation.arguments()) {
        processExpression(e);
    }
}