Example usage for org.eclipse.jdt.core.dom VariableDeclarationFragment getStartPosition

List of usage examples for org.eclipse.jdt.core.dom VariableDeclarationFragment getStartPosition

Introduction

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

Prototype

public final int getStartPosition() 

Source Link

Document

Returns the character index into the original source file indicating where the source fragment corresponding to this node begins.

Usage

From source file:br.uff.ic.gems.resources.ast.Visitor.java

@Override
public boolean visit(FieldDeclaration node) {

    List<VariableDeclarationFragment> fragments = node.fragments();

    for (VariableDeclarationFragment fragment : fragments) {
        int beginLine = cu.getLineNumber(fragment.getStartPosition());
        int endLine = cu.getLineNumber(fragment.getStartPosition() + fragment.getLength());
        int beginColumn = beginColunm(node);
        int endColumn = cu.getColumnNumber(node.getStartPosition() + node.getLength());

        SimpleName name = fragment.getName();

        if (name != null) {
            endLine = cu.getLineNumber(name.getStartPosition() + name.getLength());
            endColumn = cu.getColumnNumber(name.getStartPosition() + name.getLength());
        }//ww  w  . ja va 2  s.  c om

        languageConstructs.add(new LanguageConstruct(node.getClass().getSimpleName(), beginLine, endLine,
                beginColumn, endColumn, fragment.getName().getIdentifier()));

    }

    return true;
}

From source file:br.uff.ic.gems.resources.ast.Visitor.java

@Override
public boolean visit(VariableDeclarationExpression node) {

    List<VariableDeclarationFragment> fragments = node.fragments();
    for (VariableDeclarationFragment fragment : fragments) {
        int beginLine = cu.getLineNumber(fragment.getStartPosition());
        int endLine = cu.getLineNumber(fragment.getStartPosition() + fragment.getLength());
        int beginColumn = cu.getColumnNumber(node.getStartPosition());
        int endColumn = cu.getColumnNumber(node.getStartPosition() + node.getLength());

        SimpleName name = fragment.getName();

        if (name != null) {
            endLine = cu.getLineNumber(name.getStartPosition() + name.getLength());
            endColumn = cu.getColumnNumber(name.getStartPosition() + name.getLength());
        }/*from   w  w w.ja  va  2s.  co  m*/

        languageConstructs.add(new LanguageConstruct(node.getClass().getSimpleName(), beginLine, endLine,
                beginColumn, endColumn, fragment.getName().getIdentifier()));
    }

    return true;
}

From source file:br.uff.ic.gems.resources.ast.Visitor.java

@Override
public boolean visit(VariableDeclarationStatement node) {

    List<VariableDeclarationFragment> fragments = node.fragments();

    for (VariableDeclarationFragment fragment : fragments) {
        String identifier = fragment.getName().getIdentifier();
        int beginLine = cu.getLineNumber(fragment.getStartPosition());
        int endLine = cu.getLineNumber(fragment.getStartPosition() + fragment.getLength());
        int beginColumn = cu.getColumnNumber(node.getStartPosition());
        int endColumn = cu.getColumnNumber(node.getStartPosition() + node.getLength());

        SimpleName name = fragment.getName();

        if (name != null) {
            endLine = cu.getLineNumber(name.getStartPosition() + name.getLength());
            endColumn = cu.getColumnNumber(name.getStartPosition() + name.getLength());
        }//from ww  w .ja  v  a  2 s  .  c om

        languageConstructs.add(new LanguageConstruct(node.getClass().getSimpleName(), beginLine, endLine,
                beginColumn, endColumn, identifier));

    }

    return true;
}

From source file:br.uff.ic.mergeguider.javaparser.DepVisitor.java

@Override
public boolean visit(FieldDeclaration node) {

    Location location;/* w w  w  . ja v  a2s  .  c o m*/

    List<VariableDeclarationFragment> fragments = node.fragments();

    for (VariableDeclarationFragment fragment : fragments) {

        int elementLineBegin = cu.getLineNumber(fragment.getStartPosition());
        int elementLineEnd = cu.getLineNumber(fragment.getStartPosition() + node.getLength());
        int elementColumnBegin = cu.getColumnNumber(fragment.getStartPosition());
        int elementColumnEnd = cu.getColumnNumber(fragment.getStartPosition() + node.getLength());

        location = new Location(elementLineBegin, elementLineEnd, elementColumnBegin, elementColumnEnd);

        MyAttributeDeclaration myAttribute = new MyAttributeDeclaration(fragment, location);

        if (!classLanguageConstructsList.isEmpty()) {
            classLanguageConstructsList.get(classLanguageConstructsList.size() - 1).getAttributes()
                    .add(myAttribute);
        }

    }

    return true;
}

