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

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

Introduction

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

Prototype

public String getKey();

Source Link

Document

Returns the key for this binding.

Usage

From source file:com.github.parzonka.ccms.sorter.callgraph.TopLevelASTVisitor.java

License:Open Source License

/**
 * Returns true if this typeBinding is declared in the topLevelType.
 *
 * @param typeBinding/* w ww .j  a  v  a 2 s  . c  o m*/
 * @return
 */
protected boolean isInTopLevelType(ITypeBinding typeBinding) {
    return typeBinding != null && typeBinding.getKey().equals(this.topLevelType.getKey());
}

From source file:com.google.dart.java2dart.SyntaxTranslator.java

License:Open Source License

@Override
public boolean visit(org.eclipse.jdt.core.dom.ClassInstanceCreation node) {
    IMethodBinding binding = node.resolveConstructorBinding();
    String signature = JavaUtils.getJdtSignature(binding);
    TypeName typeNameNode = (TypeName) translate(node.getType());
    final List<Expression> arguments = translateArguments(binding, node.arguments());
    final ClassDeclaration innerClass;
    {// ww w . j  a va2s  .c o m
        AnonymousClassDeclaration anoDeclaration = node.getAnonymousClassDeclaration();
        if (anoDeclaration != null) {
            ITypeBinding superclass = anoDeclaration.resolveBinding().getSuperclass();
            signature = superclass.getKey() + StringUtils.substringAfter(signature, ";");
            String name = typeNameNode.getName().getName().replace('.', '_');
            name = name + "_" + context.generateTechnicalAnonymousClassIndex();
            innerClass = declareInnerClass(binding, anoDeclaration, name, ArrayUtils.EMPTY_STRING_ARRAY);
            typeNameNode = typeName(name);
            // prepare enclosing type
            final ITypeBinding enclosingTypeBinding = getEnclosingTypeBinding(node);
            final SimpleIdentifier enclosingTypeRef;
            final AtomicBoolean addEnclosingTypeRef = new AtomicBoolean();
            {
                if (enclosingTypeBinding != null) {
                    enclosingTypeRef = identifier(enclosingTypeBinding.getName() + "_this");
                    // add enclosing class references
                    innerClass.accept(new RecursiveASTVisitor<Void>() {
                        @Override
                        public Void visitMethodInvocation(MethodInvocation node) {
                            if (node.getTarget() == null) {
                                IMethodBinding methodBinding = (IMethodBinding) context.getNodeBinding(node);
                                if (methodBinding != null
                                        && methodBinding.getDeclaringClass() == enclosingTypeBinding) {
                                    addEnclosingTypeRef.set(true);
                                    node.setTarget(enclosingTypeRef);
                                }
                            }
                            return super.visitMethodInvocation(node);
                        }

                        @Override
                        public Void visitSimpleIdentifier(SimpleIdentifier node) {
                            if (!(node.getParent() instanceof PropertyAccess)
                                    && !(node.getParent() instanceof PrefixedIdentifier)) {
                                Object binding = context.getNodeBinding(node);
                                if (binding instanceof IVariableBinding) {
                                    IVariableBinding variableBinding = (IVariableBinding) binding;
                                    if (variableBinding.isField()
                                            && variableBinding.getDeclaringClass() == enclosingTypeBinding) {
                                        addEnclosingTypeRef.set(true);
                                        replaceNode(node.getParent(), node,
                                                propertyAccess(enclosingTypeRef, node));
                                    }
                                }
                            }
                            return super.visitSimpleIdentifier(node);
                        }
                    });
                } else {
                    enclosingTypeRef = null;
                }
            }
            // declare referenced final variables XXX
            final String finalName = name;
            anoDeclaration.accept(new ASTVisitor() {
                final Set<org.eclipse.jdt.core.dom.IVariableBinding> addedParameters = Sets.newHashSet();
                final List<FormalParameter> constructorParameters = Lists.newArrayList();
                int index;

                @Override
                public void endVisit(AnonymousClassDeclaration node) {
                    if (!constructorParameters.isEmpty()) {
                        // add parameters to the existing "inner" constructor XXX
                        for (ClassMember classMember : innerClass.getMembers()) {
                            if (classMember instanceof ConstructorDeclaration) {
                                ConstructorDeclaration innerConstructor = (ConstructorDeclaration) classMember;
                                innerConstructor.getParameters().getParameters().addAll(constructorParameters);
                                return;
                            }
                        }
                        // create new "inner" constructor
                        innerClass.getMembers().add(index, constructorDeclaration(identifier(finalName), null,
                                formalParameterList(constructorParameters), null));
                    }
                    super.endVisit(node);
                }

                @Override
                public void endVisit(SimpleName node) {
                    IBinding nameBinding = node.resolveBinding();
                    if (nameBinding instanceof org.eclipse.jdt.core.dom.IVariableBinding) {
                        org.eclipse.jdt.core.dom.IVariableBinding variableBinding = (org.eclipse.jdt.core.dom.IVariableBinding) nameBinding;
                        org.eclipse.jdt.core.dom.MethodDeclaration enclosingMethod = getEnclosingMethod(node);
                        if (!variableBinding.isField() && enclosingMethod != null
                                && variableBinding.getDeclaringMethod() != enclosingMethod.resolveBinding()
                                && addedParameters.add(variableBinding)) {
                            TypeName parameterTypeName = translateTypeName(variableBinding.getType());
                            String parameterName = variableBinding.getName();
                            SimpleIdentifier parameterNameNode = identifier(parameterName);
                            innerClass.getMembers().add(index++, fieldDeclaration(parameterTypeName,
                                    variableDeclaration(parameterNameNode)));
                            constructorParameters.add(fieldFormalParameter(null, null, parameterNameNode));
                            arguments.add(parameterNameNode);
                            context.putReference(parameterNameNode, variableBinding, null);
                        }
                    }
                    super.endVisit(node);
                }

                @Override
                public boolean visit(AnonymousClassDeclaration node) {
                    if (addEnclosingTypeRef.get()) {
                        TypeName parameterTypeName = translateTypeName(enclosingTypeBinding);
                        innerClass.getMembers().add(index++, fieldDeclaration(false, Keyword.FINAL,
                                parameterTypeName, variableDeclaration(enclosingTypeRef)));
                        constructorParameters.add(fieldFormalParameter(null, null, enclosingTypeRef));
                        arguments.add(thisExpression());
                    }
                    return super.visit(node);
                }
            });
        } else {
            innerClass = null;
        }
    }
    InstanceCreationExpression creation = instanceCreationExpression(Keyword.NEW, typeNameNode, null,
            arguments);
    context.putNodeBinding(creation, binding);
    context.putAnonymousDeclaration(creation, innerClass);
    context.getConstructorDescription(binding).instanceCreations.add(creation);
    return done(creation);
}

