Example usage for org.eclipse.jdt.core.dom SingleVariableDeclaration getModifiers

List of usage examples for org.eclipse.jdt.core.dom SingleVariableDeclaration getModifiers

Introduction

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

Prototype

public int getModifiers() 

Source Link

Document

Returns the modifiers explicitly specified on this declaration.

Usage

From source file:com.bsiag.eclipse.jdt.java.formatter.LineBreaksPreparator.java

License:Open Source License

@Override
public boolean visit(SingleVariableDeclaration node) {
    this.declarationModifierVisited = false;
    if (node.getParent() instanceof MethodDeclaration) {
        // special case: annotations on parameters without modifiers should not be treated as type annotations
        this.declarationModifierVisited = (node.getModifiers() == 0);
    }//from w w  w  .  j  av  a 2s  .co m
    return true;
}

From source file:com.google.devtools.j2cpp.types.GeneratedMethodBinding.java

License:Open Source License

public GeneratedMethodBinding(MethodDeclaration m, ITypeBinding declaringClass, boolean isSynthetic) {
    this(m.getName().getIdentifier(), m.getModifiers(), getReturnType(m), declaringClass, m.isConstructor(),
            m.isVarargs(), isSynthetic);

    @SuppressWarnings("unchecked") // safe by definition
    List<SingleVariableDeclaration> params = m.parameters();
    for (SingleVariableDeclaration param : params) {
        GeneratedVariableBinding gvb = new GeneratedVariableBinding(param.getName().getIdentifier(),
                param.getModifiers(), Types.getTypeBinding(param), false, true, declaringClass, this);
        parameters.add(gvb);//from w  w w  .j av a2s  .  com
    }
}

From source file:com.ibm.wala.cast.java.translator.jdt.JDTJava2CAstTranslator.java

License:Open Source License