From source file:br.uff.ic.mergeguider.javaparser.DepVisitor.java

@Override
public boolean visit(VariableDeclarationExpression node) {

    Location location;//w w  w . j  av  a2 s  .c o  m

    List<VariableDeclarationFragment> fragments = node.fragments();

    for (VariableDeclarationFragment fragment : fragments) {

        int elementLineBegin = cu.getLineNumber(fragment.getStartPosition());
        int elementLineEnd = cu.getLineNumber(fragment.getStartPosition() + node.getLength());
        int elementColumnBegin = cu.getColumnNumber(fragment.getStartPosition());
        int elementColumnEnd = cu.getColumnNumber(fragment.getStartPosition() + node.getLength());

        location = new Location(elementLineBegin, elementLineEnd, elementColumnBegin, elementColumnEnd);

        MyVariableDeclaration myVariableDeclaration = new MyVariableDeclaration(fragment, location);

        if (!classLanguageConstructsList.isEmpty()) {
            classLanguageConstructsList.get(classLanguageConstructsList.size() - 1).getVariableDeclarations()
                    .add(myVariableDeclaration);
        }

    }

    return true;

}

From source file:br.uff.ic.mergeguider.javaparser.DepVisitor.java

@Override
public boolean visit(VariableDeclarationStatement node) {

    Location location;/*w w  w  . j a  va 2s .  c  om*/

    List<VariableDeclarationFragment> fragments = node.fragments();

    for (VariableDeclarationFragment fragment : fragments) {

        int elementLineBegin = cu.getLineNumber(fragment.getStartPosition());
        int elementLineEnd = cu.getLineNumber(fragment.getStartPosition() + node.getLength());
        int elementColumnBegin = cu.getColumnNumber(fragment.getStartPosition());
        int elementColumnEnd = cu.getColumnNumber(fragment.getStartPosition() + node.getLength());

        location = new Location(elementLineBegin, elementLineEnd, elementColumnBegin, elementColumnEnd);

        MyVariableDeclaration myVariableDeclaration = new MyVariableDeclaration(fragment, location);

        if (!classLanguageConstructsList.isEmpty()) {
            classLanguageConstructsList.get(classLanguageConstructsList.size() - 1).getVariableDeclarations()
                    .add(myVariableDeclaration);
        }

    }

    return true;

}

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

License:Open Source License

/**
 * // ww  w  .java  2  s. c om
 * @param n
 * @param bodyDecls
 * @param enumConstants
 * @param typeBinding
 * @param name Used in creating default constructor, and passed into new ClassEntity()
 * @param context
 * @return
 */
