Example usage for org.eclipse.jdt.core.dom VariableDeclarationStatement setType

List of usage examples for org.eclipse.jdt.core.dom VariableDeclarationStatement setType

Introduction

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

Prototype

public void setType(Type type) 

Source Link

Document

Sets the base type declared in this variable declaration statement to the given type.

Usage

From source file:ch.acanda.eclipse.pmd.java.resolution.design.SingularFieldQuickFix.java

License:Open Source License

/**
 * Replaces the assignment with a variable declaration. If the assignment is the only one in the block for this
 * variable, the final modifier is added to the declaration.
 *//*from   w  w  w . j  av a2 s.c  o m*/
@SuppressWarnings("unchecked")
private void replaceAssignment(final VariableDeclarationFragment node, final Assignment assignment,
        final boolean finalDeclaration) {
    final FieldDeclaration fieldDeclaration = (FieldDeclaration) node.getParent();
    final VariableDeclarationStatement declaration = (VariableDeclarationStatement) node.getAST()
            .createInstance(VariableDeclarationStatement.class);
    declaration.setType(ASTUtil.copy(fieldDeclaration.getType()));
    final VariableDeclarationFragment fragment = (VariableDeclarationFragment) node.getAST()
            .createInstance(VariableDeclarationFragment.class);
    fragment.setName(ASTUtil.copy(node.getName()));
    fragment.setInitializer(ASTUtil.copy(assignment.getRightHandSide()));
    declaration.fragments().add(fragment);
    if (finalDeclaration) {
        final Modifier modifier = (Modifier) node.getAST().createInstance(Modifier.class);
        modifier.setKeyword(ModifierKeyword.FINAL_KEYWORD);
        declaration.modifiers().add(modifier);
    }
    ASTUtil.replace(assignment.getParent(), declaration);
}

From source file:cn.ieclipse.adt.ext.jdt.SourceGenerator.java

License:Apache License

private static void genInnerSQLiteOpenHelper(AnonymousClassDeclaration acd, AST ast,
        List<String> tableCreators) {
    MethodDeclaration md = ast.newMethodDeclaration();
    md.modifiers().addAll(ast.newModifiers((Modifier.PUBLIC)));
    md.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));
    md.setName(ast.newSimpleName("onCreate"));
    SingleVariableDeclaration svd = ast.newSingleVariableDeclaration();
    svd.setName(ast.newSimpleName("db"));
    svd.setType(ast.newSimpleType(ast.newSimpleName("SQLiteDatabase")));
    md.parameters().add(svd);/*  w  w  w. j av  a 2  s  .c  o m*/
    Block innerBlock = ast.newBlock();
    md.setBody(innerBlock);
    VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
    vdf.setName(ast.newSimpleName("sql"));
    StringLiteral sl = ast.newStringLiteral();
    sl.setLiteralValue("");
    vdf.setInitializer(sl);
    VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
    vds.setType(ast.newSimpleType(ast.newSimpleName("String")));
    innerBlock.statements().add(vds);
    for (String creator : tableCreators) {
        String[] lines = creator.split(SourceAnalysis.LF);
        for (String line : lines) {
            Assignment a = ast.newAssignment();
            a.setOperator(Assignment.Operator.PLUS_ASSIGN);
            a.setLeftHandSide(ast.newSimpleName("sql"));
            StringLiteral temp = ast.newStringLiteral();
            temp.setLiteralValue(line);
            a.setRightHandSide(temp);
            innerBlock.statements().add(ast.newExpressionStatement(a));
        }

        MethodInvocation mi = ast.newMethodInvocation();
        mi.setName(ast.newSimpleName("execSQL"));
        mi.setExpression(ast.newSimpleName("db"));
        mi.arguments().add(ast.newSimpleName("sql"));
        innerBlock.statements().add(ast.newExpressionStatement(mi));
    }

    acd.bodyDeclarations().add(md);
    // onUpgrade
    md = ast.newMethodDeclaration();
    md.modifiers().addAll(ast.newModifiers((Modifier.PUBLIC)));
    md.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));
    md.setName(ast.newSimpleName("onUpgrade"));
    svd = ast.newSingleVariableDeclaration();
    svd.setName(ast.newSimpleName("db"));
    svd.setType(ast.newSimpleType(ast.newSimpleName("SQLiteDatabase")));
    md.parameters().add(svd);

    svd = ast.newSingleVariableDeclaration();
    svd.setName(ast.newSimpleName("oldVersion"));
    svd.setType(ast.newPrimitiveType(PrimitiveType.INT));
    md.parameters().add(svd);

    svd = ast.newSingleVariableDeclaration();
    svd.setName(ast.newSimpleName("newVersion"));
    svd.setType(ast.newPrimitiveType(PrimitiveType.INT));
    md.parameters().add(svd);

    innerBlock = ast.newBlock();
    md.setBody(innerBlock);
    acd.bodyDeclarations().add(md);
}

