Example usage for org.eclipse.jdt.core.dom IVariableBinding getKey

List of usage examples for org.eclipse.jdt.core.dom IVariableBinding getKey

Introduction

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

Prototype

public String getKey();

Source Link

Document

Returns the key for this binding.

Usage

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

License:Open Source License

public FieldReference getFieldRef(IVariableBinding field) {
    if (!fFieldMap.containsKey(field.getKey())) {
        // create one
        ITypeBinding targetType = field.getDeclaringClass();
        TypeReference targetTypeRef = TypeReference.findOrCreate(fClassLoaderRef, typeToTypeID(targetType));
        ITypeBinding fieldType = field.getType();
        TypeReference fieldTypeRef = TypeReference.findOrCreate(fClassLoaderRef, typeToTypeID(fieldType));
        Atom fieldName = Atom.findOrCreateUnicodeAtom(field.getName());
        FieldReference ref = FieldReference.findOrCreate(targetTypeRef, fieldName, fieldTypeRef);

        fFieldMap.put(field.getKey(), ref);
        return ref;
    }//from   w ww . jav a  2s  .co  m
    return fFieldMap.get(field.getKey());
}

From source file:de.ovgu.cide.export.physical.internal.LocalVariableHelper.java

License:Open Source License

public static void cacheCompilationUnit(CompilationUnit compUnit) {
    clearCache();/*from   www .ja va2  s  . co  m*/

    final HashMap<String, VariableDeclaration> knownDeclarations = new HashMap<String, VariableDeclaration>();

    // find declarations
    compUnit.accept(new ASTVisitor() {
        @Override
        public boolean visit(VariableDeclarationFragment node) {
            IVariableBinding binding = node.resolveBinding();
            if (binding != null)
                knownDeclarations.put(binding.getKey(), node);
            return super.visit(node);
        }

        public boolean visit(SingleVariableDeclaration node) {
            IVariableBinding binding = node.resolveBinding();
            if (binding != null)
                knownDeclarations.put(binding.getKey(), node);
            return super.visit(node);
        }
    });

    // find access and create cache
    compUnit.accept(new ASTVisitor() {
        @Override
        public boolean visit(QualifiedName node) {
            return visitName(node);
        }

        @Override
        public boolean visit(SimpleName node) {
            return visitName(node);
        }

        private boolean visitName(Name node) {
            IBinding binding = node.resolveBinding();
            if (binding != null && binding instanceof IVariableBinding) {
                VariableDeclaration declaration = knownDeclarations.get(binding.getKey());
                if (declaration != null) {
                    localVariableAccess.put(node, declaration);
                    if (formals.get(declaration) == null) {
                        Formal formal = createFormal(declaration);
                        if (formal != null) {
                            formals.put(declaration, formal);
                            formalsReverse.put(formal, declaration);
                        }
                    }
                }
            }
            return true;
        }
    });
    initialized = true;
}

From source file:de.ovgu.cide.typing.jdt.BindingProjectColorCache.java

License:Open Source License

public Set<IFeature> getColors(IVariableBinding field) {
    return getColors(field.getKey());
}

From source file:edu.cmu.cs.crystal.annotations.AnnotationDatabase.java

License:Open Source License

/**
 * This method will return the list of annotations associated with the
 * given variable. //from   w  w w.  j a v  a  2  s  . c  om
 */
public List<ICrystalAnnotation> getAnnosForVariable(IVariableBinding binding) {
    while (binding != binding.getVariableDeclaration())
        binding = binding.getVariableDeclaration();
    String name = binding.getKey();

    List<ICrystalAnnotation> result = fields.get(name);
    if (result == null) {
        result = createAnnotations(binding.getAnnotations());
        fields.put(name, result);
    }
    return result;
}

From source file:edu.cmu.cs.crystal.annotations.AnnotationDatabase.java

License:Open Source License