private CAstEntity createClassDeclaration(ASTNode n, List/* <BodyDeclaration> */ bodyDecls,
        List/* EnumConstantDeclaration */ enumConstants, ITypeBinding typeBinding, String name, int modifiers,
        boolean isInterface, boolean isAnnotation, WalkContext context) {
    final List<CAstEntity> memberEntities = new ArrayList<CAstEntity>();

    // find and collect all initializers (type Initializer) and field initializers (type VariableDeclarationFragment).
    // instance initializer code will be inserted into each constructors.
    // all static initializer code will be grouped together in its own entity.
    ArrayList<ASTNode> inits = new ArrayList<ASTNode>();
    ArrayList<ASTNode> staticInits = new ArrayList<ASTNode>();

    if (enumConstants != null) {
        for (Object decl : enumConstants) {
            EnumConstantDeclaration ecd = (EnumConstantDeclaration) decl;
            staticInits.add(ecd); // always (implicitly) static,final (actually, no modifiers allowed)
        }
    }

    for (Object decl : bodyDecls) {
        if (decl instanceof Initializer) {
            Initializer initializer = (Initializer) decl;
            boolean isStatic = ((initializer.getModifiers() & Modifier.STATIC) != 0);
            (isStatic ? staticInits : inits).add(initializer);
        } else if (decl instanceof FieldDeclaration) {
            FieldDeclaration fd = (FieldDeclaration) decl;

            for (Object f : fd.fragments()) {
                VariableDeclarationFragment frag = (VariableDeclarationFragment) f;
                if (frag.getInitializer() != null) {
                    boolean isStatic = ((fd.getModifiers() & Modifier.STATIC) != 0);
                    (isStatic ? staticInits : inits).add(frag);
                }
            }
        }
    }

    // process entities. initializers will be folded in here.
    if (enumConstants != null) {
        for (Object decl : enumConstants) {
            memberEntities.add(visit((EnumConstantDeclaration) decl, context));
        }
    }

    for (Object d : bodyDecls) {
        BodyDeclaration decl = (BodyDeclaration) d;
        if (decl instanceof FieldDeclaration) {
            FieldDeclaration fieldDecl = (FieldDeclaration) decl;
            Collection<CAstQualifier> quals = JDT2CAstUtils.mapModifiersToQualifiers(fieldDecl.getModifiers(),
                    false, false);
            for (Object f : fieldDecl.fragments()) {
                VariableDeclarationFragment fieldFrag = (VariableDeclarationFragment) f;
                IVariableBinding fieldBinding = fieldFrag.resolveBinding();
                memberEntities
                        .add(new FieldEntity(fieldFrag.getName().getIdentifier(), fieldBinding.getType(), quals,
                                makePosition(fieldFrag.getStartPosition(),
                                        fieldFrag.getStartPosition() + fieldFrag.getLength()),
                                handleAnnotations(fieldBinding)));
            }
        } else if (decl instanceof Initializer) {
            // Initializers are inserted into constructors when making constructors.
        } else if (decl instanceof MethodDeclaration) {
            MethodDeclaration metDecl = (MethodDeclaration) decl;

            if (typeBinding.isEnum() && metDecl.isConstructor())
                memberEntities.add(createEnumConstructorWithParameters(metDecl.resolveBinding(), metDecl,
                        context, inits, metDecl));
            else {
                memberEntities.add(visit(metDecl, typeBinding, context, inits));

                // /////////////// Java 1.5 "overridden with subtype" thing (covariant return type) ///////////
                Collection<IMethodBinding> overriddenMets = JDT2CAstUtils
                        .getOverriddenMethod(metDecl.resolveBinding());
                if (overriddenMets != null) {
                    for (IMethodBinding overridden : overriddenMets)
                        if (!JDT2CAstUtils.sameErasedSignatureAndReturnType(metDecl.resolveBinding(),
                                overridden))
                            memberEntities.add(makeSyntheticCovariantRedirect(metDecl, metDecl.resolveBinding(),
                                    overridden, context));
                }
            }
        } else if (decl instanceof AbstractTypeDeclaration) {
            memberEntities.add(visit((AbstractTypeDeclaration) decl, context));
        } else if (decl instanceof AnnotationTypeMemberDeclaration) {
            // TODO: need to decide what to do with these
        } else {
            Assertions.UNREACHABLE("BodyDeclaration not Field, Initializer, or Method");
        }
    }

    // add default constructor(s) if necessary
    // most default constructors have no parameters; however, those created by anonymous classes will have parameters
    // (they just call super with those parameters)
    for (Object m : typeBinding.getDeclaredMethods()) {
        IMethodBinding met = (IMethodBinding) m;
        if (met.isDefaultConstructor()) {
            if (typeBinding.isEnum())
                memberEntities.add(createEnumConstructorWithParameters(met, n, context, inits, null));
            else if (met.getParameterTypes().length > 0)
                memberEntities.add(createDefaultConstructorWithParameters(met, n, context, inits));
            else
                memberEntities.add(createDefaultConstructor(name, typeBinding, context, inits, n));
        }
    }

    if (typeBinding.isEnum() && !typeBinding.isAnonymous())
        doEnumHiddenEntities(typeBinding, staticInits, memberEntities, context);

    // collect static inits
    if (!staticInits.isEmpty()) {
        Map<CAstNode, CAstEntity> childEntities = HashMapFactory.make();
        final MethodContext newContext = new MethodContext(context, childEntities);
        // childEntities is the same one as in the ProcedureEntity. later visit(New), etc. may add to this.

        CAstNode[] bodyNodes = new CAstNode[staticInits.size()];
        for (int i = 0; i < staticInits.size(); i++)
            bodyNodes[i] = visitFieldInitNode(staticInits.get(i), newContext);
        CAstNode staticInitAst = makeNode(newContext, fFactory, n, CAstNode.BLOCK_STMT, bodyNodes);
        memberEntities.add(new ProcedureEntity(staticInitAst, typeBinding, childEntities, newContext, null));
    }

    Collection<CAstQualifier> quals = JDT2CAstUtils.mapModifiersToQualifiers(modifiers, isInterface,
            isAnnotation);

    Set<CAstAnnotation> annotations = handleAnnotations(typeBinding);

    return new ClassEntity(typeBinding, name, quals, memberEntities, makePosition(n), annotations);
}