From source file:com.google.devtools.j2cpp.translate.JavaToIOSTypeConverter.java

License:Open Source License

@Override
public boolean visit(VariableDeclarationStatement node) {
    @SuppressWarnings("unchecked")
    List<VariableDeclarationFragment> vars = node.fragments(); // safe by definition
    for (VariableDeclarationFragment var : vars) {
        IVariableBinding binding = Types.getVariableBinding(var);
        Type newType = Types.makeIOSType(binding.getType());
        if (newType != null) {
            ITypeBinding newTypeBinding = Types.getTypeBinding(newType);
            GeneratedVariableBinding varBinding = new GeneratedVariableBinding(NameTable.getName(binding),
                    binding.getModifiers(), newTypeBinding, false, false, binding.getDeclaringClass(),
                    binding.getDeclaringMethod());
            Types.addMappedVariable(var, varBinding);
            node.setType(newType);
        }/*from  w  w w .  j a v  a2 s . c o  m*/
    }

    return super.visit(node);
}

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

License:Apache License

public static VariableDeclarationStatement newVariableDeclarationStatement(AST ast,
        VariableDeclarationFragment fragment) {
    IVariableBinding varBinding = Types.getVariableBinding(fragment);
    VariableDeclarationStatement decl = ast.newVariableDeclarationStatement(fragment);
    decl.setType(newType(ast, varBinding.getType()));
    ASTUtil.getModifiers(decl).addAll(newModifiers(ast, varBinding.getModifiers()));
    return decl;/*from www  .  ja v a  2  s .  co m*/
}

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

License:Open Source License

private Statement getIDOCheckOutStatement(AST ast, Set imports) {
    VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
    vdf.setName(ast.newSimpleName("entity"));
    VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
    vds.setType(ast.newSimpleType(ast.newSimpleName("IDOEntity")));
    imports.add("com.idega.data.IDOEntity");

    ThisExpression thisExpression = ast.newThisExpression();
    MethodInvocation mi = ast.newMethodInvocation();
    mi.setExpression(thisExpression);//  ww  w  .j  a v  a  2s.c om
    mi.setName(ast.newSimpleName("idoCheckOutPooledEntity"));
    vdf.setInitializer(mi);

    return vds;
}

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

License:Open Source License

private Statement getDataCollectingStatement(AST ast, String returnType, String variableName, String methodName,
        String[] parameterNames) {
    VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
    vdf.setName(ast.newSimpleName(variableName));
    VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
    vds.setType(getType(ast, returnType));

    CastExpression ce = ast.newCastExpression();
    ce.setType(ast.newSimpleType(ast.newSimpleName(getType().getTypeQualifiedName())));
    ce.setExpression(ast.newSimpleName("entity"));

    ParenthesizedExpression pe = ast.newParenthesizedExpression();
    pe.setExpression(ce);/*from  w  ww.j a v  a 2s . co  m*/
    MethodInvocation mi = ast.newMethodInvocation();
    mi.setExpression(pe);
    mi.setName(ast.newSimpleName(methodName));
    vdf.setInitializer(mi);

    for (int i = 0; i < parameterNames.length; i++) {
        mi.arguments().add(ast.newSimpleName(parameterNames[i]));
    }

    return vds;
}

From source file:com.motorola.studio.android.generatemenucode.model.codegenerators.MenuHandlerCodeGenerator.java

License:Apache License

/**
 * Generates the following code://  w ww .  j av a2s  . c o m
 * <code>MenuInflater inflater = getMenuInflater();</code>
 * @param methodDeclaration
 */