public void addAnnotationToField(ICrystalAnnotation anno, FieldDeclaration field) {
    IVariableBinding binding;
    String name;/*from   www.  java  2 s  .  c o m*/
    List<ICrystalAnnotation> annoList;

    if (field.fragments().isEmpty())
        return;

    binding = ((VariableDeclarationFragment) field.fragments().get(0)).resolveBinding();
    name = binding.getKey();
    annoList = fields.get(name);
    if (annoList == null) {
        annoList = new ArrayList<ICrystalAnnotation>();
        fields.put(name, annoList);
    }
    annoList.add(anno);
}

From source file:fr.labri.harmony.rta.junit.jdt.JDTVisitorRTA.java

License:Open Source License

public boolean visit(FieldAccess a) {
    IVariableBinding b = a.resolveFieldBinding();

    if (b != null && currentJavaMethod != null) {
        if (b.getDeclaringClass() != null) {
            if (!(a.getParent() instanceof Assignment)) {

                String fieldId = getVariableBindingId(b);
                //System.err.println("Add "+fieldId);
                currentJavaMethod.getCallsSite().add(fieldId);
            } else {
                if (((Assignment) a.getParent()).getLeftHandSide() instanceof FieldAccess) {
                    if (((FieldAccess) ((Assignment) a.getParent()).getLeftHandSide())
                            .resolveFieldBinding() != null) {
                        if (!((FieldAccess) ((Assignment) a.getParent()).getLeftHandSide())
                                .resolveFieldBinding().getKey().equals(b.getKey())) {

                            String fieldId = getVariableBindingId(b);
                            //System.err.println("Add "+fieldId);
                            currentJavaMethod.getCallsSite().add(fieldId);
                        }/*from   w  w w  . j av a2s. com*/
                    }
                }
            }
        }
    }

    return true;
}

From source file:fr.labri.harmony.rta.junit.jdt.JDTVisitorRTA.java

License:Open Source License

public boolean visit(SuperFieldAccess a) {
    IVariableBinding b = a.resolveFieldBinding();

    if (b != null && currentJavaMethod != null) {
        if (b.getDeclaringClass() != null) {
            if (!(a.getParent() instanceof Assignment)) {

                String fieldId = getVariableBindingId(b);
                //System.err.println("Add "+fieldId);
                currentJavaMethod.getCallsSite().add(fieldId);
            } else {
                if (((Assignment) a.getParent()).getLeftHandSide() instanceof SuperFieldAccess) {
                    if (!((SuperFieldAccess) ((Assignment) a.getParent()).getLeftHandSide())
                            .resolveFieldBinding().getKey().equals(b.getKey())) {

                        String fieldId = getVariableBindingId(b);
                        //System.err.println("Add "+fieldId);
                        currentJavaMethod.getCallsSite().add(fieldId);
                    }//  w  ww  .  ja  va 2 s.c o m
                }
            }
        }
    }

    return true;
}

From source file:fr.labri.harmony.rta.junit.jdt.JDTVisitorRTA.java

License:Open Source License

public boolean visit(SimpleName n) {
    IBinding ib = n.resolveBinding();/*  ww w  . j  ava 2  s  .  c o m*/
    if (ib != null) {
        if (ib.getKind() == IBinding.VARIABLE) {

            IVariableBinding b = (IVariableBinding) n.resolveBinding();
            if (b.isField()) {

                if (b != null && currentJavaMethod != null) {
                    if (b.getDeclaringClass() != null) {
                        if (!(n.getParent() instanceof Assignment)) {
                            String fieldId = getVariableBindingId(b);
                            //System.err.println("Add "+fieldId);
                            currentJavaMethod.getCallsSite().add(fieldId);
                        } else {
                            if (n.getParent().getParent() instanceof Assignment) {
                                if (((Assignment) n.getParent().getParent())
                                        .getLeftHandSide() instanceof SimpleName)
                                    if (!(((SimpleName) ((Assignment) n.getParent().getParent())
                                            .getLeftHandSide())).resolveBinding().getKey().equals(b.getKey())) {
                                        String fieldId = getVariableBindingId(b);
                                        //System.err.println("Add "+fieldId);
                                        currentJavaMethod.getCallsSite().add(fieldId);
                                    }
                            }
                        }
                    }
                }
            }

        }
    }
    return true;
}

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

License:Open Source License

