Example usage for org.eclipse.jdt.core.dom Type resolveBinding

List of usage examples for org.eclipse.jdt.core.dom Type resolveBinding

Introduction

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

Prototype

public final ITypeBinding resolveBinding() 

Source Link

Document

Resolves and returns the binding for this type.

Usage

From source file:br.uff.ic.mergeguider.languageConstructs.MyVariableDeclaration.java

public ITypeBinding resolveTypeBinding() {

    Type type = null;

    if (this.singleVariableDeclaration != null) {
        type = this.singleVariableDeclaration.getType();
    } else if (this.variableDeclaration != null) {
        ASTNode parent = this.variableDeclaration.getParent();

        if (parent instanceof VariableDeclarationStatement) {
            VariableDeclarationStatement variable = (VariableDeclarationStatement) parent;
            type = variable.getType();//from   w w w.  ja va2 s. c o m
        } else if (parent instanceof VariableDeclarationExpression) {
            VariableDeclarationExpression variable = (VariableDeclarationExpression) parent;
            type = variable.getType();
        }
    }

    if (type != null) {
        return type.resolveBinding();
    } else {
        return null;
    }
}

From source file:com.android.ide.eclipse.adt.internal.refactorings.extractstring.ReplaceStringsVisitor.java

License:Open Source License

/**
 * Examines if the StringLiteral is part of an assignment corresponding to the
 * a string variable declaration, e.g. String foo = id.
 *
 * The parent fragment is of syntax "var = expr" or "var[] = expr".
 * We want the type of the variable, which is either held by a
 * VariableDeclarationStatement ("type [fragment]") or by a
 * VariableDeclarationExpression. In either case, the type can be an array
 * but for us all that matters is to know whether the type is an int or
 * a string./*from ww w  . ja  v a2s  .  c o m*/
 */
private boolean examineVariableDeclaration(StringLiteral node) {
    VariableDeclarationFragment fragment = findParentClass(node, VariableDeclarationFragment.class);

    if (fragment != null) {
        ASTNode parent = fragment.getParent();

        Type type = null;
        if (parent instanceof VariableDeclarationStatement) {
            type = ((VariableDeclarationStatement) parent).getType();
        } else if (parent instanceof VariableDeclarationExpression) {
            type = ((VariableDeclarationExpression) parent).getType();
        }

        if (type instanceof SimpleType) {
            return isJavaString(type.resolveBinding());
        }
    }

    return false;
}

From source file:com.android.ide.eclipse.adt.internal.refactorings.extractstring.ReplaceStringsVisitor.java

License:Open Source License

/**
 * Returns true if the given type is or derives from android.content.Context.
 *//*from   w  ww  . ja  v  a 2s .  co m*/
private boolean isAndroidContext(Type type) {
    if (type != null) {
        return isAndroidContext(type.resolveBinding());
    }
    return false;
}

From source file:com.architexa.diagrams.jdt.extractors.MethodParametersExtractor.java

License:Open Source License

@Override
public boolean visit(MethodDeclaration methodDecl) {
    Resource methodDeclRes = bindingToResource(methodDecl.resolveBinding());

    final List<Resource> parametersRes = new LinkedList<Resource>();
    for (Object arg0 : methodDecl.parameters()) {
        SingleVariableDeclaration svd = (SingleVariableDeclaration) arg0;
        parametersRes.add(bindingToResource(svd.getType().resolveBinding()));
    }/*from   w ww.jav  a  2 s.  com*/

    Resource paramaterList = rdfModel.createList(parametersRes.iterator());
    rdfModel.addStatement(methodDeclRes, RJCore.parameter, paramaterList);

    Type retType = methodDecl.getReturnType2();
    if (retType != null) {
        Resource methodDeclTypeRes = bindingToResource(retType.resolveBinding());
        rdfModel.addStatement(methodDeclRes, RJCore.returnType, methodDeclTypeRes);
    }

    rdfModel.addStatement(methodDeclRes, MethodParametersSupport.parameterCachedLabel,
            getParamLabel(methodDecl));

    return true;
}

From source file:com.architexa.diagrams.jdt.extractors.MethodParametersExtractor.java

License:Open Source License

private String getParamLabel(MethodDeclaration methodDecl) {
    String retVal = "(";

    String methodSig = "";
    boolean first = true;
    try {/*w  ww  .ja v a 2s .  c  om*/
        for (Object arg0 : methodDecl.parameters()) {
            SingleVariableDeclaration svd = (SingleVariableDeclaration) arg0;
            if (first)
                first = false;
            else
                methodSig += ",";
            methodSig += svd.getType().resolveBinding().getName();
        }
    } catch (Exception e) {
        methodSig = "...";
        logger.error("Unexpected Exception", e);
    }

    retVal += methodSig;
    retVal += "): ";
    try {
        Type retType = methodDecl.getReturnType2();
        if (retType != null)
            retVal += retType.resolveBinding().getName();
        else
            retVal += "void";
    } catch (Exception e) {
        retVal += "void";
        logger.error("Unexpected Exception", e);
    }
    return retVal;
}

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

License:Open Source License

@Override
public boolean visit(org.eclipse.jdt.core.dom.CatchClause node) {
    SimpleIdentifier exceptionParameter = translateSimpleName(node.getException().getName());
    Block block = translate(node.getBody());
    // "catch (e) {}" or "on Type catch (e) {}"
    Type javaExceptionType = node.getException().getType();
    ITypeBinding javaExceptionBinding = javaExceptionType.resolveBinding();
    if (JavaUtils.isTypeNamed(javaExceptionBinding, "java.lang.Exception")) {
        return done(catchClause(null, exceptionParameter, null, block));
    } else {/*w w  w .java  2  s . c  o  m*/
        TypeName exceptionType = translate(javaExceptionType);
        return done(catchClause(exceptionType, exceptionParameter, null, block));
    }
}

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