private CAstNode makeIteratorEnhancedForLoop(EnhancedForStatement n, WalkContext context) {
    // case 1: iterator
    CAstNode exprNode = visitNode(n.getExpression(), context);
    SingleVariableDeclaration svd = n.getParameter();
    Statement body = n.getBody();

    // expand into:

    // typical for loop:
    // { [inits]; while (cond) { [body]; [label continueTarget]; iters } [label breakTarget]
    // BLOCK(BLOCK(init1,init2,...),LOOP(cond,BLOCK(bodyblock,continuetarget,BLOCK(iter1,iter2,...))),breaktarget

    // in our case:
    // the only init is "Iterator iter = iterable.iter()"
    // cond is "iter.hasNext()"
    // bodyblock should be prepended with "[final] Type var = iter.next()" (put in the block that body belongs to)
    // iter is null
    // continuetarget and breaktarget are the same as in a regular for loop
    // BLOCK(iterassign,LOOP(cond,BLOCK(paramassign,bodyblock,continuetarget)),breaktarget)

    final String tmpName = "iter tmp"; // this is an illegal Java identifier, we will use this variable to hold the "invisible"
                                       // iterator

    /*-------- make "iter = iterator.iter()" ---------*/
    // make a fake method ref
    MethodReference iterMethodRef = fIdentityMapper.fakeMethodRefNoArgs(
            "Ljava/lang/Iterable;.iterator()Ljava/util/Iterator<TT;>;", "Ljava/lang/Iterable", "iterator",
            "Ljava/util/Iterator");
    CAstNode iterCallSiteRef = fFactory/*from  w  ww  . j  av a 2  s  . com*/
            .makeConstant(CallSiteReference.make(0, iterMethodRef, IInvokeInstruction.Dispatch.INTERFACE));
    // Iterable.iter() throws no exceptions.

    CAstNode iterCallNode = makeNode(context, fFactory, n, CAstNode.CALL, exprNode, iterCallSiteRef);
    // handle runtimeexception
    Object o1 = new Object(); // dummy object used for mapping / exceptions
    for (Pair<ITypeBinding, Object> catchTarget : context.getCatchTargets(fRuntimeExcType))
        context.cfg().add(o1, catchTarget.snd, catchTarget.fst);
    context.cfg().map(o1, iterCallNode); // TODO: this might not work, lots of calls in this one statement.

    CAstNode iterAssignNode = makeNode(context, fFactory, n, CAstNode.DECL_STMT, fFactory.makeConstant(
            new InternalCAstSymbol(tmpName, fTypeDict.getCAstTypeFor(ast.resolveWellKnownType("int")), true)),
            iterCallNode);

    // MATCHUP: wrap in a block
    iterAssignNode = makeNode(context, fFactory, n, CAstNode.BLOCK_STMT, iterAssignNode);

    // TODO: TOTEST: using this and Iterable.hasNext() explicitly in same file.

    /*---------- cond: iter.hasNext(); -----------*/
    MethodReference hasNextMethodRef = fIdentityMapper.fakeMethodRefNoArgs("Ljava/util/Iterator;.hasNext()Z",
            "Ljava/util/Iterator", "hasNext", "Z");
    CAstNode iterVar = makeNode(context, fFactory, n, CAstNode.VAR, fFactory.makeConstant(tmpName));
    CAstNode hasNextCallSiteRef = fFactory
            .makeConstant(CallSiteReference.make(0, hasNextMethodRef, IInvokeInstruction.Dispatch.INTERFACE));

    // throws no exceptions.
    CAstNode hasNextCallNode = makeNode(context, fFactory, n, CAstNode.CALL, iterVar, hasNextCallSiteRef);
    // handle runtimeexception
    Object o2 = new Object(); // dummy object used for mapping / exceptions
    for (Pair<ITypeBinding, Object> catchTarget : context.getCatchTargets(fRuntimeExcType))
        context.cfg().add(o2, catchTarget.snd, catchTarget.fst);
    context.cfg().map(o2, hasNextCallNode); // TODO: this might not work, lots of calls in this one statement.

    /*---------- paramassign: var = (Type) iter.next() ---------*/
    MethodReference nextMethodRef = fIdentityMapper.fakeMethodRefNoArgs("Ljava/util/Iterator;.next()TE;",
            "Ljava/util/Iterator", "next", "Ljava/lang/Object");
    CAstNode nextCallSiteRef = fFactory
            .makeConstant(CallSiteReference.make(0, nextMethodRef, IInvokeInstruction.Dispatch.INTERFACE));
    // throws no exceptions.
    CAstNode iterVar2 = makeNode(context, fFactory, n, CAstNode.VAR, fFactory.makeConstant(tmpName));
    CAstNode nextCallNode = makeNode(context, fFactory, n, CAstNode.CALL, iterVar2, nextCallSiteRef);
    for (Pair<ITypeBinding, Object> catchTarget : context.getCatchTargets(fRuntimeExcType))
        context.cfg().add(svd, catchTarget.snd, catchTarget.fst);
    context.cfg().map(svd, nextCallNode);

    // TODO: another cfg edge associated with svd! is this okay? prolly not... associate it with the cast, somehow...
    CAstNode castedNode = createCast(svd, nextCallNode, ast.resolveWellKnownType("java.lang.Object"),
            svd.resolveBinding().getType(), context);

    Object defaultValue = JDT2CAstUtils.defaultValueForType(svd.resolveBinding().getType());
    CAstNode nextAssignNode = makeNode(context, fFactory, n, CAstNode.DECL_STMT,
            fFactory.makeConstant(new CAstSymbolImpl(svd.getName().getIdentifier(),
                    fTypeDict.getCAstTypeFor(svd.resolveBinding().getType()),
                    (svd.getModifiers() & Modifier.FINAL) != 0, defaultValue)),
            castedNode);

    /*----------- put it all together ----------*/
    ASTNode breakTarget = makeBreakOrContinueTarget(n, "breakLabel" + n.getStartPosition());
    ASTNode continueTarget = makeBreakOrContinueTarget(n, "continueLabel" + n.getStartPosition());
    String loopLabel = context.getLabelMap().get(n);
    WalkContext loopContext = new LoopContext(context, loopLabel, breakTarget, continueTarget);

    // LOCAL_SCOPE(BLOCK(iterassign,LOOP(cond,BLOCK(BLOCK(paramassign,bodyblock),continuetarget,BLOCK())),breaktarget))
    return makeNode(context, fFactory, n, CAstNode.LOCAL_SCOPE, makeNode(context, fFactory, n,
            CAstNode.BLOCK_STMT, iterAssignNode,
            makeNode(context, fFactory, n, CAstNode.LOOP, hasNextCallNode,
                    makeNode(context, fFactory, n, CAstNode.BLOCK_STMT,
                            makeNode(context, fFactory, n, CAstNode.LOCAL_SCOPE,
                                    makeNode(context, fFactory, n, CAstNode.BLOCK_STMT, nextAssignNode,
                                            visitNode(body, loopContext))),
                            visitNode(continueTarget, context),
                            makeNode(context, fFactory, n, CAstNode.BLOCK_STMT))),
            visitNode(breakTarget, context)));
}

