Example usage for org.eclipse.jdt.core.dom IBinding equals

List of usage examples for org.eclipse.jdt.core.dom IBinding equals

Introduction

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

Prototype

@Override
public boolean equals(Object obj);

Source Link

Document

There is no special definition of equality for bindings; equality is simply object identity.

Usage

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

public void treatingSimpleName(SimpleName node) {
    IBinding simpleNameBinding = node.resolveBinding();

    if (simpleNameBinding == null) {
        return;// w w  w.ja v a2  s.  com
    }

    //Treating Variable or Attribute
    if (simpleNameBinding instanceof IVariableBinding || simpleNameBinding instanceof ITypeBinding) {

        //Treating Attribute
        for (MyAttributeDeclaration attribute : classLanguageConstructsList
                .get(classLanguageConstructsList.size() - 1).getAttributes()) {

            IVariableBinding attributeBinding = attribute.getFieldDeclaration().resolveBinding();
            if (attributeBinding == null) {
                continue;
            }

            if (simpleNameBinding.equals(attributeBinding)) {

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

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

                if (!attribute.getLocation().contains(location)) {

                    MyAttributeCall myAttributeCall = new MyAttributeCall(node, location);

                    if (!classLanguageConstructsList.isEmpty()) {
                        classLanguageConstructsList.get(classLanguageConstructsList.size() - 1)
                                .getAttributeCalls().add(myAttributeCall);
                    }
                }
            }

        }

        //Treating variables
        for (MyVariableDeclaration variableDeclaration : classLanguageConstructsList
                .get(classLanguageConstructsList.size() - 1).getVariableDeclarations()) {

            IVariableBinding attributeBinding = variableDeclaration.resolveBinding();
            if (attributeBinding == null) {
                continue;
            }

            if (simpleNameBinding.equals(attributeBinding)) {

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

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

                if (!variableDeclaration.getLocation().contains(location)) {

                    MyVariableCall myVariableCall = new MyVariableCall(node, location);

                    classLanguageConstructsList.get(classLanguageConstructsList.size() - 1).getVariableCalls()
                            .add(myVariableCall);
                }
            }

        }
    }
}

From source file:edu.cmu.cs.crystal.tac.eclipse.EclipseTAC.java

License:Open Source License

/**
 * Depth-first search in class and interface hierarchy to find a method or field.
 * @tag usage.parameter: Clients will usually want to pass <code>false</code> for both flags; they will be used in recursive calls.
 * @param genericAccessedElement Most generic version of the binding to be searched for.
 * @param isMethod This must match the type of <code>genericAccessedElement</code>:
 * <code>true</code> to resolve a method, <code>false</code> to resolve a field.
 * @param type Type to search.//  www  .  ja va 2s. c om
 * @param skipPrivate If <code>true</code>, private declarations are ignored.
 * @param skipPackagePrivate If <code>true</code>, package-private declarations are ignored.
 * @return First type found in the hierarchy that declares the given method or field.
 */
private ITypeBinding findElementDeclarationByName(IBinding genericAccessedElement, boolean isMethod,
        ITypeBinding type, boolean skipPrivate, boolean skipPackagePrivate) {
    if (isMethod) {
        for (IMethodBinding b : type.getDeclaredMethods()) {
            if (skipPrivate && Modifier.isPrivate(b.getModifiers()))
                continue; // skip private method
            if (skipPackagePrivate && isDefaultBinding(b))
                continue; // skip package-private method
            if (genericAccessedElement.equals(b.getMethodDeclaration() /* use generic method */)) {
                return type;
            }
        }
    } else {
        for (IVariableBinding b : type.getDeclaredFields()) {
            if (skipPrivate && Modifier.isPrivate(b.getModifiers()))
                continue; // skip private field
            if (skipPackagePrivate && isDefaultBinding(b))
                continue; // skip package-private field
            if (genericAccessedElement.equals(b.getVariableDeclaration() /* use generic field */))
                return type;
        }
    }

    ITypeBinding result = null;
    if (type.getSuperclass() != null) {
        ITypeBinding t = type.getSuperclass();
        result = findElementDeclarationByName(genericAccessedElement, isMethod, t,
                true /* always skip private declarations in supertypes */, !t.getPackage()
                        .equals(type.getPackage()) /* skip package-private when leaving current package */);
        if (result != null)
            return result;
    }
    // go though interfaces as well
    for (ITypeBinding i : type.getInterfaces()) {
        // keep going for fields: could be static
        result = findElementDeclarationByName(genericAccessedElement, isMethod, i,
                true /* always skip private declarations in supertypes */, !i.getPackage()
                        .equals(type.getPackage()) /* skip package-private when leaving current package */);
        if (result != null)
            return result;
    }
    return result;
}