From source file:com.motorolamobility.preflighting.core.internal.utils.ProjectUtils.java

License:Apache License

private static void analizeBody(final CompilationUnit javaCompilationUnit, final Method method, Block body) {
    body.accept(new ASTVisitor() {

        @Override/*from  w  ww  . j  ava 2 s .  com*/
        public boolean visit(VariableDeclarationFragment node) {
            String varName = node.getName().getIdentifier();
            ITypeBinding typeBinding = node.resolveBinding().getType();
            String typeQualifiedName = typeBinding.getQualifiedName();
            int modifiers = typeBinding.getModifiers();
            boolean isFinal = isFinal(modifiers);
            boolean isStatic = isStatic(modifiers);
            String value = null;
            Expression initializer = node.getInitializer();
            if (initializer != null) {
                value = initializer.toString();
            }
            int lineNumber = javaCompilationUnit.getLineNumber(node.getStartPosition());

            Variable variable = new Variable();
            variable.setName(varName);
            variable.setType(typeQualifiedName);
            variable.setFinal(isFinal);
            variable.setStatic(isStatic);
            variable.setValue(value);
            variable.setLineNumber(lineNumber);

            method.addVariable(variable);

            return super.visit(node);
        }

        @Override
        public boolean visit(MethodInvocation node) {
            // Fill invoked method model.
            MethodInvocation invoked = node;
            IMethodBinding methodBinding = invoked.resolveMethodBinding();
            if (methodBinding != null) {
                IMethodBinding methodDeclaration = methodBinding.getMethodDeclaration();
                ITypeBinding declaringClass = methodDeclaration.getDeclaringClass();
                String declaringClassName = "";
                if (declaringClass != null) {
                    declaringClassName = declaringClass.getQualifiedName();
                }
                String methodSimpleName = methodBinding.getName();
                int lineNumber = javaCompilationUnit.getLineNumber(invoked.getStartPosition());
                String returnType = methodBinding.getReturnType().getQualifiedName();
                int methodModifiers = methodBinding.getModifiers();
                boolean isVirtual = isMethodVirtual(methodModifiers);
                String sourceFileFullPath = ((File) javaCompilationUnit.getProperty(JAVA_FILE_PROPERTY))
                        .getAbsolutePath();

                // Retrieve parameter types and look for R constants used
                // within method arguments
                List arguments = invoked.arguments();
                List<String> parameterTypes = new ArrayList<String>(arguments.size());
                List<String> parameterNames = new ArrayList<String>(arguments.size());
                for (Object argument : arguments) {
                    Expression argumentExpression = (Expression) argument;
                    ITypeBinding typeBinding = argumentExpression.resolveTypeBinding();
                    String parameterType = "";
                    String parameterName = "";
                    if (typeBinding != null) {
                        parameterType = typeBinding.getName();
                        parameterName = argumentExpression.toString();
                    } else {
                        continue;
                    }

                    parameterTypes.add(parameterType);
                    parameterNames.add(parameterName);
                    if (argumentExpression instanceof QualifiedName) /*
                                                                     * Can
                                                                     * be a
                                                                     * constant
                                                                     * access
                                                                     */
                    {
                        QualifiedName qualifiedName = (QualifiedName) argumentExpression;
                        String fullQualifiedName = qualifiedName.getQualifier().getFullyQualifiedName();
                        if (fullQualifiedName.startsWith("R.")) /*
                                                                 * Accessing
                                                                 * a R
                                                                 * constant
                                                                 */
                        {
                            Constant constant = new Constant();
                            constant.setSourceFileFullPath(sourceFileFullPath);
                            constant.setLine(lineNumber);
                            constant.setType(parameterType);
                            Object constantExpressionValue = qualifiedName.resolveConstantExpressionValue();
                            if (constantExpressionValue != null) {
                                String constantValueHex = constantExpressionValue.toString();
                                if (constantExpressionValue instanceof Integer) {
                                    Integer integerValue = (Integer) constantExpressionValue;
                                    constantValueHex = Integer.toHexString(integerValue);
                                }
                                constant.setValue(constantValueHex);
                                method.getInstructions().add(constant);
                            }
                        }

                    }
                }

                // Get the name of the object who owns the method being
                // called.
                Expression expression = invoked.getExpression();
                String objectName = null;
                if ((expression != null) && (expression instanceof SimpleName)) {
                    SimpleName simpleName = (SimpleName) expression;
                    objectName = simpleName.getIdentifier();
                }

                // Get the variable, if any, that received the method
                // returned value
                ASTNode parent = invoked.getParent();
                String assignedVariable = null;
                if (parent instanceof VariableDeclarationFragment) {
                    VariableDeclarationFragment variableDeclarationFragment = (VariableDeclarationFragment) parent;
                    assignedVariable = variableDeclarationFragment.getName().getIdentifier();
                } else if (parent instanceof Assignment) {
                    Assignment assignment = (Assignment) parent;
                    Expression leftHandSide = assignment.getLeftHandSide();
                    if (leftHandSide instanceof SimpleName) {
                        SimpleName name = (SimpleName) leftHandSide;
                        assignedVariable = name.getIdentifier();
                    }
                }

                // Fill Invoke object and add to the method model.
                Invoke invoke = new Invoke();
                invoke.setLine(lineNumber);
                invoke.setMethodName(methodSimpleName);
                invoke.setObjectName(objectName);
                invoke.setType(getMethodTypeString(isVirtual));
                invoke.setReturnType(returnType);
                invoke.setClassCalled(declaringClassName);
                invoke.setParameterTypes(parameterTypes);
                invoke.setParameterNames(parameterNames);
                invoke.setSourceFileFullPath(sourceFileFullPath);
                invoke.setAssignedVariable(assignedVariable);

                method.getInstructions().add(invoke);
            }
            return super.visit(node);
        }

        @Override
        public boolean visit(VariableDeclarationExpression node) {

            return super.visit(node);
        }

        @Override
        public boolean visit(Assignment node) {
            Expression lhs = node.getLeftHandSide();
            String name = "";
            if (lhs instanceof SimpleName) {
                SimpleName simpleName = (SimpleName) lhs;
                name = simpleName.getIdentifier();
            }
            ITypeBinding typeBinding = lhs.resolveTypeBinding();
            String type = typeBinding.getName();
            // method.addAssigment(assignment);

            // TODO Auto-generated method stub
            return super.visit(node);
        }
    });
}