From source file:com.ibm.wala.cast.java.translator.jdt.JDTJava2CAstTranslator.java

License:Open Source License

private CAstNode makeArrayEnhancedForLoop(EnhancedForStatement n, WalkContext context) {
    // ********* BEFORE:
    // for ( String x: doSomething() ) { ... }
    // ********* AFTER:
    // {/*from  w  w  w.  j  a  v  a2s . co  m*/
    // String tmparray[] = doSomething();
    // for ( int tmpindex = 0; i < tmparray.length; tmpindex++ ) {
    // String x = tmparray[tmpindex];
    // ...
    // }
    // }
    // simplest:
    // LOCAL_SCOPE(BLOCK(arrayDecl,indexDecl,LOOP(cond,BLOCK(nextAssign,bodyblock,continuetarget,iter)),breaktarget))
    // match up exactly:
    // LOCAL_SCOPE(BLOCK(arrayDecl,LOCAL_SCOPE(BLOCK(BLOCK(indexDecl),LOOP(cond,BLOCK(LOCAL_SCOPE(BLOCK(nextAssign,bodyblock)),continuetarget,BLOCK(iter))),breaktarget))))

    /*------ arrayDecl --------- String tmparray[] = doSomething() ------*/
    final String tmpArrayName = "for temp array"; // illegal java identifier
    CAstNode exprNode = visitNode(n.getExpression(), context);
    CAstNode arrayDeclNode = makeNode(context, fFactory, n, CAstNode.DECL_STMT,
            fFactory.makeConstant(new InternalCAstSymbol(tmpArrayName,
                    fTypeDict.getCAstTypeFor(n.getExpression().resolveTypeBinding()), true)),
            exprNode);

    /*------ indexDecl --------- int tmpindex = 0 ------*/
    final String tmpIndexName = "for temp index";
    CAstNode indexDeclNode = makeNode(context, fFactory, n, CAstNode.DECL_STMT,
            fFactory.makeConstant(new InternalCAstSymbol(tmpIndexName,
                    fTypeDict.getCAstTypeFor(ast.resolveWellKnownType("int")), true)),
            fFactory.makeConstant(new Integer(0)));

    /*------ cond ------------- tmpindex < tmparray.length ------*/
    CAstNode tmpArrayLengthNode = makeNode(context, fFactory, n, CAstNode.ARRAY_LENGTH,
            makeNode(context, fFactory, n, CAstNode.VAR, fFactory.makeConstant(tmpArrayName),
                    fFactory.makeConstant(fTypeDict.getCAstTypeFor(n.getExpression().resolveTypeBinding()))));
    CAstNode condNode = makeNode(context, fFactory, n, CAstNode.BINARY_EXPR, CAstOperator.OP_LT,
            makeNode(context, fFactory, n, CAstNode.VAR, fFactory.makeConstant(tmpIndexName),
                    fFactory.makeConstant(JavaPrimitiveType.INT)),
            tmpArrayLengthNode);

    /*------ tmpIndexInc -------- tmpindex++ ------*/
    CAstNode tmpArrayIncNode = makeNode(context, fFactory, n, CAstNode.ASSIGN_POST_OP,
            makeNode(context, fFactory, n, CAstNode.VAR, fFactory.makeConstant(tmpIndexName)),
            fFactory.makeConstant(1), CAstOperator.OP_ADD);

    /*------ tmpArrayAccess ----- String x = tmparray[tmpindex] ------*/
    CAstNode tmpArrayAccessNode = makeNode(context, fFactory, n, CAstNode.ARRAY_REF,
            makeNode(context, fFactory, n, CAstNode.VAR, fFactory.makeConstant(tmpArrayName)),
            fFactory.makeConstant(
                    fIdentityMapper.getTypeRef(n.getExpression().resolveTypeBinding().getComponentType())),
            makeNode(context, fFactory, n, CAstNode.VAR, fFactory.makeConstant(tmpIndexName)));

    SingleVariableDeclaration svd = n.getParameter();
    Object defaultValue = JDT2CAstUtils.defaultValueForType(svd.resolveBinding().getType());
    CAstNode nextAssignNode = makeNode(context, fFactory, n, CAstNode.DECL_STMT,
            fFactory.makeConstant(new CAstSymbolImpl(svd.getName().getIdentifier(),
                    fTypeDict.getCAstTypeFor(n.getExpression().resolveTypeBinding().getComponentType()),
                    (svd.getModifiers() & Modifier.FINAL) != 0, defaultValue)),
            tmpArrayAccessNode);

    // LOCAL_SCOPE(BLOCK(arrayDecl,LOCAL_SCOPE(BLOCK(BLOCK(indexDecl),LOOP(cond,BLOCK(LOCAL_SCOPE(BLOCK(nextAssign,bodyblock)),continuetarget,BLOCK(iter))),breaktarget))))
    // more complicated than it has to be, but it matches up exactly with the Java expansion above.

    ASTNode breakTarget = makeBreakOrContinueTarget(n, "breakLabel" + n.getStartPosition());
    ASTNode continueTarget = makeBreakOrContinueTarget(n, "continueLabel" + n.getStartPosition());
    String loopLabel = (String) context.getLabelMap().get(n);
    WalkContext loopContext = new LoopContext(context, loopLabel, breakTarget, continueTarget);

    return makeNode(context, fFactory, n, CAstNode.LOCAL_SCOPE, makeNode(context, fFactory, n,
            CAstNode.BLOCK_STMT, arrayDeclNode,
            makeNode(context, fFactory, n, CAstNode.LOCAL_SCOPE, makeNode(context, fFactory, n,
                    CAstNode.BLOCK_STMT, makeNode(context, fFactory, n, CAstNode.BLOCK_STMT, indexDeclNode),
                    makeNode(context, fFactory, n, CAstNode.LOOP, condNode,
                            makeNode(context, fFactory, n, CAstNode.BLOCK_STMT,
                                    makeNode(context, fFactory, n, CAstNode.LOCAL_SCOPE,
                                            makeNode(context, fFactory, n, CAstNode.BLOCK_STMT, nextAssignNode,
                                                    visitNode(n.getBody(), loopContext))),
                                    visitNode(continueTarget, context),
                                    makeNode(context, fFactory, n, CAstNode.BLOCK_STMT, tmpArrayIncNode))),
                    visitNode(breakTarget, context)))));

}

