// Função auxiliar para copiar o tipo da variável // considerando que um tipo nao pode pertencer a outra AST private Type getType(ITypeBinding typeBinding, AST newAST) { if (typeBinding.isPrimitive()) { return newAST.newPrimitiveType(PrimitiveType.toCode(typeBinding.getName())); } else if (typeBinding.isArray()) { return newAST.newArrayType( this.getType(typeBinding.getElementType(), newAST), typeBinding.getDimensions()); } else if (typeBinding.isParameterizedType()) { ParameterizedType pt = newAST.newParameterizedType(this.getType(typeBinding.getTypeDeclaration(), newAST)); for (ITypeBinding itb : typeBinding.getTypeArguments()) { pt.typeArguments().add(this.getType(itb, newAST)); } return pt; } else if (typeBinding.isWildcardType()) { WildcardType wt = newAST.newWildcardType(); wt.setBound( typeBinding.getBound() == null ? null : this.getType(typeBinding.getBound(), newAST), typeBinding.isUpperbound()); return wt; } else { try { return newAST.newSimpleType(newAST.newName(typeBinding.getQualifiedName())); } catch (Exception e) { return newAST.newSimpleType(newAST.newName(typeBinding.getName())); } } }
@Override protected void initialize(ITypeBinding binding) { Assert.isTrue(binding.isWildcardType()); super.initialize(binding); ITypeBinding bound = binding.getBound(); if (bound != null) { fBound = getEnvironment().create(bound); } }
private Type evaluateVariableType( AST ast, ImportRewrite imports, ImportRewriteContext importRewriteContext, IBinding targetContext) { if (fOriginalNode.getParent() instanceof MethodInvocation) { MethodInvocation parent = (MethodInvocation) fOriginalNode.getParent(); if (parent.getExpression() == fOriginalNode) { // _x_.foo() -> guess qualifier type by looking for a type with method 'foo' ITypeBinding[] bindings = ASTResolving.getQualifierGuess( fOriginalNode.getRoot(), parent.getName().getIdentifier(), parent.arguments(), targetContext); if (bindings.length > 0) { for (int i = 0; i < bindings.length; i++) { addLinkedPositionProposal(KEY_TYPE, bindings[i]); } return imports.addImport(bindings[0], ast, importRewriteContext); } } } ITypeBinding binding = ASTResolving.guessBindingForReference(fOriginalNode); if (binding != null) { if (binding.isWildcardType()) { binding = ASTResolving.normalizeWildcardType(binding, isVariableAssigned(), ast); if (binding == null) { // only null binding applies binding = ast.resolveWellKnownType("java.lang.Object"); // $NON-NLS-1$ } } if (isVariableAssigned()) { ITypeBinding[] typeProposals = ASTResolving.getRelaxingTypes(ast, binding); for (int i = 0; i < typeProposals.length; i++) { addLinkedPositionProposal(KEY_TYPE, typeProposals[i]); } } return imports.addImport(binding, ast, importRewriteContext); } // no binding, find type AST node instead -> ABC a= x-> use 'ABC' as is Type type = ASTResolving.guessTypeForReference(ast, fOriginalNode); if (type != null) { return type; } if (fVariableKind == CONST_FIELD) { return ast.newSimpleType(ast.newSimpleName("String")); // $NON-NLS-1$ } return ast.newSimpleType(ast.newSimpleName("Object")); // $NON-NLS-1$ }
public static String bestNameableType(ITypeBinding type) { { String result_ = fqTypeName(type); if (result_ != null) return removeTypeParam(result_); } if (type.isCapture() || type.isWildcardType() || type.isTypeVariable()) { return bestNameableType(type.getErasure()); } // Find an interface or superclass being implemented. // For anonymous class, there can only be one. BUT for // local class, things are super f-ed up... if (type.isAnonymous()) { // any interfaces? if (type.getInterfaces().length > 0) { String result = fqTypeName(type.getInterfaces()[0]); assert (result != null); return removeTypeParam(result); } else { String result = fqTypeName(type.getSuperclass()); assert (result != null); return removeTypeParam(result); } } if (type.isLocal()) { // We can only make sense of this is there is just one // superclass+inferface. if (type.getInterfaces().length == 0) { // EASY! String result = fqTypeName(type.getSuperclass()); assert (result != null); return removeTypeParam(result); } else { // Well, maybe superclass is object? if (type.getSuperclass().getQualifiedName().equals(Object.class.getName())) { String result = fqTypeName(type.getInterfaces()[0]); assert (result != null); return removeTypeParam(result); } else { // Otherwise, return the superclass, I guess, but print a warning: String result = fqTypeName(type.getSuperclass()); assert (result != null); System.err.println("Local class has multiple supertypes..." + type.getBinaryName()); return removeTypeParam(result); } } } return Utilities.impossible(); }
private ClipboardData getClipboardData(ITypeRoot inputElement, int offset, int length) { CompilationUnit astRoot = SharedASTProvider.getAST(inputElement, SharedASTProvider.WAIT_ACTIVE_ONLY, null); if (astRoot == null) { return null; } // do process import if selection spans over import declaration or package List<ImportDeclaration> list = astRoot.imports(); if (!list.isEmpty()) { if (offset < ((ASTNode) list.get(list.size() - 1)).getStartPosition()) { return null; } } else if (astRoot.getPackage() != null) { if (offset < ((ASTNode) astRoot.getPackage()).getStartPosition()) { return null; } } ArrayList<SimpleName> typeImportsRefs = new ArrayList<SimpleName>(); ArrayList<SimpleName> staticImportsRefs = new ArrayList<SimpleName>(); ImportReferencesCollector.collect( astRoot, inputElement.getJavaProject(), new Region(offset, length), typeImportsRefs, staticImportsRefs); if (typeImportsRefs.isEmpty() && staticImportsRefs.isEmpty()) { return null; } HashSet<String> namesToImport = new HashSet<String>(typeImportsRefs.size()); for (int i = 0; i < typeImportsRefs.size(); i++) { Name curr = typeImportsRefs.get(i); IBinding binding = curr.resolveBinding(); if (binding != null && binding.getKind() == IBinding.TYPE) { ITypeBinding typeBinding = (ITypeBinding) binding; if (typeBinding.isArray()) { typeBinding = typeBinding.getElementType(); } if (typeBinding.isTypeVariable() || typeBinding.isCapture() || typeBinding.isWildcardType()) { // can be removed when bug 98473 is fixed continue; } if (typeBinding.isMember() || typeBinding.isTopLevel()) { String name = Bindings.getRawQualifiedName(typeBinding); if (name.length() > 0) { namesToImport.add(name); } } } } HashSet<String> staticsToImport = new HashSet<String>(staticImportsRefs.size()); for (int i = 0; i < staticImportsRefs.size(); i++) { Name curr = staticImportsRefs.get(i); IBinding binding = curr.resolveBinding(); if (binding != null) { StringBuffer buf = new StringBuffer(Bindings.getImportName(binding)); if (binding.getKind() == IBinding.METHOD) { buf.append("()"); // $NON-NLS-1$ } staticsToImport.add(buf.toString()); } } if (namesToImport.isEmpty() && staticsToImport.isEmpty()) { return null; } String[] typeImports = namesToImport.toArray(new String[namesToImport.size()]); String[] staticImports = staticsToImport.toArray(new String[staticsToImport.size()]); return new ClipboardData(inputElement, typeImports, staticImports); }