예제 #1
0
  public boolean needToCastBundleType(TypeMirror type) throws InvalidTypeException {
    if (isPrimitive(type)) {
      return false;
    }

    if (isArrayType(type)) {
      return needToCastArrayType((ArrayType) type);
    }

    if (isArrayListType(type)) {
      return false;
    }

    if (isSparseParcelableArray(type)) {
      return needToCastSparseParcelableArray((DeclaredType) type);
    }

    if (types.isAssignable(type, bundleType)) {
      return !types.isSameType(type, bundleType);
    }

    if (isAggregateType(type)) {
      return needToCastAggregateType(type);
    }

    if (types.isAssignable(type, serializableType)) {
      return !types.isSameType(type, serializableType);
    }

    throw new InvalidTypeException(InvalidTypeException.Container.BUNDLE, type);
  }
예제 #2
0
  public boolean needToCastIntentType(TypeMirror type) throws InvalidTypeException {
    if (isPrimitive(type)) {
      return false;
    }

    if (isArrayType(type)) {
      return needToCastArrayType((ArrayType) type);
    }

    if (isArrayListType(type)) {
      return false;
    }

    if (types.isAssignable(type, bundleType)) {
      return !types.isSameType(type, bundleType);
    }

    if (isAggregateType(type)) {
      return needToCastAggregateType(type);
    }

    if (types.isAssignable(type, serializableType)) {
      return !types.isSameType(type, serializableType);
    }

    throw new InvalidTypeException(InvalidTypeException.Container.INTENT, type);
  }
예제 #3
0
  public String getIntentType(TypeMirror type) throws InvalidTypeException {
    // Primitive
    if (isPrimitive(type)) {
      return getPrimitiveType(type);
    }

    // Array
    if (isArrayType(type)) {
      return getArrayType((ArrayType) type);
    }

    // ArrayList
    if (isArrayListType(type)) {
      return getArrayListType((DeclaredType) type);
    }

    if (types.isAssignable(type, bundleType)) {
      return "Bundle";
    }

    if (isAggregateType(type)) {
      return getAggregateType(type);
    }

    if (types.isAssignable(type, serializableType)) {
      return "Serializable";
    }
    throw new InvalidTypeException(InvalidTypeException.Container.INTENT, type);
  }
예제 #4
0
  public String getBundleType(TypeMirror type) throws InvalidTypeException {
    // Primitive
    if (isPrimitive(type)) {
      return getPrimitiveType(type);
    }

    // Array
    if (isArrayType(type)) {
      return getArrayType((ArrayType) type);
    }

    // ArrayList
    if (isArrayListType(type)) {
      return getArrayListType((DeclaredType) type);
    }

    // Sparse ParcelableArray
    if (isSparseParcelableArray(type)) {
      return "SparseParcelableArray";
    }

    // Other types
    if (types.isAssignable(type, bundleType)) {
      return "Bundle";
    }

    if (isAggregateType(type)) {
      return getAggregateType(type);
    }

    if (types.isAssignable(type, serializableType)) {
      return "Serializable";
    }
    throw new InvalidTypeException(InvalidTypeException.Container.BUNDLE, type);
  }
예제 #5
0
 private boolean isSparseParcelableArray(TypeMirror type) {
   if (types.isAssignable(types.erasure(type), sparseArrayType) && type instanceof DeclaredType) {
     List<? extends TypeMirror> typeArguments = ((DeclaredType) type).getTypeArguments();
     if (typeArguments.size() == 1) {
       return types.isAssignable(typeArguments.get(0), parcelableType);
     }
   }
   return false;
 }
예제 #6
0
 private boolean needToCastAggregateType(TypeMirror type) throws InvalidTypeException {
   if (types.isAssignable(type, charSequenceType)) {
     return !types.isSameType(type, charSequenceType);
   }
   if (types.isAssignable(type, stringType)) {
     return !types.isSameType(type, stringType);
   }
   if (types.isAssignable(type, parcelableType)) {
     return !types.isSameType(type, parcelableType);
   }
   throw new InvalidTypeException(type);
 }