protected void declareInflaterVariable(MethodDeclaration methodDeclaration) {
    MethodInvocation getMenuInflaterInvoke = createMethodInvocation(null,
            CodeGeneratorBasedOnMenuConstants.GET_MENU_INFLATER);
    VariableDeclarationFragment declarationFragment = typeDeclaration.getAST().newVariableDeclarationFragment();
    SimpleName inflater = typeDeclaration.getAST()
            .newSimpleName(CodeGeneratorBasedOnMenuConstants.INFLATER_VARIABLE);
    declarationFragment.setName(inflater);
    declarationFragment.setInitializer(getMenuInflaterInvoke);
    VariableDeclarationStatement declarationStatement = typeDeclaration.getAST()
            .newVariableDeclarationStatement(declarationFragment);
    SimpleName menuInflaterName = typeDeclaration.getAST()
            .newSimpleName(CodeGeneratorBasedOnMenuConstants.MENU_INFLATER_VARIABLE);
    SimpleType menuInflaterType = typeDeclaration.getAST().newSimpleType(menuInflaterName);
    declarationStatement.setType(menuInflaterType);

    addStatementIfNotFound(methodDeclaration, declarationStatement, false);
}

From source file:com.motorola.studio.android.generateviewbylayout.codegenerators.SaveStateCodeGenerator.java

License:Apache License

/**
 * Inserts SharedPreferences.Editor statement into onPause method
 * @param onPauseFoundMethod/*w w  w . j  ava  2s.  c  o m*/
 * @param preferencesVarName
 * @return
 */
@SuppressWarnings("unchecked")
public SimpleName insertPreferencesEditor(MethodDeclaration onPauseFoundMethod, SimpleName preferencesVarName) {
    SimpleName editorVarName = onPauseFoundMethod.getAST().newSimpleName(EDITOR);
    boolean alreadyAddedVariable = false;
    if (onPauseFoundMethod.getBody() != null) {
        outer: for (Object s : onPauseFoundMethod.getBody().statements()) {
            if (s instanceof VariableDeclarationStatement) {
                VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement) s;
                if (variableDeclarationStatement.getType().toString()
                        .equals(SHARED_PREFERENCES + "." + EDITOR_CAPITAL_LETTER)) {
                    for (Object f : variableDeclarationStatement.fragments()) {
                        VariableDeclarationFragment frag = (VariableDeclarationFragment) f;
                        if (frag.getName().toString().equals(EDITOR)) {
                            alreadyAddedVariable = true;
                            break outer;
                        }
                    }
                }
            }
        }
    }
    if (!alreadyAddedVariable) {
        VariableDeclarationFragment getEditorPreference = onPauseFoundMethod.getAST()
                .newVariableDeclarationFragment();
        MethodInvocation getEditorInvoke = onPauseFoundMethod.getAST().newMethodInvocation();
        SimpleName editInvokeName = onPauseFoundMethod.getAST().newSimpleName(EDIT);
        getEditorInvoke.setName(editInvokeName);
        SimpleName editInvokeVariable = onPauseFoundMethod.getAST()
                .newSimpleName(preferencesVarName.getIdentifier());
        getEditorInvoke.setExpression(editInvokeVariable);
        getEditorPreference.setInitializer(getEditorInvoke);

        getEditorPreference.setName(editorVarName);
        VariableDeclarationStatement getEditorVariableDeclarationStatement = onPauseFoundMethod.getAST()
                .newVariableDeclarationStatement(getEditorPreference);
        SimpleName sharedPreferencesName1 = onPauseFoundMethod.getAST().newSimpleName(SHARED_PREFERENCES);
        SimpleType type = onPauseFoundMethod.getAST().newSimpleType(sharedPreferencesName1);
        SimpleName sharedPreferencesName2 = onPauseFoundMethod.getAST().newSimpleName(EDITOR_CAPITAL_LETTER);
        QualifiedType editorPreferencesType = onPauseFoundMethod.getAST().newQualifiedType(type,
                sharedPreferencesName2);
        getEditorVariableDeclarationStatement.setType(editorPreferencesType);
        onPauseFoundMethod.getBody().statements().add(getEditorVariableDeclarationStatement);
    }
    return editorVarName;
}

From source file:com.motorola.studio.android.generateviewbylayout.codegenerators.SaveStateCodeGenerator.java

License:Apache License

/**
 * @param method//from w w w.jav a2 s.c o  m
 * @return {@link SimpleName} for the variable with SharedPreferences type inside the method body
 */