From source file:com.google.devtools.cyclefinder.ReferenceGraph.java

License:Apache License

private void addSubtypeEdges() {
    SetMultimap<String, String> subtypes = HashMultimap.create();
    for (ITypeBinding type : allTypes.values()) {
        collectSubtypes(type.getKey(), type, subtypes);
    }/*from   w w  w  .  j  av  a2  s.c o m*/
    for (String type : allTypes.keySet()) {
        for (Edge e : ImmutableList.copyOf(edges.get(type))) {
            Set<String> targetSubtypes = subtypes.get(e.getTarget().getKey());
            Set<String> whitelistKeys = Sets.newHashSet();
            IVariableBinding field = e.getField();
            for (String subtype : targetSubtypes) {
                ITypeBinding subtypeBinding = allTypes.get(subtype);
                if ((field != null && field.isField()
                        && whitelist.isWhitelistedTypeForField(field, subtypeBinding))
                        || whitelist.containsType(subtypeBinding)) {
                    whitelistKeys.add(subtype);
                    whitelistKeys.addAll(subtypes.get(subtype));
                }
            }
            for (String subtype : Sets.difference(targetSubtypes, whitelistKeys)) {
                addEdge(Edge.newSubtypeEdge(e, allTypes.get(subtype)));
            }
        }
    }
}

From source file:com.google.devtools.cyclefinder.ReferenceGraph.java

License:Apache License

private void collectSubtypes(String originalType, ITypeBinding type, Multimap<String, String> subtypes) {
    for (ITypeBinding interfaze : type.getInterfaces()) {
        subtypes.put(interfaze.getKey(), originalType);
        collectSubtypes(originalType, interfaze, subtypes);
    }/*ww  w . ja va 2 s .c om*/
    if (type.getSuperclass() != null) {
        subtypes.put(type.getSuperclass().getKey(), originalType);
        collectSubtypes(originalType, type.getSuperclass(), subtypes);
    }
}

From source file:com.google.devtools.cyclefinder.ReferenceGraph.java

License:Apache License

private void addSuperclassEdges() {
    for (ITypeBinding type : allTypes.values()) {
        ITypeBinding superclass = type.getSuperclass();
        while (superclass != null) {
            for (Edge e : edges.get(superclass.getKey())) {
                addEdge(Edge.newSuperclassEdge(e, type, superclass));
            }/*from  w w  w . j a  v a2 s.  co  m*/
            superclass = superclass.getSuperclass();
        }
    }
}

