// 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())); } } }
/** * @param type * @return */ protected String getNameOfType(final Type type) { String nameOfType = ""; if (type != null) { if (type.isPrimitiveType()) { nameOfType = type.toString(); } else if (type.isParameterizedType()) { nameOfType = getParametrizedType((ParameterizedType) type); } else if (type.isArrayType()) { final ArrayType array = (ArrayType) type; nameOfType = getNameOfType(array.getElementType()) /*+ "[]"*/; } else if (type.isUnionType()) { // TODO: this is used for exceptions till now // So we will just capture the first type that we encounter final UnionType uType = (UnionType) type; final StringBuffer sb = new StringBuffer(); for (final Object unionedType : uType.types()) { sb.append(getNameOfType(((Type) unionedType))); break; // sb.append(" | "); } // sb.delete(sb.length() - 3, sb.length()); nameOfType = sb.toString(); } else if (type.isWildcardType()) { final WildcardType wType = (WildcardType) type; nameOfType = (wType.isUpperBound() ? "? extends " : "? super ") + getNameOfType(wType.getBound()); } else { nameOfType = getFullyQualifiedNameFor(type.toString()); } } return nameOfType; }
/* * @see ASTVisitor#visit(WildcardType) * @since 3.0 */ public boolean visit(WildcardType node) { this.fBuffer.append("?"); // $NON-NLS-1$ Type bound = node.getBound(); if (bound != null) { if (node.isUpperBound()) { this.fBuffer.append(" extends "); // $NON-NLS-1$ } else { this.fBuffer.append(" super "); // $NON-NLS-1$ } bound.accept(this); } return false; }