From source file:nl.han.ica.core.issue.detector.EncapsulateFieldDetector.java

@Override
public void detectIssues() {
    for (CompilationUnit compilationUnit : compilationUnits) {
        FieldAccessVisitor fieldAccessVisitor = new FieldAccessVisitor();
        compilationUnit.accept(fieldAccessVisitor);
        if (!fieldAccessVisitor.getQualifiedNameList().isEmpty()) {
            qualifiedNamesList.addAll(fieldAccessVisitor.getQualifiedNameList());
        }//from   w  ww  . ja va2s .  co m

        FieldDeclarationVisitor fieldDeclarationVisitor = new FieldDeclarationVisitor();
        compilationUnit.accept(fieldDeclarationVisitor);
        if (!fieldDeclarationVisitor.getFieldDeclarations().isEmpty()) {
            fieldDeclarations.addAll(fieldDeclarationVisitor.getFieldDeclarations());
        }

        log.info(qualifiedNamesList);
        log.info(fieldDeclarations);

    }

    for (QualifiedName qualifiedName : qualifiedNamesList) {
        for (FieldDeclaration declaration : fieldDeclarations) {
            if (!fieldDeclarationFieldAccessHashMap.containsKey(declaration)) {
                fieldDeclarationFieldAccessHashMap.put(declaration, new ArrayList<QualifiedName>());
            }
            IBinding binding = ((VariableDeclarationFragment) declaration.fragments().get(0)).resolveBinding();
            log.fatal("Declaration " + declaration);
            log.fatal("Declaration binding "
                    + ((VariableDeclarationFragment) declaration.fragments().get(0)).resolveBinding());
            log.fatal("Qualified Name " + qualifiedName);
            log.fatal("Qualified Name binding " + qualifiedName.resolveBinding());
            if (binding.equals(qualifiedName.resolveBinding())) {
                fieldDeclarationFieldAccessHashMap.get(declaration).add(qualifiedName);
                break;
            }
        }
    }

    for (FieldDeclaration declaration : fieldDeclarationFieldAccessHashMap.keySet()) {
        if (Modifier.isPublic(declaration.getModifiers())) {
            Issue issue = createIssue(declaration);
            issue.getNodes().addAll(fieldDeclarationFieldAccessHashMap.get(declaration));
            log.info(fieldDeclarationFieldAccessHashMap.get(declaration));
        }
    }

    /*for (FieldDeclaration declaration : fieldDeclarations) {
            
            
    IBinding binding = ((VariableDeclarationFragment) declaration.fragments().get(0)).resolveBinding();
            
            
    if (Modifier.isPublic(declaration.getModifiers())) {
        Issue issue = createIssue(declaration);
        issue.getNodes().addAll(fieldDeclarationFieldAccessHashMap.get(declaration));
        log.info(fieldDeclarationFieldAccessHashMap.get(declaration));
    }
    }*/

}

From source file:org.autorefactor.refactoring.ASTHelper.java

License:Open Source License

/**
 * Returns whether the provided binding and expression represent the same local variable.
 *
 * @param binding the binding to analyze
 * @param expr the expression to analyze
 * @return {@code true} if the provided binding and expression represent the same local variable,
 *         {@code false} otherwise/*from  w  ww. j  a  v a2s  .  c o  m*/
 */
public static boolean isSameLocalVariable(IBinding binding, Expression expr) {
    return isLocalVariable(binding) && expr != null && expr.getNodeType() == SIMPLE_NAME
    // no need to use IVariableBinding.isEqualTo(IBinding) since we are looking for a *local* variable
            && binding.equals(((SimpleName) expr).resolveBinding());
}

From source file:org.autorefactor.refactoring.rules.CollectionAddAllRefactoring.java

License:Open Source License

private boolean isSameLocalVariable(Expression expr1, Expression expr2) {
    if (expr1 instanceof SimpleName && expr2 instanceof SimpleName) {
        final IBinding binding1 = ((SimpleName) expr1).resolveBinding();
        final IBinding binding2 = ((SimpleName) expr2).resolveBinding();
        if (binding1 instanceof IVariableBinding) {
            return binding1.equals(binding2);
        }// w  ww  .j av  a  2 s.c  o  m
    }
    return false;
}