/** * Returns a method name from the {@link android.content.res.Resources} class for array resource * binding, null if the element type is not supported. */ private static FieldResourceBinding.Type getArrayResourceMethodName(Element element) { TypeMirror typeMirror = element.asType(); if (TYPED_ARRAY_TYPE.equals(typeMirror.toString())) { return FieldResourceBinding.Type.TYPED_ARRAY; } if (TypeKind.ARRAY.equals(typeMirror.getKind())) { ArrayType arrayType = (ArrayType) typeMirror; String componentType = arrayType.getComponentType().toString(); if (STRING_TYPE.equals(componentType)) { return FieldResourceBinding.Type.STRING_ARRAY; } else if ("int".equals(componentType)) { return FieldResourceBinding.Type.INT_ARRAY; } else if ("java.lang.CharSequence".equals(componentType)) { return FieldResourceBinding.Type.TEXT_ARRAY; } } return null; }
private TypeDeclaration typeToTypeDeclaration(TypeMirror tMirror){ int dimension = 0; TypeMirror aType = tMirror; ITypeDeclaration[] generics = null; if(tMirror.getKind() == TypeKind.ARRAY){ for(;aType.getKind() == TypeKind.ARRAY; aType = ((ArrayType)tMirror).getComponentType()) { dimension++; } } if(aType.getKind() == TypeKind.DECLARED){ DeclaredType dType = (DeclaredType) aType; List<? extends TypeMirror> parameters = dType.getTypeArguments(); if( parameters!=null && parameters.size()>0){ generics = new ITypeDeclaration[parameters.size()]; int i=0; for(TypeMirror gType: parameters){ generics[i] = typeToTypeDeclaration(gType); i++; } } } return new TypeDeclaration(owner, generics, dimension); }
private void recordTypeUsage( final TypeMirror type, final Map<Pair<BinaryName, String>, UsagesData<String>> p) { List<TypeMirror> types = new LinkedList<TypeMirror>(); types.add(type); while (!types.isEmpty()) { TypeMirror currentType = types.remove(0); if (currentType == null) continue; switch (currentType.getKind()) { case DECLARED: final Symbol typeSym = ((Type) currentType).tsym; if (typeSym != null && (typeSym.getKind().isClass() || typeSym.getKind().isInterface())) { addUsage (typeSym, activeClass.peek(), p, ClassIndexImpl.UsageType.TYPE_REFERENCE); } types.addAll(((DeclaredType) currentType).getTypeArguments()); break; case ARRAY: types.add(((ArrayType) currentType).getComponentType()); break; } } }
public void testOverrideMethods5() throws Exception { performTest("package test;\npublic class Test extends XX<Number> {\npublic Test(){\n} }\nclass XX<T> {\npublic void test(T ... a) {}}", new OverrideMethodsTask(30), new Validator() { public void validate(CompilationInfo info) { TypeElement clazz = info.getElements().getTypeElement("test.Test"); ExecutableElement method = ElementFilter.methodsIn(clazz.getEnclosedElements()).get(0); assertTrue(method.getSimpleName().contentEquals("test")); TypeElement te = info.getElements().getTypeElement("java.lang.Number"); assertEquals(1, method.getParameters().size()); TypeMirror paramType = method.getParameters().get(0).asType(); assertNotNull(paramType); assertTrue(paramType.getKind() == TypeKind.ARRAY); assertTrue(info.getTypes().isSameType(te.asType(), ((ArrayType)paramType).getComponentType())); assertTrue(method.isVarArgs()); } }); }
private void addDependency(TypeMirror tm) { if (tm.getKind() == TypeKind.ARRAY) { addDependency(((ArrayType)tm).getComponentType()); } else if (tm.getKind() == TypeKind.WILDCARD) { WildcardType wt = (WildcardType)tm; TypeMirror bound = wt.getSuperBound(); if (bound == null) { bound = wt.getExtendsBound(); } addDependency(bound); } else if (tm.getKind() == TypeKind.DECLARED) { addDependency( ((TypeElement)compilationInfo.getTypes().asElement(tm)).getQualifiedName().toString() ); } }
private static String getTypeClass(TypeMirror type, CompilationInfo javac) { TypeKind kind = type.getKind(); if (kind.isPrimitive()) { return type.toString(); } else if (kind == TypeKind.ARRAY) { return resolveArrayClass((ArrayType) type, javac); } else if (kind == TypeKind.DECLARED) { return ((TypeElement) ((DeclaredType) type).asElement()).getQualifiedName().toString(); } else if (kind == TypeKind.ERROR) { return type.toString(); } else if (kind == TypeKind.TYPEVAR) { return javac.getTypes().erasure(type).toString(); } else { throw new IllegalStateException("Unknown type: " + type + ", " + type.getKind()); // NOI18N } }
private static void appendSimpleTypeName(StringBuilder ret, TypeMirror type) { switch (type.getKind()) { case DECLARED: DeclaredType declared = (DeclaredType) type; TypeElement element = (TypeElement) declared.asElement(); ret.append(element.getSimpleName()); break; case TYPEVAR: appendSimpleTypeName(ret, ((TypeVariable) type).getUpperBound()); break; case WILDCARD: appendSimpleTypeName(ret, ((WildcardType) type).getExtendsBound()); break; case ARRAY: appendSimpleTypeName(ret, ((ArrayType) type).getComponentType()); ret.append("Array"); break; default: ret.append(type); } }
private static void containedTypevarsRecursively(@NonNull TypeMirror tm, @NonNull Collection<TypeVariable> typeVars) { switch (tm.getKind()) { case TYPEVAR: typeVars.add((TypeVariable) tm); break; case DECLARED: DeclaredType type = (DeclaredType) tm; for (TypeMirror t : type.getTypeArguments()) { containedTypevarsRecursively(t, typeVars); } break; case ARRAY: containedTypevarsRecursively(((ArrayType) tm).getComponentType(), typeVars); break; case WILDCARD: if (((WildcardType) tm).getExtendsBound() != null) { containedTypevarsRecursively(((WildcardType) tm).getExtendsBound(), typeVars); } if (((WildcardType) tm).getSuperBound() != null) { containedTypevarsRecursively(((WildcardType) tm).getSuperBound(), typeVars); } break; } }
/** * uses FQNs where possible since javadoc does not match imports for * parameter types */ private CharSequence resolveTypeName(TypeMirror asType, boolean isVarArgs) { CharSequence ptype; if (asType.getKind() == TypeKind.DECLARED) { // snip generics Element e = ((DeclaredType) asType).asElement(); ptype = e.getKind().isClass() || e.getKind().isInterface() ? ((TypeElement) e).getQualifiedName() : e.getSimpleName(); } else if (asType.getKind() == TypeKind.TYPEVAR) { do { // Type Erasure JLS 4.6 asType = ((TypeVariable) asType).getUpperBound(); } while (asType.getKind() == TypeKind.TYPEVAR); ptype = resolveTypeName(asType, isVarArgs); } else if (isVarArgs && asType.getKind() == TypeKind.ARRAY) { ptype = resolveTypeName(((ArrayType)asType).getComponentType(), false) + "..."; //NOI18N } else { ptype = asType.toString(); } return ptype; }
private void checkPublicType(ModelContext ctx, TypeMirror type, ExecutableElement method, boolean acceptVoid, String msg) throws ModelException { TypeKind kind = type.getKind(); if (kind.isPrimitive()) { return; } switch (kind) { case ARRAY: checkPublicType(ctx, ((ArrayType) type).getComponentType(), method, false, msg); return; case DECLARED: if (ctx.isPublic(type) || ctx.isProtected(type)) { return; } throw new ModelException(msg + ": " + type + " is not public or protected"); case VOID: if (acceptVoid) { return; } throw new ModelException(msg + ": unsupported type " + type); default: throw new ModelException(msg + ": unsupported type " + type); } }
private void addVarArgs(MethodSpec.Builder specBuilder, LanguageInjector languageInjector, String[] arguments, List<? extends VariableElement> parameters, VariableElement param) { if (parameters.size() <= arguments.length) { ArrayType varargType = (ArrayType) param.asType(); TypeMirror type = varargType.getComponentType(); for (int argPos = parameters.size()-1; argPos < arguments.length; argPos++) { if (argPos > 0) specBuilder.addCode(", "); String argument = arguments[argPos]; languageInjector.add(specBuilder, type, argument); } } }
@Override public void readOutResultsFromStub(VariableElement param, ParamType paramType, String paramName, MethodSpec.Builder methodBuilder) { if (param.asType().getKind() == TypeKind.ARRAY) { methodBuilder.beginControlFlow("if (" + paramName + " != null)"); methodBuilder.addStatement("reply.writeInt(" + paramName + ".length)"); methodBuilder.beginControlFlow("for($T item:" + paramName + " )", ((ArrayType) param.asType()).getComponentType()); methodBuilder.addStatement("Class pClass = getParcelerClass(item)"); methodBuilder.addStatement("reply.writeString(pClass.getName())"); methodBuilder.addStatement("org.parceler.Parcels.wrap(pClass, item).writeToParcel(reply, android.os.Parcelable.PARCELABLE_WRITE_RETURN_VALUE)"); methodBuilder.endControlFlow(); methodBuilder.endControlFlow(); methodBuilder.beginControlFlow("else"); methodBuilder.addStatement("reply.writeInt(-1)"); methodBuilder.endControlFlow(); } }
@Override public void readResultsFromProxy(TypeMirror resultType, MethodSpec.Builder methodBuilder) { if (resultType.getKind() == TypeKind.ARRAY) { methodBuilder.addStatement("int size_result = reply.readInt()"); methodBuilder.beginControlFlow("if (size_result >= 0)"); methodBuilder.addStatement("result = new $T[size_result]", ((ArrayType) resultType).getComponentType()); methodBuilder.beginControlFlow("for(int i=0; i<size_result; i++)"); methodBuilder.addStatement("result[i]= ($T)getParcelerObject(reply.readString(), reply)", ((ArrayType) resultType).getComponentType()); methodBuilder.endControlFlow(); methodBuilder.endControlFlow(); methodBuilder.beginControlFlow("else"); methodBuilder.addStatement("result = null"); methodBuilder.endControlFlow(); } else { methodBuilder.beginControlFlow("if (reply.readInt() != 0)"); methodBuilder.addStatement("result = ($T)getParcelerObject(reply.readString(), reply)", resultType); methodBuilder.endControlFlow(); methodBuilder.beginControlFlow("else"); methodBuilder.addStatement("result = null"); methodBuilder.endControlFlow(); } }
static boolean hasUncheckedWarning(TypeMirror type) { switch (type.getKind()) { case DECLARED: DeclaredType declared = (DeclaredType) type; for (TypeMirror typeParam : declared.getTypeArguments()) { if (hasUncheckedWarning(typeParam)) { return true; } } return false; case TYPEVAR: return true; case WILDCARD: return ((WildcardType) type).getExtendsBound() != null; case ARRAY: return hasUncheckedWarning(((ArrayType) type).getComponentType()); default: return false; } }
AttributeModel(ExecutableElement element) { this.element = element; this.name = element.getSimpleName().toString(); TypeMirror type = element.getReturnType(); this.isArray = type.getKind() == TypeKind.ARRAY; this.type = this.isArray ? ((ArrayType) type).getComponentType() : type; this.kind = AttributeTypeKind.from(this.type); this.suffix = this.isArray ? "[]" : ""; this.mirrorModel = getMirrorModelIfAnnotation(); }
private void appendType(StringBuilder sb, TypeMirror type, boolean varArg) { switch (type.getKind()) { case ARRAY: appendType(sb, ((ArrayType)type).getComponentType(), false); sb.append(varArg ? "..." : "[]"); //NOI18N break; case DECLARED: sb.append(((TypeElement)((DeclaredType)type).asElement()).getQualifiedName()); break; default: sb.append(type); } }
String desc(TypeMirror tm) { switch (tm.getKind()) { case BOOLEAN: return "Z"; case BYTE: return "B"; case SHORT: return "S"; case CHAR: return "C"; case INT: return "I"; case LONG: return "J"; case FLOAT: return "F"; case DOUBLE: return "D"; case VOID: return "V"; case DECLARED: String s = ((TypeElement)((DeclaredType)tm).asElement()).getQualifiedName().toString(); s = s.replace('.', '/'); return "L" + s + ";"; case ARRAY: return "[" + desc(((ArrayType)tm).getComponentType()); default: return tm.getKind().toString(); } }
public ExpressionTree Type(TypeMirror type) { Type t = (Type) type; JCExpression tp; switch (type.getKind()) { case WILDCARD: { WildcardType a = ((WildcardType) type); tp = make.at(NOPOS).Wildcard(make.at(NOPOS).TypeBoundKind(a.kind), (JCExpression) Type(a.type)); break; } case DECLARED: JCExpression clazz = (JCExpression) QualIdent(t.tsym); tp = t.getTypeArguments().isEmpty() ? clazz : make.at(NOPOS).TypeApply(clazz, Types(t.getTypeArguments())); break; case ARRAY: tp = make.at(NOPOS).TypeArray((JCExpression) Type(((ArrayType) type).getComponentType())); break; case NULL: tp = make.at(NOPOS).Literal(TypeTag.BOT, null); break; case ERROR: tp = make.at(NOPOS).Ident(((ErrorType) type).tsym.name); break; default: return make.at(NOPOS).Type((Type)type); } return tp.setType(t); }
@Override public StringBuilder visitArray(ArrayType t, Boolean p) { boolean isVarArg = varArg; varArg = false; visit(t.getComponentType(), p); return DEFAULT_VALUE.append(isVarArg ? "..." : "[]"); //NOI18N }
/** * Returns true if the method is a main method * @param method to be checked * @return true when the method is a main method */ public static boolean isMainMethod (final ExecutableElement method) { if (!"main".contentEquals(method.getSimpleName())) { //NOI18N return false; } long flags = ((Symbol.MethodSymbol)method).flags(); //faster if (((flags & Flags.PUBLIC) == 0) || ((flags & Flags.STATIC) == 0)) { return false; } if (method.getReturnType().getKind() != TypeKind.VOID) { return false; } List<? extends VariableElement> params = method.getParameters(); if (params.size() != 1) { return false; } TypeMirror param = params.get(0).asType(); if (param.getKind() != TypeKind.ARRAY) { return false; } ArrayType array = (ArrayType) param; TypeMirror compound = array.getComponentType(); if (compound.getKind() != TypeKind.DECLARED) { return false; } return "java.lang.String".contentEquals(((TypeElement)((DeclaredType)compound).asElement()).getQualifiedName()); //NOI18N }
private static String getRealTypeName(TypeMirror type, CompilationInfo ci) { final TypeMirror et = ci.getTypes().erasure(type); if (et.getKind() == TypeKind.DECLARED) { return ElementUtilities.getBinaryName((TypeElement)((DeclaredType)et).asElement()); } if (et.getKind() == TypeKind.ARRAY) { return getRealTypeName(((ArrayType)et).getComponentType(), ci) + "[]"; // NOI18N } return et.toString(); }
/** * * @param info context {@link CompilationInfo} * @param iterable tested {@link TreePath} * @return generic type of an {@link Iterable} or {@link ArrayType} at a TreePath */ public static TypeMirror getIterableGenericType(CompilationInfo info, TreePath iterable) { TypeElement iterableElement = info.getElements().getTypeElement("java.lang.Iterable"); //NOI18N if (iterableElement == null) { return null; } TypeMirror iterableType = info.getTrees().getTypeMirror(iterable); if (iterableType == null) { return null; } TypeMirror designedType = null; if (iterableType.getKind() == TypeKind.DECLARED) { DeclaredType declaredType = (DeclaredType) iterableType; if (!info.getTypes().isSubtype(info.getTypes().erasure(declaredType), info.getTypes().erasure(iterableElement.asType()))) { return null; } ExecutableElement iteratorMethod = (ExecutableElement) iterableElement.getEnclosedElements().get(0); ExecutableType iteratorMethodType = (ExecutableType) info.getTypes().asMemberOf(declaredType, iteratorMethod); List<? extends TypeMirror> typeArguments = ((DeclaredType) iteratorMethodType.getReturnType()).getTypeArguments(); if (!typeArguments.isEmpty()) { designedType = typeArguments.get(0); } else { TypeElement jlObject = info.getElements().getTypeElement("java.lang.Object"); if (jlObject != null) { designedType = jlObject.asType(); } } } else if (iterableType.getKind() == TypeKind.ARRAY) { designedType = ((ArrayType) iterableType).getComponentType(); } if (designedType == null) { return null; } return JavaPluginUtils.resolveCapturedType(info, designedType); }
private static List<? extends TypeMirror> computeNewArray(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) { NewArrayTree nat = (NewArrayTree) parent.getLeaf(); if (nat.getType() == error) { types.add(ElementKind.CLASS); types.add(ElementKind.ENUM); types.add(ElementKind.INTERFACE); return null; } for (Tree dimension : nat.getDimensions()) { if (dimension == error) { types.add(ElementKind.PARAMETER); types.add(ElementKind.LOCAL_VARIABLE); types.add(ElementKind.FIELD); return Collections.singletonList(info.getTypes().getPrimitiveType(TypeKind.INT)); } } for (Tree init : nat.getInitializers()) { if (init == error) { TypeMirror whole = info.getTrees().getTypeMirror(parent); if (whole == null || whole.getKind() != TypeKind.ARRAY) return null; types.add(ElementKind.PARAMETER); types.add(ElementKind.LOCAL_VARIABLE); types.add(ElementKind.FIELD); return Collections.singletonList(((ArrayType) whole).getComponentType()); } } return null; }
private void createWrap(TypeElement source, String fqn, List<Tree> members) throws IllegalStateException { Set<Modifier> mods = EnumSet.of(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL); final String name = decapitalize(source.getSimpleName()); List<VariableTree> params = Collections.singletonList(make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), name, make.QualIdent(source), null)); //NOI18N String bodyText = preparePattern(workingCopy, source.asType(), WRAP_PATTERNS, name, source.getSimpleName().toString(), fqn); //NOI18N SourcePositions[] positions = new SourcePositions[1]; StatementTree body = workingCopy.getTreeUtilities().parseStatement(bodyText, positions); ModifiersTree modifiers = prepareModifiers(workingCopy, mods, make, false); MethodTree wrap = make.Method(modifiers, "wrap", make.ArrayType(make.QualIdent(fqn)), Collections.<TypeParameterTree>emptyList(), params, Collections.<ExpressionTree>emptyList(), (BlockTree) body, null, true); //NOI18N members.add(wrap); }
/** * Returns a {@link ArrayType} if the {@link TypeMirror} represents a primitive array or * throws an {@link IllegalArgumentException}. */ public static ArrayType asArray(TypeMirror maybeArrayType) { return maybeArrayType.accept(new CastingTypeVisitor<ArrayType>() { @Override public ArrayType visitArray(ArrayType type, String ignore) { return type; } }, "primitive array"); }
private static String resolveArrayClass(ArrayType array, CompilationInfo javac) { TypeMirror type = array; StringBuilder dim = new StringBuilder(); for (int i = 0; type.getKind() == TypeKind.ARRAY; i++) { type = ((ArrayType) type).getComponentType(); dim.append("[]"); // NOI18N } return getTypeClass(type, javac) + dim; }
public static String typeAsString(TypeMirror type) { switch( type.getKind() ) { case DECLARED: return nameAsString(((DeclaredType)type).asElement()); case ARRAY: return typeAsString(((ArrayType)type).getComponentType()) + "[]"; // NOI18N default: return type.toString(); } }
@Override public CI createArrayItem(CompilationInfo info, ArrayType type, int substitutionOffset, ReferencesCount referencesCount, Elements elements) { int dim = 0; TypeMirror tm = type; while(tm.getKind() == TypeKind.ARRAY) { tm = ((ArrayType)tm).getComponentType(); dim++; } if (tm.getKind().isPrimitive()) { String kwd = tm.toString(); StringBuilder sb = new StringBuilder(kwd); for(int i = 0; i < dim; i++) { sb.append("[]"); //NOI18N } return new CI(sb.toString(), 670 - SMART_TYPE, kwd); } if (tm.getKind() == TypeKind.DECLARED || tm.getKind() == TypeKind.ERROR) { DeclaredType dt = (DeclaredType)tm; TypeElement elem = (TypeElement)dt.asElement(); String simpleName = elem.getSimpleName().toString(); String fqn = elem.getQualifiedName().toString(); int weight = 50; if (fqn.startsWith("java.lang") || fqn.startsWith("java.util")) { // NOI18N weight -= 10; } else if (fqn.startsWith("org.omg") || fqn.startsWith("org.apache")) { // NOI18N weight += 10; } else if (fqn.startsWith("com.sun") || fqn.startsWith("com.ibm") || fqn.startsWith("com.apple")) { // NOI18N weight += 20; } else if (fqn.startsWith("sun") || fqn.startsWith("sunw") || fqn.startsWith("netscape")) { // NOI18N weight += 30; } return new CI(simpleName, 800 - SMART_TYPE, referencesCount != null ? simpleName + '#' + weight + '#' + info.getElementUtilities().getElementName(elem.getEnclosingElement(), true) : simpleName); } throw new IllegalArgumentException("array element kind=" + tm.getKind()); }
public static TypeElement getEnclosedArrayElement(Element element, Elements elementUtils) { ArrayType arrayType = (ArrayType) element.asType(); if (arrayType != null) { TypeName tName = TypeName.get(arrayType.getComponentType()); if (tName != null) { return elementUtils.getTypeElement(tName.toString()); } } return null; }
/** * * @param info context {@link CompilationInfo} * @param iterable tested {@link TreePath} * @return generic type of an {@link Iterable} or {@link ArrayType} at a TreePath */ public static TypeMirror getIterableGenericType(CompilationInfo info, TreePath iterable) { TypeElement iterableElement = info.getElements().getTypeElement("java.lang.Iterable"); //NOI18N if (iterableElement == null) { return null; } TypeMirror iterableType = info.getTrees().getTypeMirror(iterable); if (iterableType == null) { return null; } TypeMirror designedType = null; if (iterableType.getKind() == TypeKind.DECLARED) { DeclaredType declaredType = (DeclaredType) iterableType; if (!info.getTypes().isSubtype(info.getTypes().erasure(declaredType), info.getTypes().erasure(iterableElement.asType()))) { return null; } ExecutableElement iteratorMethod = (ExecutableElement) iterableElement.getEnclosedElements().get(0); ExecutableType iteratorMethodType = (ExecutableType) info.getTypes().asMemberOf(declaredType, iteratorMethod); List<? extends TypeMirror> typeArguments = ((DeclaredType) iteratorMethodType.getReturnType()).getTypeArguments(); if (!typeArguments.isEmpty()) { designedType = typeArguments.get(0); } else { TypeElement jlObject = info.getElements().getTypeElement("java.lang.Object"); if (jlObject != null) { designedType = jlObject.asType(); } } } else if (iterableType.getKind() == TypeKind.ARRAY) { designedType = ((ArrayType) iterableType).getComponentType(); } if (designedType == null) { return null; } return resolveTypeForDeclaration(info, designedType); }
@Override public StringBuilder visitArray(ArrayType type, StringBuilder sb) { visit(type.getComponentType(), sb); List<? extends AnnotationMirror> annotationMirrors = type.getAnnotationMirrors(); if (!annotationMirrors.isEmpty()) { sb.append(" "); appendAnnotations(annotationMirrors, sb); } return sb.append("[]"); }
public static boolean containsErrorsRecursively(TypeMirror tm) { switch (tm.getKind()) { case ERROR: return true; case DECLARED: DeclaredType type = (DeclaredType) tm; for (TypeMirror t : type.getTypeArguments()) { if (containsErrorsRecursively(t)) return true; } return false; case ARRAY: return containsErrorsRecursively(((ArrayType) tm).getComponentType()); case WILDCARD: if (((WildcardType) tm).getExtendsBound() != null && containsErrorsRecursively(((WildcardType) tm).getExtendsBound())) { return true; } if (((WildcardType) tm).getSuperBound() != null && containsErrorsRecursively(((WildcardType) tm).getSuperBound())) { return true; } return false; case OTHER: return true; default: return false; } }
public TypeMirror getComponentType(TypeMirror t) { if (isArray(t)) { ArrayType at = (ArrayType) t; return at.getComponentType(); } throw new IllegalArgumentException(); }
private static String createHtmlHeader(boolean deprecated, ExecutableElement e) { StringBuilder sb = new StringBuilder(); sb.append("<html>"); if (deprecated) sb.append("<s>"); if (e.getKind() == ElementKind.CONSTRUCTOR) { sb.append(e.getEnclosingElement().getSimpleName()); } else { sb.append(e.getSimpleName()); } if (deprecated) sb.append("</s>"); sb.append("("); // NOI18N for(Iterator<? extends VariableElement> it = e.getParameters().iterator(); it.hasNext(); ) { VariableElement param = it.next(); if (!it.hasNext() && e.isVarArgs() && param.asType().getKind() == TypeKind.ARRAY) { sb.append(translateToHTML(print(((ArrayType) param.asType()).getComponentType()))); sb.append("..."); } else { sb.append(translateToHTML(print(param.asType()))); } sb.append(" "); // NOI18N sb.append(param.getSimpleName()); if (it.hasNext()) { sb.append(", "); // NOI18N } } sb.append(")"); // NOI18N if ( e.getKind() != ElementKind.CONSTRUCTOR ) { TypeMirror rt = e.getReturnType(); if ( rt.getKind() != TypeKind.VOID ) { sb.append(" : "); // NOI18N sb.append(translateToHTML(print(e.getReturnType()))); } } return sb.toString(); }
@Override public Boolean visitArray(ArrayType a, EqualVisitorParam p) { if (p.type.getKind().equals(ARRAY)) { ArrayType b = (ArrayType) p.type; return equal(a.getComponentType(), b.getComponentType(), p.visiting); } return false; }
public void testGetTypeName() throws Exception { TestUtilities.copyStringToFileObject(testFO, "package foo;" + "public class TestClass {" + "}"); runUserActionTask(testFO, new Task<CompilationController>() { public void run(CompilationController controller) throws IOException { controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); Elements elements = controller.getElements(); Types types = controller.getTypes(); String typeName = String.class.getName(); String resolvedTypeName = MethodModelSupport.getTypeName(elements.getTypeElement(typeName).asType()); assertEquals(typeName, resolvedTypeName); typeName = InputStream.class.getName(); resolvedTypeName = MethodModelSupport.getTypeName(elements.getTypeElement(typeName).asType()); assertEquals(typeName, resolvedTypeName); resolvedTypeName = MethodModelSupport.getTypeName(types.getPrimitiveType(TypeKind.INT)); assertEquals("int", resolvedTypeName); typeName = String.class.getName(); resolvedTypeName = MethodModelSupport.getTypeName(types.getArrayType(elements.getTypeElement(typeName).asType())); assertEquals("java.lang.String[]", resolvedTypeName); PrimitiveType primitiveType = types.getPrimitiveType(TypeKind.BYTE); ArrayType arrayType = types.getArrayType(primitiveType); resolvedTypeName = MethodModelSupport.getTypeName(arrayType); assertEquals("byte[]", resolvedTypeName); } }); }
private static String getTypeBinaryName(TypeMirror t) { if (t instanceof ArrayType) { TypeMirror ct = ((ArrayType) t).getComponentType(); return getTypeBinaryName(ct)+"[]"; } if (t instanceof DeclaredType) { return ElementUtilities.getBinaryName((TypeElement) ((DeclaredType) t).asElement()); } return t.toString(); }
@NonNull private TypeMirror findComponentType(@NonNull TypeMirror type) { if (type.getKind() != TypeKind.ARRAY) { return type; } return findComponentType(((ArrayType)type).getComponentType()); }
private Element getElement(Tree tree) { TreePath expPath = TreePath.getPath(cut, tree); Element e = trees.getElement(expPath); if (e == null) { if (tree instanceof ParenthesizedTree) { e = getElement(((ParenthesizedTree) tree).getExpression()); //if (e == null) { // System.err.println("Have null element for "+tree); //} //System.err.println("\nHAVE "+e.asType().toString()+" for ParenthesizedTree "+tree); } else if (tree instanceof TypeCastTree) { e = getElement(((TypeCastTree) tree).getType()); //if (e == null) { // System.err.println("Have null element for "+tree); //} //System.err.println("\nHAVE "+e.asType().toString()+" for TypeCastTree "+tree); } else if (tree instanceof AssignmentTree) { e = getElement(((AssignmentTree) tree).getVariable()); } else if (tree instanceof ArrayAccessTree) { e = getElement(((ArrayAccessTree) tree).getExpression()); if (e != null) { TypeMirror tm = e.asType(); if (tm.getKind() == TypeKind.ARRAY) { tm = ((ArrayType) tm).getComponentType(); e = types.asElement(tm); } } //System.err.println("ArrayAccessTree = "+((ArrayAccessTree) tree).getExpression()+", element = "+getElement(((ArrayAccessTree) tree).getExpression())+", type = "+getElement(((ArrayAccessTree) tree).getExpression()).asType()); } } return e; }