From source file:eu.cloudwave.wp5.feedback.eclipse.performance.core.ast.CollectionSourceDetector.java

License:Apache License

private Optional<Procedure> getLocalVariableProcedureAssignment(final String variableName,
        final MethodDeclaration methodDeclaration, final int maxPosition) {
    final ProcedureWrapper procedureBucket = new ProcedureWrapper();

    methodDeclaration.getBody().accept(new ASTVisitor() {

        @Override/*from w  w  w .  j  a  va2  s  . co m*/
        public boolean visit(final VariableDeclarationFragment node) {
            if (node.getStartPosition() < maxPosition && node.getName().toString().equals(variableName)) {
                setSourceProcedure(node.getInitializer());
            }
            return false;
        }

        @Override
        public boolean visit(final Assignment node) {
            if (node.getStartPosition() < maxPosition
                    && node.getLeftHandSide().toString().equals(variableName)) {
                setSourceProcedure(node.getRightHandSide());
            }
            return false;
        }

        private void setSourceProcedure(final Expression expression) {
            if (expression != null && expression instanceof MethodInvocation) {
                final MethodInvocation methodInvocation = ((MethodInvocation) expression);
                procedureBucket
                        .set(new MethodInvocationExtension(methodInvocation).createCorrelatingProcedure());
            }
        }
    });

    return procedureBucket.get();
}

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

License:Open Source License

@Override
public boolean visit(VariableDeclarationFragment node) {
    if (fieldOffsetsFromLoc.contains(VF.integer(node.getParent().getStartPosition()))) {
        ICompilationUnit icu = JavaCore.createCompilationUnitFrom(file);
        try {//from  w ww  . ja  v  a2s .c o m
            IJavaElement fieldElement = icu.getElementAt(node.getStartPosition());
            if (fieldElement != null && fieldElement instanceof IField) {
                fieldSet.add((IField) fieldElement);
            }
        } catch (JavaModelException e) {
            ISourceLocation pos = VF.sourceLocation(loc.getURI(), node.getStartPosition(), node.getLength());
            throw new Throw(VF.string("Error during field find visit: " + e.getMessage()), pos, null);
        }
    }
    return true;
}