From source file:edu.buffalo.cse.green.relationships.RelationshipVisitor.java

License:Open Source License

/**
 * Ensures no recognizers attempt to use static fields for relationships
 * //from w ww . jav  a2  s .c o  m
 * @author Gene Wang
 */
public boolean visit(SingleVariableDeclaration svd) {
    //      System.out.println("visiting SingleVariableDeclaration node");
    if (isField(svd.getName()) && (svd.getModifiers() & STATIC) != 0)
        return false;

    return true;
}

From source file:java5totext.input.JDTVisitor.java

License:Open Source License

@Override
public void endVisit(org.eclipse.jdt.core.dom.SingleVariableDeclaration node) {
    SingleVariableDeclaration element = (SingleVariableDeclaration) this.binding.get(node);
    this.initializeNode(element, node);
    element.setExtraArrayDimensions(node.getExtraDimensions());
    element.setVarargs(node.isVarargs());

    if (this.binding.get(node.getInitializer()) != null) {
        element.setInitializer((Expression) this.binding.get(node.getInitializer()));
    }/*from   ww w. ja v a2  s.c o m*/
    if (this.binding.get(node.getType()) != null) {
        element.setType((NamedElementRef) this.binding.get(node.getType()));
    }
    element.setName(node.getName().getIdentifier());
    // visibility modifiers
    for (Object modifierNode : node.modifiers()) {
        if ((((IExtendedModifier) modifierNode).isModifier()) && (this.binding.get(modifierNode) != null)) {
            element.getModifiers().add((Modifier) this.binding.get(modifierNode));
        }
    }
    JDTVisitorUtils.manageBindingDeclaration(element, node.getName(), this);
}