private IList getIds(IVariableBinding vb, Initializer possibleParent) {
    String key = vb.getKey();
    IList l = idStore.get(key);/*from  w  ww.j  a  v a2  s  .  c o  m*/
    if (l == null) {
        l = importVariableBinding(vb, possibleParent);
        idStore.put(key, l);
    }
    return l;
}

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

License:Open Source License

private void importBindingInfo(ASTNode n) {

    // type bindings
    ITypeBinding tb = null;/*from   w  ww.  jav  a2s.  c  o  m*/

    if (n instanceof org.eclipse.jdt.core.dom.Type) {
        tb = ((org.eclipse.jdt.core.dom.Type) n).resolveBinding();
    } else if (n instanceof AbstractTypeDeclaration) {
        tb = ((AbstractTypeDeclaration) n).resolveBinding();
    } else if (n instanceof AnonymousClassDeclaration) {
        tb = ((AnonymousClassDeclaration) n).resolveBinding();
    } else if (n instanceof Expression) {
        tb = ((Expression) n).resolveTypeBinding();
    } else if (n instanceof TypeDeclarationStatement) {
        tb = ((TypeDeclarationStatement) n).resolveBinding();
    } else if (n instanceof TypeParameter) {
        tb = ((TypeParameter) n).resolveBinding();
    } else if (n instanceof EnumDeclaration) {
        tb = ((EnumDeclaration) n).resolveBinding();
    } else if (n instanceof AnnotationTypeDeclaration) {
        tb = ((AnnotationTypeDeclaration) n).resolveBinding();
    }

    if (tb != null) {
        Initializer possibleParent = null;
        try {
            ASTNode scope = scopeStack.peek();
            if (scope instanceof Initializer) {
                possibleParent = (Initializer) scope;
            }
        } catch (EmptyStackException e) {
            // ignore
        }
        if (fillOldStyleUsage) {
            addBinding(typeBindings, n, bindingCache.getEntity(tb, possibleParent));
        }
    }

    // method and constructor bindings
    IMethodBinding mb = null;
    IMethodBinding cb = null;

    if (n instanceof ClassInstanceCreation) {
        cb = ((ClassInstanceCreation) n).resolveConstructorBinding();
    } else if (n instanceof ConstructorInvocation) {
        cb = ((ConstructorInvocation) n).resolveConstructorBinding();
    } else if (n instanceof EnumConstantDeclaration) {
        cb = ((EnumConstantDeclaration) n).resolveConstructorBinding();
    } else if (n instanceof MethodDeclaration) {
        mb = ((MethodDeclaration) n).resolveBinding();
    } else if (n instanceof MethodInvocation) {
        mb = ((MethodInvocation) n).resolveMethodBinding();
        // if this is a method invocation on an AnonymousClassDeclaration,
        // call importBindingInfo first to get declaration Entity.
        Expression exp = ((MethodInvocation) n).getExpression();
        if (exp instanceof ClassInstanceCreation) {
            AnonymousClassDeclaration acdc = ((ClassInstanceCreation) exp).getAnonymousClassDeclaration();
            if (acdc != null) {
                importBindingInfo(acdc);
            }
        }
    } else if (n instanceof SuperConstructorInvocation) {
        cb = ((SuperConstructorInvocation) n).resolveConstructorBinding();
    } else if (n instanceof SuperMethodInvocation) {
        mb = ((SuperMethodInvocation) n).resolveMethodBinding();
    }

    if (mb != null) {
        if (fillOldStyleUsage) {
            addBinding(methodBindings, n, bindingCache.getEntity(mb));
        }
        if (n instanceof MethodDeclaration) {
            addBinding(methodDecls, n, bindingCache.getEntity(mb));
            addMethodBody((MethodDeclaration) n, mb);
        }
        if (n instanceof MethodInvocation) {
            MethodInvocation mi = (MethodInvocation) n;
            int mods = mi.resolveMethodBinding().getMethodDeclaration().getModifiers();
            List<IValue> modsForN = bindingCache.getModifiers(mods);
            if (mi.resolveMethodBinding().getMethodDeclaration().isDeprecated())
                modsForN.add(BindingConverter.deprecatedModifier);
            for (IValue modifier : modsForN)
                modifiers.insert(VF.tuple(bindingCache.getEntity(mb), modifier));
        }
    }

    if (cb != null && fillOldStyleUsage) {
        addBinding(constructorBindings, n, bindingCache.getEntity(cb));
    }

    // field and variable bindings
    IVariableBinding vb = null;
    IVariableBinding fb = null;

    if (n instanceof EnumConstantDeclaration) {
        fb = ((EnumConstantDeclaration) n).resolveVariable();
    } else if (n instanceof FieldDeclaration) {
        FieldDeclaration fd = (FieldDeclaration) n;
        for (Object vdfo : fd.fragments()) {
            VariableDeclarationFragment vdf = (VariableDeclarationFragment) vdfo;
            addBinding(fieldDecls, n, bindingCache.getEntity(vdf.resolveBinding()));
        }
    } else if (n instanceof FieldAccess) {
        FieldAccess fa = (FieldAccess) n;
        fb = fa.resolveFieldBinding();

        // check for 'length' access of array object
        Expression exp = fa.getExpression();
        if (exp.resolveTypeBinding().isArray() && fb.getName().equals("length")) {
            // put arrayLengthField in idStore, so it doesn't have to call
            // importVariableBinding()
            // (which cannot distinguish between 'length' access and a local
            // var inside an initializer).
            // don't include type of exp, b/c we can't do the same further
            // down
            bindingCache.put(fb.getKey(), BindingConverter.arrayLengthField);
        }

        // if the field is accessed on an AnonymousClassDeclaration,
        // call importBindingInfo first to get declaration Entity.
        if (exp instanceof ClassInstanceCreation) {
            AnonymousClassDeclaration acdc = ((ClassInstanceCreation) exp).getAnonymousClassDeclaration();
            if (acdc != null) {
                importBindingInfo(acdc);
            }
        }
    } else if (n instanceof SuperFieldAccess) {
        fb = ((SuperFieldAccess) n).resolveFieldBinding();
    } else if (n instanceof VariableDeclaration) {
        vb = ((VariableDeclaration) n).resolveBinding();
    } else if (n instanceof SimpleName) {
        // local variable, parameter or field.
        SimpleName name = (SimpleName) n;
        IBinding b = name.resolveBinding();
        if (b instanceof IVariableBinding) {
            vb = (IVariableBinding) b;

            if (vb.getDeclaringClass() != null) {
                // field
                fb = vb;
                vb = null;
            } else {
                // The field 'length' of an array type has no declaring
                // class
                // Let's try to distinguish between 'length' access and a
                // local variable/parameter
                ASTNode parent = n.getParent();
                if (vb.getName().equals("length") && parent != null && parent instanceof QualifiedName
                        && vb.toString().equals("public final int length")) {
                    // assume 'length' access of array object (local
                    // variables can't be public)
                    // put arrayLengthField in idStore, so it doesn't have
                    // to call importVariableBinding()
                    // (which cannot distinguish between 'length' access and
                    // a local var inside an initializer).
                    // we can't get the array type of the object of which
                    // the field was accessed
                    bindingCache.put(vb.getKey(), BindingConverter.arrayLengthField);
                }
            }
        }
    }

    if (fb != null || vb != null) {
        Initializer possibleParent = null;
        try {
            ASTNode scope = scopeStack.peek();
            if (scope instanceof Initializer) {
                possibleParent = (Initializer) scope;
            }
        } catch (EmptyStackException e) {
            // ignore
        }

        if (fb != null && fillOldStyleUsage) {
            addBinding(fieldBindings, n, bindingCache.getEntity(fb, possibleParent));
        }
        if (vb != null) {
            addBinding(variableBindings, n, bindingCache.getEntity(vb, possibleParent));
        }
    }

    // package bindings
    IPackageBinding pb = null;
    if (n instanceof PackageDeclaration) {
        pb = ((PackageDeclaration) n).resolveBinding();
    }

    if (pb != null) {
        addBinding(packageBindings, n, bindingCache.getEntity(pb));
    }
}