예제 #7
0
 private String getAggregateType(TypeMirror type) throws InvalidTypeException {
   if (types.isAssignable(type, stringType)) { // String is subtype of CharSequence should go first
     return "String";
   }
   if (types.isAssignable(type, charSequenceType)) {
     return "CharSequence";
   }
   if (types.isAssignable(type, parcelableType)) {
     return "Parcelable";
   }
   throw new InvalidTypeException(type);
 }
예제 #8
0
  public ErrorDescription[] check(JPAProblemContext ctx, AttributeWrapper attrib) {
    if (!(attrib.getModelElement() instanceof Basic)) {
      return null;
    }

    TreeUtilities treeUtils = ctx.getCompilationInfo().getTreeUtilities();
    Types types = ctx.getCompilationInfo().getTypes();
    TypeMirror attrType = attrib.getType();

    TypeMirror typeSerializable =
        treeUtils.parseType(
            "java.io.Serializable", // NOI18N
            ctx.getJavaClass());

    TypeMirror typeEnum =
        treeUtils.parseType(
            "java.lang.Enum", // NOI18N
            ctx.getJavaClass());

    TypeMirror typeCollection =
        treeUtils.parseType(
            "java.util.Collection", // NOI18N
            ctx.getJavaClass());

    if (types.isAssignable(attrType, typeSerializable)
        || types.isAssignable(attrType, typeEnum)
        || types.isAssignable(attrType, typeCollection)) {
      return null;
    }

    for (String typeName : fixedBasicTypes) {
      TypeMirror type = treeUtils.parseType(typeName, ctx.getJavaClass());

      if (type != null && types.isSameType(attrType, type)) {
        return null;
      }
    }

    if (Utilities.hasAnnotation(attrib.getJavaElement(), JPAAnnotations.ELEMENT_COLLECTION)) {
      // according to annotation it's not basic  type and need to be verified in appropriate
      // validator
      return null;
    }

    return new ErrorDescription[] {
      Rule.createProblem(
          attrib.getJavaElement(),
          ctx,
          NbBundle.getMessage(ValidBasicType.class, "MSG_ValidBasicType"))
    };
  }
예제 #9
0
 private boolean isArrayListType(TypeMirror type) {
   if (types.isAssignable(types.erasure(type), arrayListType)) {
     List<? extends TypeMirror> typeArguments = ((DeclaredType) type).getTypeArguments();
     if (typeArguments.size() == 1) {
       TypeMirror arg = typeArguments.get(0);
       if (types.isAssignable(arg, integerType)) {
         return true;
       }
       if (isAggregateType(arg)) {
         return true;
       }
     }
   }
   return false;
 }