From source file:com.google.devtools.cyclefinder.TypeCollector.java

License:Apache License

public static String getNameForType(ITypeBinding type) {
    String name = renamings.get(type);
    if (name != null) {
        return name;
    }//from   w  w w. j  a  v a  2s.co m
    name = type.getName();
    if (!Strings.isNullOrEmpty(name)) {
        return name;
    }
    return type.getKey();
}

From source file:com.google.devtools.cyclefinder.TypeCollector.java

License:Apache License

public void visitType(ITypeBinding type) {
    if (type == null) {
        return;/*from www .j  a  va  2 s .  c  o m*/
    }
    type = getElementType(type);
    if (allTypes.containsKey(type.getKey()) || type.isPrimitive()) {
        return;
    }
    if (hasNestedWildcard(type)) {
        // Avoid infinite recursion caused by nested wildcard types.
        return;
    }
    allTypes.put(type.getKey(), type);
    visitType(type.getSuperclass());
    visitType(type.getDeclaringClass());
    for (IVariableBinding field : type.getDeclaredFields()) {
        ITypeBinding fieldType = field.getType();
        for (ITypeBinding typeParam : fieldType.getTypeArguments()) {
            visitType(typeParam);
        }
        visitType(fieldType);
    }
    for (ITypeBinding interfaze : type.getInterfaces()) {
        visitType(interfaze);
    }
}

From source file:com.google.devtools.j2cpp.gen.CppStatementGenerator.java

License:Open Source License

@Override
public boolean visit(NumberLiteral node) {
    String token = node.getToken();
    ITypeBinding binding = Types.getTypeBinding(node);
    assert binding.isPrimitive();
    char kind = binding.getKey().charAt(0); // Primitive types have single-character keys.

    // Convert floating point literals to C format.  No checking is
    // necessary, since the format was verified by the parser.
    if (kind == 'D' || kind == 'F') {
        if (token.matches(FLOATING_POINT_SUFFIX_REGEX)) {
            token = token.substring(0, token.length() - 1); // strip suffix
        }/*from w ww  .  j  av a  2 s .  c o  m*/
        if (token.matches(HEX_LITERAL_REGEX)) {
            token = Double.toString(Double.parseDouble(token));
        } else if (!token.matches(EXPONENTIAL_FLOATING_POINT_REGEX)) {
            if (token.indexOf('.') == -1) {
                token += ".0"; // C requires a fractional part, except in exponential form.
            }
        }
        if (kind == 'F') {
            token += 'f';
        }
    } else if (kind == 'J') {
        if (token.equals("0x8000000000000000L") || token.equals("-9223372036854775808L")) {
            // Convert min long literal to an expression
            token = "-0x7fffffffffffffffLL - 1";
        } else {
            // Convert Java long literals to long long for Obj-C
            if (token.startsWith("0x")) {
                buffer.append("(long long) "); // Ensure constant is treated as signed.
            }
            int pos = token.length() - 1;
            int numLs = 0;
            while (pos > 0 && token.charAt(pos) == 'L') {
                numLs++;
                pos--;
            }

            if (numLs == 1) {
                token += 'L';
            }
        }
    } else if (kind == 'I') {
        if (token.startsWith("0x")) {
            buffer.append("(int) "); // Ensure constant is treated as signed.
        }
        if (token.equals("0x80000000") || token.equals("-2147483648")) {
            // Convert min int literal to an expression
            token = "-0x7fffffff - 1";
        }
    }
    buffer.append(token);
    return false;
}

From source file:com.google.devtools.j2objc.gen.LiteralGenerator.java

License:Apache License

public static String fixNumberToken(String token, ITypeBinding type) {
    token = token.replace("_", ""); // Remove any embedded underscores.
    assert type.isPrimitive();
    char kind = type.getKey().charAt(0); // Primitive types have single-character keys.

    switch (kind) {
    case 'D':
        return fixDoubleToken(token);
    case 'F':
        return fixFloatToken(token);
    case 'J':
        return fixLongToken(token);
    case 'I':
        return fixIntToken(token);
    default://  w  w  w .j  a v  a 2s  .co  m
        return token;
    }
}

From source file:com.google.devtools.j2objc.gen.ObjectiveCSourceFileGenerator.java

License:Open Source License

protected AbstractTypeDeclaration getLocalTypeNode(ITypeBinding type) {
    return typesByKey.get(type.getKey());
}