@SuppressWarnings("unchecked")
public SimpleName getPreferenceVariable(MethodDeclaration method) {
    SimpleName preferencesVarName = method.getAST().newSimpleName(PREFERENCES);
    boolean alreadyAddedVariable = false;
    if (method.getBody() != null) {
        outer: for (Object s : method.getBody().statements()) {
            if (s instanceof VariableDeclarationStatement) {
                VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement) s;
                if (variableDeclarationStatement.getType().toString().equals(SHARED_PREFERENCES)) {
                    for (Object f : variableDeclarationStatement.fragments()) {
                        VariableDeclarationFragment frag = (VariableDeclarationFragment) f;
                        if (frag.getName().toString().equals(PREFERENCES)) {
                            alreadyAddedVariable = true;
                            break outer;
                        }
                    }
                }
            }
        }
    }
    if (!alreadyAddedVariable) {
        VariableDeclarationFragment getPreferencefragment = method.getAST().newVariableDeclarationFragment();
        MethodInvocation invoke = method.getAST().newMethodInvocation();
        SimpleName invokeName = method.getAST().newSimpleName(GET_PREFERENCES);
        invoke.setName(invokeName);
        if (getCodeGeneratorData().getAssociatedType().equals(CodeGeneratorDataBasedOnLayout.TYPE.ACTIVITY)) {
            SimpleName invokeMode = method.getAST().newSimpleName(MODE_PRIVATE);
            invoke.arguments().add(invokeMode);
        } else if (getCodeGeneratorData().getAssociatedType()
                .equals(CodeGeneratorDataBasedOnLayout.TYPE.FRAGMENT)) {
            SimpleName invokeMode = method.getAST().newSimpleName(MODE_PRIVATE);
            SimpleName activityRef = method.getAST().newSimpleName("Activity");
            QualifiedName qName = method.getAST().newQualifiedName(activityRef, invokeMode);

            MethodInvocation activityInvoke = method.getAST().newMethodInvocation();
            SimpleName activityMethodName = method.getAST().newSimpleName("getActivity");
            activityInvoke.setName(activityMethodName);
            invoke.setExpression(activityInvoke);

            invoke.arguments().add(qName);
        }

        getPreferencefragment.setInitializer(invoke);
        getPreferencefragment.setName(preferencesVarName);
        VariableDeclarationStatement variableDeclarationStatement = method.getAST()
                .newVariableDeclarationStatement(getPreferencefragment);
        SimpleName sharedPreferencesName = method.getAST().newSimpleName(SHARED_PREFERENCES);
        SimpleType sharedPreferencesType = method.getAST().newSimpleType(sharedPreferencesName);
        variableDeclarationStatement.setType(sharedPreferencesType);
        method.getBody().statements().add(variableDeclarationStatement);
    }
    return preferencesVarName;
}

From source file:de.dentrassi.varlink.generator.JdtGenerator.java

License:Open Source License

