private TypeMirror getComponentType(TypeMirror mirror) { if (mirror.getKind() != TypeKind.ARRAY) { return null; } ArrayType arrayType = (ArrayType) mirror; return arrayType.getComponentType(); }
private boolean needToCastArrayType(ArrayType type) throws InvalidTypeException { TypeMirror componentType = type.getComponentType(); if (isPrimitive(componentType)) { return false; } try { return needToCastAggregateType(componentType); } catch (InvalidTypeException e) { throw new InvalidTypeException(type); } }
private String getArrayType(ArrayType type) throws InvalidTypeException { TypeMirror componentType = type.getComponentType(); if (isPrimitive(componentType)) { try { return getPrimitiveType(componentType).concat("Array"); } catch (InvalidTypeException e) { throw new InvalidTypeException(type); } } try { return getAggregateType(componentType).concat("Array"); } catch (InvalidTypeException e) { throw new InvalidTypeException(type); } }
private void parseBindMany( Element element, Map<TypeElement, BindingClass> targetClassMap, Set<String> erasedTargetNames) { boolean hasError = false; TypeElement enclosingElement = (TypeElement) element.getEnclosingElement(); // Verify that the type is a List or an array. TypeMirror elementType = element.asType(); String erasedType = doubleErasure(elementType); TypeMirror viewType = null; FieldCollectionViewBinding.Kind kind = null; if (elementType.getKind() == TypeKind.ARRAY) { ArrayType arrayType = (ArrayType) elementType; viewType = arrayType.getComponentType(); kind = FieldCollectionViewBinding.Kind.ARRAY; } else if (LIST_TYPE.equals(erasedType)) { DeclaredType declaredType = (DeclaredType) elementType; List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments(); if (typeArguments.size() != 1) { error( element, "@%s List must have a generic component. (%s.%s)", Bind.class.getSimpleName(), enclosingElement.getQualifiedName(), element.getSimpleName()); hasError = true; } else { viewType = typeArguments.get(0); } kind = FieldCollectionViewBinding.Kind.LIST; } else { throw new AssertionError(); } if (viewType != null && viewType.getKind() == TypeKind.TYPEVAR) { TypeVariable typeVariable = (TypeVariable) viewType; viewType = typeVariable.getUpperBound(); } // Verify that the target type extends from View. if (viewType != null && !isSubtypeOfType(viewType, VIEW_TYPE) && !isInterface(viewType)) { error( element, "@%s List or array type must extend from View or be an interface. (%s.%s)", Bind.class.getSimpleName(), enclosingElement.getQualifiedName(), element.getSimpleName()); hasError = true; } if (hasError) { return; } // Assemble information on the field. String name = element.getSimpleName().toString(); int[] ids = element.getAnnotation(Bind.class).value(); if (ids.length == 0) { error( element, "@%s must specify at least one ID. (%s.%s)", Bind.class.getSimpleName(), enclosingElement.getQualifiedName(), element.getSimpleName()); return; } Integer duplicateId = findDuplicate(ids); if (duplicateId != null) { error( element, "@%s annotation contains duplicate ID %d. (%s.%s)", Bind.class.getSimpleName(), duplicateId, enclosingElement.getQualifiedName(), element.getSimpleName()); } assert viewType != null; // Always false as hasError would have been true. String type = viewType.toString(); boolean required = isRequiredBinding(element); BindingClass bindingClass = getOrCreateTargetClass(targetClassMap, enclosingElement); FieldCollectionViewBinding binding = new FieldCollectionViewBinding(name, type, kind, required); bindingClass.addFieldCollection(ids, binding); erasedTargetNames.add(enclosingElement.toString()); }
private void parseInjectViews( Element element, Map<TypeElement, ViewInjector> targetClassMap, Set<String> erasedTargetNames) { boolean hasError = false; TypeElement enclosingElement = (TypeElement) element.getEnclosingElement(); // Verify that the type is a List or an array. TypeMirror elementType = element.asType(); String erasedType = doubleErasure(elementType); TypeMirror viewType = null; CollectionBinding.Kind kind = null; if (elementType.getKind() == TypeKind.ARRAY) { ArrayType arrayType = (ArrayType) elementType; viewType = arrayType.getComponentType(); kind = CollectionBinding.Kind.ARRAY; } else if (LIST_TYPE.equals(erasedType)) { DeclaredType declaredType = (DeclaredType) elementType; List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments(); if (typeArguments.size() != 1) { error( element, "@InjectViews List must have a generic component. (%s.%s)", enclosingElement.getQualifiedName(), element.getSimpleName()); hasError = true; } else { viewType = typeArguments.get(0); } kind = CollectionBinding.Kind.LIST; } else { error( element, "@InjectViews must be a List or array. (%s.%s)", enclosingElement.getQualifiedName(), element.getSimpleName()); hasError = true; } if (viewType instanceof TypeVariable) { TypeVariable typeVariable = (TypeVariable) viewType; viewType = typeVariable.getUpperBound(); } // Verify that the target type extends from View. if (viewType != null && !isSubtypeOfType(viewType, VIEW_TYPE) && !isInterface(viewType)) { error( element, "@InjectViews type must extend from View or be an interface. (%s.%s)", enclosingElement.getQualifiedName(), element.getSimpleName()); hasError = true; } // Verify common generated code restrictions. hasError |= isInaccessibleViaGeneratedCode(InjectViews.class, "fields", element); hasError |= isBindingInWrongPackage(InjectViews.class, element); if (hasError) { return; } // Assemble information on the injection point. String name = element.getSimpleName().toString(); int[] ids = element.getAnnotation(InjectViews.class).value(); if (ids.length == 0) { error( element, "@InjectViews must specify at least one ID. (%s.%s)", enclosingElement.getQualifiedName(), element.getSimpleName()); return; } Integer duplicateId = findDuplicate(ids); if (duplicateId != null) { error( element, "@InjectViews annotation contains duplicate ID %d. (%s.%s)", duplicateId, enclosingElement.getQualifiedName(), element.getSimpleName()); } assert viewType != null; // Always false as hasError would have been true. String type = viewType.toString(); boolean required = element.getAnnotation(Optional.class) == null; ViewInjector viewInjector = getOrCreateTargetClass(targetClassMap, enclosingElement); CollectionBinding binding = new CollectionBinding(name, type, kind, required); viewInjector.addCollection(ids, binding); erasedTargetNames.add(enclosingElement.toString()); }
void addMethod(ModuleMetaModel context, AnnotationKey key2, AnnotationState annotation) { // String id = (String) annotation.get("id"); ExecutableElement methodElt = (ExecutableElement) context.processingContext.get(key2.getElement()); // for (Phase phase : Phase.values()) { if (phase.annotation.getName().equals(key2.getType().toString())) { ElementHandle.Method origin = (ElementHandle.Method) key2.getElement(); // First remove the previous method Key<MethodMetaModel> key = Key.of(origin, MethodMetaModel.class); if (getChild(key) == null) { // Parameters ArrayList<ParameterMetaModel> parameters = new ArrayList<ParameterMetaModel>(); List<? extends TypeMirror> parameterTypeMirrors = ((ExecutableType) methodElt.asType()).getParameterTypes(); List<? extends VariableElement> parameterVariableElements = methodElt.getParameters(); for (int i = 0; i < parameterTypeMirrors.size(); i++) { VariableElement parameterVariableElt = parameterVariableElements.get(i); TypeMirror parameterTypeMirror = parameterTypeMirrors.get(i); String typeLiteral = context.processingContext.getLiteralName(parameterTypeMirror); // String parameterName = parameterVariableElt.getSimpleName().toString(); // Determine cardinality TypeMirror parameterSimpleTypeMirror; Cardinality parameterCardinality; switch (parameterTypeMirror.getKind()) { case DECLARED: DeclaredType dt = (DeclaredType) parameterTypeMirror; TypeElement col = context.processingContext.getTypeElement("java.util.List"); TypeMirror tm = context.processingContext.erasure(col.asType()); TypeMirror err = context.processingContext.erasure(dt); // context.env.isSubtype(err, tm) if (err.equals(tm)) { if (dt.getTypeArguments().size() != 1) { throw CONTROLLER_METHOD_PARAMETER_NOT_RESOLVED.failure(parameterVariableElt); } else { parameterCardinality = Cardinality.LIST; parameterSimpleTypeMirror = dt.getTypeArguments().get(0); } } else { parameterCardinality = Cardinality.SINGLE; parameterSimpleTypeMirror = parameterTypeMirror; } break; case ARRAY: // Unwrap array ArrayType arrayType = (ArrayType) parameterTypeMirror; parameterCardinality = Cardinality.ARRAY; parameterSimpleTypeMirror = arrayType.getComponentType(); break; default: throw CONTROLLER_METHOD_PARAMETER_NOT_RESOLVED.failure(parameterVariableElt); } if (parameterSimpleTypeMirror.getKind() != TypeKind.DECLARED) { throw CONTROLLER_METHOD_PARAMETER_NOT_RESOLVED.failure(parameterVariableElt); } // TypeElement te = (TypeElement) context.processingContext.asElement(parameterSimpleTypeMirror); ElementHandle.Type a = ElementHandle.Type.create(te); // if (te.toString().equals("java.lang.String") || te.getAnnotation(Mapped.class) != null) { // Not sure we should use @Param for this (i.e for now it looks hackish) // however it does make sense later to use the regex part for non router // parameters Param param = parameterVariableElt.getAnnotation(Param.class); String alias = param != null && param.name().length() > 0 ? param.name() : null; parameters.add( new PhaseParameterMetaModel( parameterName, parameterCardinality, a, typeLiteral, alias)); } else { parameters.add(new ContextualParameterMetaModel(parameterName, typeLiteral)); } } // MethodMetaModel method = new MethodMetaModel( origin, id, phase, methodElt.getSimpleName().toString(), parameters); addChild(key, method); modified = true; } break; } } }
@Override public ASTType visitArray(ArrayType arrayType, Void v) { return new ASTArrayType(arrayType.getComponentType().accept(this, null)); }
@Override public Void visitArray(ArrayType t, VariableElement element) { return t.getComponentType().accept(arrayFieldProcessor, element); }
@Override public JsonType visitArray(ArrayType arrayType, Void o) { throw new UnsupportedOperationException(arrayType.toString()); }
@Override public TypeElement visitArray(ArrayType t, Void p) { return visit(t.getComponentType()); }
@Override @DefinedBy(Api.LANGUAGE_MODEL) public Void visitArray(ArrayType t, Types types) { visit(t.getComponentType(), types); return null; }
@Override public Boolean visitArray(ArrayType t, MembersInjectionBinding p) { return visit(t.getComponentType(), p); }