License:Open Source License

@Override
public boolean visit(org.eclipse.jdt.core.dom.ParameterizedType node) {
    List<TypeName> typeArguments;
    {// w w  w.  j  av a2s  .  c om
        List<?> javaTypeArguments = node.typeArguments();
        boolean hasMethodTypeVariable = false;
        for (Object _javaTypeArgument : javaTypeArguments) {
            org.eclipse.jdt.core.dom.Type javaTypeArgument = (org.eclipse.jdt.core.dom.Type) _javaTypeArgument;
            ITypeBinding binding = javaTypeArgument.resolveBinding();
            if (binding != null && binding.isTypeVariable() && binding.getDeclaringMethod() != null) {
                hasMethodTypeVariable = true;
                break;
            }
        }
        if (hasMethodTypeVariable) {
            typeArguments = null;
        } else {
            typeArguments = translateTypeNames(javaTypeArguments);
        }
    }
    ITypeBinding binding = node.resolveBinding();
    TypeName typeName = typeName(((TypeName) translate(node.getType())).getName(), typeArguments);
    context.putNodeBinding(typeName, binding);
    context.putNodeTypeBinding(typeName, binding);
    return done(typeName);
}

From source file:com.google.devtools.j2cpp.translate.JavaToIOSTypeConverter.java

License:Open Source License

@Override
public boolean visit(MethodDeclaration node) {
    IMethodBinding binding = Types.getMethodBinding(node);
    ITypeBinding returnBinding = binding.getReturnType();
    ITypeBinding newBinding = Types.mapType(returnBinding);
    if (returnBinding != newBinding) {
        node.setReturnType2(Types.makeType(newBinding));
    }//www .  ja v a 2s. c  om

    @SuppressWarnings("unchecked")
    List<SingleVariableDeclaration> parameters = node.parameters();
    for (Iterator<SingleVariableDeclaration> iterator = parameters.iterator(); iterator.hasNext();) {
        SingleVariableDeclaration parameter = iterator.next();
        Type type = parameter.getType();
        ITypeBinding varBinding = type.resolveBinding();
        if (varBinding != null) { // true for primitive types
            newBinding = Types.mapType(varBinding);
            if (varBinding != newBinding) {
                parameter.setType(Types.makeType(newBinding));
            }
        }
    }
    return super.visit(node);
}

From source file:com.google.devtools.j2cpp.translate.Rewriter.java

License:Open Source License

/**
 * Add a static write accessor method for a specified variable.
 *///from w w  w .  ja  v a 2 s . c om
private MethodDeclaration makeStaticWriter(VariableDeclarationFragment var, String paramName, Type type,
        int modifiers) {
    AST ast = var.getAST();
    String varName = var.getName().getIdentifier();
    IVariableBinding varBinding = Types.getVariableBinding(var);

    Type returnType = ast.newPrimitiveType(PrimitiveType.VOID);
    Types.addBinding(returnType, ast.resolveWellKnownType("void"));
    String methodName = "set" + NameTable.capitalize(varName);
    MethodDeclaration accessor = createBlankAccessor(var, methodName, modifiers, returnType);
    GeneratedMethodBinding binding = new GeneratedMethodBinding(accessor, varBinding.getDeclaringClass(),
            false);
    Types.addBinding(accessor, binding);
    Types.addBinding(accessor.getName(), binding);

    SingleVariableDeclaration param = ast.newSingleVariableDeclaration();
    param.setName(ast.newSimpleName(paramName));
    Type paramType = NodeCopier.copySubtree(ast, type);
    param.setType(paramType);
    Types.addBinding(paramType, type.resolveBinding());
    @SuppressWarnings("unchecked")
    List<SingleVariableDeclaration> parameters = accessor.parameters(); // safe by definition
    GeneratedVariableBinding paramBinding = new GeneratedVariableBinding(paramName, 0, type.resolveBinding(),
            false, true, varBinding.getDeclaringClass(), binding);
    Types.addBinding(param, paramBinding);
    Types.addBinding(param.getName(), paramBinding);
    parameters.add(param);
    binding.addParameter(paramBinding);

    Assignment assign = ast.newAssignment();
    SimpleName sn = ast.newSimpleName(NameTable.getName(varBinding));
    assign.setLeftHandSide(sn);
    Types.addBinding(sn, varBinding);
    assign.setRightHandSide(NodeCopier.copySubtree(ast, param.getName()));
    Types.addBinding(assign, varBinding.getType());
    ExpressionStatement assignStmt = ast.newExpressionStatement(assign);

    @SuppressWarnings("unchecked")
    List<Statement> stmts = accessor.getBody().statements(); // safe by definition
    stmts.add(assignStmt);
    Symbols.scanAST(accessor);
    return accessor;
}

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

License:Open Source License

protected String getTypeName(Type type) throws ClassNotFoundException {
    assert type != null && !(type instanceof PrimitiveType);
    String fullName;//from  ww w.j  a  v  a  2  s . co m
    ITypeBinding binding = type.resolveBinding();
    if (binding != null) {
        binding = Types.mapType(binding);
        fullName = NameTable.getFullName(binding);
    } else if (type instanceof SimpleType) {
        fullName = ((SimpleType) type).getName().getFullyQualifiedName();
    } else if (type instanceof QualifiedType) {
        fullName = ((QualifiedType) type).getName().getFullyQualifiedName();
    } else {
        throw new ClassNotFoundException(type.toString());
    }
    return fullName;
}