예제 #10
0
 private String getArrayListType(DeclaredType type) throws InvalidTypeException {
   TypeMirror arg = type.getTypeArguments().get(0);
   if (types.isAssignable(arg, integerType)) {
     return "IntegerArrayList";
   }
   try {
     return getAggregateType(arg).concat("ArrayList");
   } catch (InvalidTypeException e) {
     throw new InvalidTypeException(type);
   }
 }
 /**
  * Check shareable components for serializability.
  *
  * @param round The round environment.
  */
 private void checkShareableComponents(RoundEnvironment round) {
   Set<? extends Element> elts = round.getElementsAnnotatedWith(Shareable.class);
   note("processing %d shareable elements", elts.size());
   TypeMirror serializable = elementUtils.getTypeElement("java.io.Serializable").asType();
   for (Element elt : elts) {
     note("examining %s", elt);
     TypeMirror type = elt.asType();
     if (typeUtils.isAssignable(type, serializable)) {
       note("shareable type %s is serializable", type);
     } else {
       warning(elt, "shareable type %s is not serializable", type);
     }
   }
 }
  public ValidateDescriptor(ModelDescriptor modelDescriptor, Element field) {
    super(modelDescriptor, field);
    this.validateIf = field.getAnnotation(ValidateIf.class);
    this.validateIfValue = field.getAnnotation(ValidateIfValue.class);

    ProcessingEnvironment processingEnv = modelDescriptor.getProcessingEnvironment();
    Types typeUtils = processingEnv.getTypeUtils();
    this.isMethodValidation = getField().getKind().equals(ElementKind.METHOD);

    if (this.isMethodValidation) {
      // Make sure our method validation returns a boolean value
      if (!((ExecutableElement) this.field).getReturnType().getKind().equals(TypeKind.BOOLEAN)) {
        modelDescriptor
            .getMessager()
            .printMessage(
                Diagnostic.Kind.ERROR,
                "Methods annotated with @Validate must return a boolean value!",
                field);
      }

      methodAnnotation = getField().getAnnotation(Validate.class);
      if (this.methodAnnotation == null) {
        modelDescriptor
            .getMessager()
            .printMessage(Diagnostic.Kind.ERROR, "Could not retrieve method validation annotation");
      }
    } else {
      this.isList =
          typeUtils.isAssignable(
              field.asType(),
              typeUtils.getDeclaredType(
                  processingEnv
                      .getElementUtils()
                      .getTypeElement(ListContainer.class.getCanonicalName()),
                  typeUtils.getWildcardType(null, null)));
    }
  }
  private Decorator getDecorator(
      TypeElement element, List<SourceMethod> methods, String implName, String implPackage) {
    DecoratedWithPrism decoratorPrism = DecoratedWithPrism.getInstanceOn(element);

    if (decoratorPrism == null) {
      return null;
    }

    TypeElement decoratorElement = (TypeElement) typeUtils.asElement(decoratorPrism.value());

    if (!typeUtils.isAssignable(decoratorElement.asType(), element.asType())) {
      messager.printMessage(element, decoratorPrism.mirror, Message.DECORATOR_NO_SUBTYPE);
    }

    List<MappingMethod> mappingMethods = new ArrayList<MappingMethod>(methods.size());

    for (SourceMethod mappingMethod : methods) {
      boolean implementationRequired = true;
      for (ExecutableElement method :
          ElementFilter.methodsIn(decoratorElement.getEnclosedElements())) {
        if (elementUtils.overrides(method, mappingMethod.getExecutable(), decoratorElement)) {
          implementationRequired = false;
          break;
        }
      }
      Type declaringMapper = mappingMethod.getDeclaringMapper();
      if (implementationRequired && !(mappingMethod.isDefault() || mappingMethod.isStatic())) {
        if ((declaringMapper == null) || declaringMapper.equals(typeFactory.getType(element))) {
          mappingMethods.add(new DelegatingMethod(mappingMethod));
        }
      }
    }

    boolean hasDelegateConstructor = false;
    boolean hasDefaultConstructor = false;
    for (ExecutableElement constructor :
        ElementFilter.constructorsIn(decoratorElement.getEnclosedElements())) {
      if (constructor.getParameters().isEmpty()) {
        hasDefaultConstructor = true;
      } else if (constructor.getParameters().size() == 1) {
        if (typeUtils.isAssignable(element.asType(), first(constructor.getParameters()).asType())) {
          hasDelegateConstructor = true;
        }
      }
    }

    if (!hasDelegateConstructor && !hasDefaultConstructor) {
      messager.printMessage(element, decoratorPrism.mirror, Message.DECORATOR_CONSTRUCTOR);
    }

    Decorator decorator =
        new Decorator.Builder()
            .elementUtils(elementUtils)
            .typeFactory(typeFactory)
            .mapperElement(element)
            .decoratorPrism(decoratorPrism)
            .methods(mappingMethods)
            .hasDelegateConstructor(hasDelegateConstructor)
            .options(options)
            .versionInformation(versionInformation)
            .implName(implName)
            .implPackage(implPackage)
            .extraImports(getExtraImports(element))
            .build();

    return decorator;
  }
예제 #14
0
 private boolean needToCastSparseParcelableArray(DeclaredType type) {
   List<? extends TypeMirror> typeArguments = type.getTypeArguments();
   return !types.isAssignable(typeArguments.get(0), parcelableType);
 }
예제 #15
0
 private boolean isAggregateType(TypeMirror type) {
   return types.isAssignable(type, stringType)
       || types.isAssignable(type, charSequenceType)
       || types.isAssignable(type, parcelableType);
 }