From source file:lang.java.jdt.internal.JdtAstToRascalAstConverter.java

License:Open Source License

public boolean visit(SingleVariableDeclaration node) {
    IValue name = values.string(node.getName().getFullyQualifiedName());

    java.util.Map.Entry<IValueList, IValueList> extendedModifiers;
    if (node.getAST().apiLevel() == AST.JLS2) {
        extendedModifiers = new java.util.AbstractMap.SimpleEntry<IValueList, IValueList>(
                parseModifiers(node.getModifiers()), new IValueList(values));
    } else {//from w ww  .j a v a 2 s . c om
        extendedModifiers = parseExtendedModifiers(node.modifiers());

    }

    IValue type = visitChild(node.getType());
    IValue initializer = node.getInitializer() == null ? null : visitChild(node.getInitializer());

    IValue isVarags = values.bool(false);
    if (node.getAST().apiLevel() >= AST.JLS3) {
        isVarags = values.bool(node.isVarargs());
    }

    /*
     * for (int i = 0; i < node.getExtraDimensions(); i++) { 
     * //TODO: What todo with the extra dimensions... 
     *       this.buffer.append("[]");
     * //$NON-NLS-1$ }
     */

    ownValue = constructRascalNode(node, name, extendedModifiers.getKey().asList(),
            extendedModifiers.getValue().asList(), type, optional(initializer), isVarags);
    return false;
}

From source file:net.sf.eclipsecs.ui.quickfixes.coding.FinalLocalVariableQuickfix.java

License:Open Source License

/**
 * {@inheritDoc}//ww w  .ja va2  s  . c  o  m
 */
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
    return new ASTVisitor() {

        public boolean visit(SingleVariableDeclaration node) {
            if (containsPosition(node, markerStartOffset) && !Modifier.isFinal(node.getModifiers())) {
                if (!Modifier.isFinal(node.getModifiers())) {
                    Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
                    node.modifiers().add(finalModifier);
                }
            }
            return true;
        }

        public boolean visit(VariableDeclarationStatement node) {
            if (containsPosition(node, markerStartOffset) && !Modifier.isFinal(node.getModifiers())) {
                if (!Modifier.isFinal(node.getModifiers())) {
                    Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
                    node.modifiers().add(finalModifier);
                }
            }
            return true;
        }
    };
}

From source file:net.sf.eclipsecs.ui.quickfixes.misc.FinalParametersQuickfix.java

License:Open Source License

/**
 * {@inheritDoc}/*from  w  ww.  j  a  va 2 s  . c  o m*/
 */
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
    return new ASTVisitor() {

        public boolean visit(SingleVariableDeclaration node) {
            if (containsPosition(node, markerStartOffset) && !Modifier.isFinal(node.getModifiers())) {
                if (!Modifier.isFinal(node.getModifiers())) {
                    Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
                    node.modifiers().add(finalModifier);
                }
            }
            return true;
        }
    };
}

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

License:Open Source License

public boolean visit(SingleVariableDeclaration node) {
    printIndent();//from  www.  j  av  a 2  s  .  c  om
    if (node.getAST().apiLevel() == JLS2) {
        printModifiers(node.getModifiers());
    }
    if (node.getAST().apiLevel() >= JLS3) {
        printModifiers(node.modifiers());
    }
    node.getType().accept(this);
    if (node.getAST().apiLevel() >= JLS3) {
        if (node.isVarargs()) {
            if (node.getAST().apiLevel() >= AST.JLS8) {
                List annotations = node.varargsAnnotations();
                if (annotations.size() > 0) {
                    this.buffer.append(' ');
                }
                visitAnnotationsList(annotations);
            }
            this.buffer.append("...");//$NON-NLS-1$
        }
    }
    this.buffer.append(" ");//$NON-NLS-1$
    node.getName().accept(this);
    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.getInitializer() != null) {
        this.buffer.append("=");//$NON-NLS-1$
        node.getInitializer().accept(this);
    }
    return false;
}