@SuppressWarnings("unchecked")
private void createInternalMethod(final TypeDeclaration parentTypeDeclaration, final MethodInformation m,
        final MethodDeclaration md) {
    final AST ast = md.getAST();

    final Block body = ast.newBlock();
    md.setBody(body);/*from ww  w  .  ja v a2  s. c  o  m*/

    /*
     * return this.connection.call(CallRequest.of("io.systemd.network.List"))
     * .thenApply(cr -> { check(cr);
     *
     * final Iterator<JsonElement> i = cr.getParameters().values().iterator();
     *
     * return asList( this.varlink .fromJson( Netdev[].class, i.next())); }); }
     */

    // add arguments

    if (!m.getParameters().isEmpty()) {

        // code: Map<String,Object> parameters = new HashMap<> ();

        final VariableDeclarationFragment parameters = ast.newVariableDeclarationFragment();
        parameters.setName(ast.newSimpleName("parameters"));

        final VariableDeclarationStatement decl = ast.newVariableDeclarationStatement(parameters);
        body.statements().add(decl);
        final ParameterizedType map = ast.newParameterizedType(ast.newSimpleType(ast.newName("java.util.Map")));
        map.typeArguments().add(ast.newSimpleType(ast.newName("java.lang.String")));
        map.typeArguments().add(ast.newSimpleType(ast.newName("java.lang.Object")));

        decl.setType(map);

        final ClassInstanceCreation init = ast.newClassInstanceCreation();
        init.setType(ast.newParameterizedType(ast.newSimpleType(ast.newName("java.util.HashMap"))));
        init.arguments().add(ast.newNumberLiteral(Integer.toString(m.getParameters().size())));
        parameters.setInitializer(init);

        for (final String argName : m.getParameters().keySet()) {
            final MethodInvocation mi = ast.newMethodInvocation();
            mi.setExpression(ast.newSimpleName("parameters"));
            mi.setName(ast.newSimpleName("put"));

            mi.arguments().add(JdtHelper.newStringLiteral(ast, argName));
            mi.arguments().add(ast.newSimpleName(argName));
        }

    }

    // return

    final ReturnStatement ret = ast.newReturnStatement();
    body.statements().add(ret);

    final MethodInvocation mi = ast.newMethodInvocation();
    mi.setName(ast.newSimpleName("call"));
    final FieldAccess fa = ast.newFieldAccess();
    fa.setExpression(ast.newThisExpression());
    fa.setName(ast.newSimpleName("connection"));
    mi.setExpression(fa);

    final MethodInvocation cr = ast.newMethodInvocation();
    cr.setExpression(ast.newName("de.dentrassi.varlink.spi.CallRequest"));
    cr.setName(ast.newSimpleName("of"));
    cr.arguments().add(newStringLiteral(ast, m.getInterface().getName() + "." + toUpperFirst(m.getName())));

    if (!m.getParameters().isEmpty()) {
        cr.arguments().add(ast.newSimpleName("parameters"));
    }

    mi.arguments().add(cr);

    final MethodInvocation thenApply = ast.newMethodInvocation();
    thenApply.setName(ast.newSimpleName("thenApply"));
    thenApply.setExpression(mi);

    // add transformation

    final LambdaExpression le = ast.newLambdaExpression();
    le.setParentheses(false);
    thenApply.arguments().add(le);
    final VariableDeclarationFragment p = ast.newVariableDeclarationFragment();
    p.setName(ast.newSimpleName("result"));
    le.parameters().add(p);
    final Block transform = ast.newBlock();
    le.setBody(transform);

    {
        // check result

        final MethodInvocation check = ast.newMethodInvocation();
        check.setName(ast.newSimpleName("checkError"));
        transform.statements().add(ast.newExpressionStatement(check));
        check.arguments().add(ast.newSimpleName("result"));
    }

    if (m.getReturnTypes().isEmpty()) {

        final ReturnStatement transformRet = ast.newReturnStatement();
        transformRet.setExpression(ast.newNullLiteral());
        transform.statements().add(transformRet);

    } else {

        final int returns = m.getReturnTypes().size();

        if (returns > 0) {

            // return this.varlink.fromJson(DriveCondition.class, result.getParameters());
            // return this.varlink.fromJson(DriveCondition.class,
            // result.getFirstParameter());

            final FieldAccess varlink = ast.newFieldAccess();
            varlink.setExpression(ast.newThisExpression());
            varlink.setName(ast.newSimpleName("varlink"));

            final MethodInvocation fromJson = ast.newMethodInvocation();
            fromJson.setExpression(varlink);
            fromJson.setName(ast.newSimpleName("fromJson"));

            // FIXME: add to parent
            {
                final ParameterizedType ttt = ast
                        .newParameterizedType(ast.newSimpleType(ast.newName(TYPE_TOKEN_TYPE_NAME)));

                ttt.typeArguments().add(m.createMainReturnType(ast));
                final ClassInstanceCreation tt = ast.newClassInstanceCreation();
                tt.setType(JdtHelper.copyNode(ast, ttt));

                final AnonymousClassDeclaration decl = ast.newAnonymousClassDeclaration();
                tt.setAnonymousClassDeclaration(decl);

                final MethodInvocation getType = ast.newMethodInvocation();
                getType.setExpression(tt);
                getType.setName(ast.newSimpleName("getType"));

                final VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
                vdf.setName(ast.newSimpleName(m.getName() + "_returnTypeToken"));
                vdf.setInitializer(getType);
                final FieldDeclaration fd = ast.newFieldDeclaration(vdf);
                fd.setType(ast.newSimpleType(ast.newName("java.lang.reflect.Type")));
                make(fd, PRIVATE_KEYWORD, FINAL_KEYWORD, STATIC_KEYWORD);

                parentTypeDeclaration.bodyDeclarations().add(fd);
            }

            fromJson.arguments().add(ast.newSimpleName(m.getName() + "_returnTypeToken"));

            // json fragment

            final MethodInvocation fragment = ast.newMethodInvocation();
            if (returns == 1) {
                fragment.setName(ast.newSimpleName("getFirstParameter"));
            } else {
                fragment.setName(ast.newSimpleName("getParameters"));
            }
            fragment.setExpression(ast.newSimpleName("result"));

            fromJson.arguments().add(fragment);

            // return

            final ReturnStatement transformRet = ast.newReturnStatement();
            transformRet.setExpression(fromJson);
            transform.statements().add(transformRet);
        }

        // FIXME: handle return type

        // FIXME: handle n

    }

    // set return

    ret.setExpression(thenApply);
}