Java Code Examples for com.google.common.reflect.TypeToken#equals()

The following examples show how to use com.google.common.reflect.TypeToken#equals() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FieldDelegate.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
private static Field findField(Class<?> owner, TypeToken<?> type, String name) {
    try {
        final Field field = owner.getDeclaredField(name);
        final TypeToken<?> actualType = TypeToken.of(field.getGenericType());
        if(!type.equals(actualType)) {
            throw new NoSuchFieldError(
                "Expected field " + Members.qualifiedName(field) +
                " to have exact type " + type +
                " but the actual type is " + actualType
            );
        }
        return field;
    } catch(NoSuchFieldException e) {
        throw new NoSuchFieldError(e.getMessage());
    }
}
 
Example 2
Source File: BasicParameterType.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public BasicParameterType(String name, TypeToken<T> type, String description, T defaultValue, boolean hasDefaultValue) {
    this.name = name;
    if (type!=null && type.equals(TypeToken.of(type.getRawType()))) {
        // prefer Class if it's already a raw type; keeps persistence simpler (and the same as before)
        this.type = (Class<T>) type.getRawType();
    } else {
        this.typeT = type;
    }
    this.description = description;
    this.defaultValue = defaultValue;
    if (defaultValue!=null && !defaultValue.getClass().equals(Object.class)) {
        // if default value is null (or is an Object, which is ambiguous on resolution to to rebind), 
        // don't bother to set this as it creates noise in the persistence files
        this.hasDefaultValue = hasDefaultValue;
    }
}
 
Example 3
Source File: GenericMethodType.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
public GenericMethodType<? extends T> resolveIn(TypeToken<?> context) {
    final TypeToken<? extends T> returnType = (TypeToken<? extends T>) context.resolveType(this.returnType.getType());
    final ImmutableList<TypeToken<?>> parameterTypes = this.parameterTypes.stream()
                                                                          .map(t -> context.resolveType(t.getType()))
                                                                          .collect(Collectors.toImmutableList());
    return returnType.equals(this.returnType) && parameterTypes.equals(this.parameterTypes)
           ? this
           : new GenericMethodType<>(returnType, parameterTypes);
}
 
Example 4
Source File: TypeTokens.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/** returns null if it's raw, else the type token */
@Nullable
public static <T> TypeToken<T> getTypeTokenIfNotRaw(@Nullable TypeToken<T> type) {
    if (type==null || type.equals(TypeToken.of(type.getRawType()))) {
        return null;
    } else {
        return type;
    }
}
 
Example 5
Source File: TypeTokens.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/** returns raw type, if it's raw, else null;
 * used e.g. to set only one of the raw type or the type token,
 * for instance to make serialized output nicer */
@Nullable
public static <T> Class<? super T> getRawTypeIfRaw(@Nullable TypeToken<T> type) {
    if (type==null || !type.equals(TypeToken.of(type.getRawType()))) {
        return null;
    } else {
        return type.getRawType();
    }
}
 
Example 6
Source File: JsonConfigWriter.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@Nullable
 private String schemaFormatForType(TypeToken<?> type, ApiConfig apiConfig) {
   TypeToken<?> serializedType = ApiAnnotationIntrospector.getSchemaType(type, apiConfig);
   if (!type.equals(serializedType)) {
     return schemaFormatForType(serializedType, apiConfig);
   }
   return typeLoader.getSchemaFormat(type);
}
 
Example 7
Source File: JavaOptionalConverterModule.java    From beanmother with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canHandle(Object source, TypeToken<?> targetTypeToken) {
    return stringToNumberConverter.canHandle(source, TypeToken.of(Integer.class))
            && targetTypeToken.equals(TypeToken.of(OptionalInt.class));
}
 
Example 8
Source File: JsonConfigWriter.java    From endpoints-java with Apache License 2.0 4 votes vote down vote up
/**
 * Adds an arbitrary, non-array type to a given schema config.
 *
 * @param schemasNode the config to store the generated type schema
 * @param type the type from which to generate a schema
 * @param enclosingType for bean properties, the enclosing bean type, used for resolving type
 *        variables
 * @return the name of the schema generated from the type
 */
@VisibleForTesting
String addTypeToSchema(
    ObjectNode schemasNode, TypeToken<?> type, TypeToken<?> enclosingType, ApiConfig apiConfig,
    List<ApiParameterConfig> parameterConfigs) throws ApiConfigException {
  if (typeLoader.isSchemaType(type)) {
    return typeLoader.getSchemaType(type);
  } else if (Types.isObject(type)) {
    if (!schemasNode.has(ANY_SCHEMA_NAME)) {
      ObjectNode anySchema = objectMapper.createObjectNode();
      anySchema.put("id", ANY_SCHEMA_NAME);
      anySchema.put("type", "any");
      schemasNode.set(ANY_SCHEMA_NAME, anySchema);
    }
    return ANY_SCHEMA_NAME;
  } else if (Types.isMapType(type)) {
    if (!schemasNode.has(MAP_SCHEMA_NAME)) {
      ObjectNode mapSchema = objectMapper.createObjectNode();
      mapSchema.put("id", MAP_SCHEMA_NAME);
      mapSchema.put("type", "object");
      schemasNode.set(MAP_SCHEMA_NAME, mapSchema);
    }
    return MAP_SCHEMA_NAME;
  }

  // If we already have this schema defined, don't define it again!
  String typeName = Types.getSimpleName(type, apiConfig.getSerializationConfig());
  JsonNode existing = schemasNode.get(typeName);
  if (existing != null && existing.isObject()) {
    return typeName;
  }

  ObjectNode schemaNode = objectMapper.createObjectNode();
  Class<?> c = type.getRawType();
  if (c.isEnum()) {
    schemasNode.set(typeName, schemaNode);
    schemaNode.put("id", typeName);
    schemaNode.put("type", "string");

    ArrayNode enumNode = objectMapper.createArrayNode();
    for (Object enumConstant : c.getEnumConstants()) {
      enumNode.add(enumConstant.toString());
    }
    schemaNode.set("enum", enumNode);
  } else {
    // JavaBean
    TypeToken<?> serializedType = ApiAnnotationIntrospector.getSchemaType(type, apiConfig);
    if (!type.equals(serializedType)) {
      return addTypeToSchema(schemasNode, serializedType, enclosingType, apiConfig,
          parameterConfigs);
    } else {
      addBeanTypeToSchema(schemasNode, typeName, schemaNode, type, apiConfig, parameterConfigs);
    }
  }
  return typeName;
}
 
Example 9
Source File: SameClassConverter.java    From beanmother with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canHandle(Object source, TypeToken<?> targetTypeToken) {
    return targetTypeToken.equals(TypeToken.of(source.getClass())) ||
            targetTypeToken.getRawType().isInstance(source);
}
 
Example 10
Source File: JavaOptionalConverterModule.java    From beanmother with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canHandle(Object source, TypeToken<?> targetTypeToken) {
    return stringToNumberConverter.canHandle(source, TypeToken.of(Long.class))
            && targetTypeToken.equals(TypeToken.of(OptionalLong.class));
}
 
Example 11
Source File: JavaOptionalConverterModule.java    From beanmother with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canHandle(Object source, TypeToken<?> targetTypeToken) {
    return stringToNumberConverter.canHandle(source, TypeToken.of(Double.class))
            && targetTypeToken.equals(TypeToken.of(OptionalDouble.class));
}
 
Example 12
Source File: GuavaOptionalConverterModule.java    From beanmother with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canHandle(Object source, TypeToken<?> targetTypeToken) {
    return numberConverter.canHandle(source, TypeToken.of(Integer.class))
            && targetTypeToken.equals(new TypeToken<Optional<Integer>>(){});
}
 
Example 13
Source File: JavaOptionalConverterModule.java    From beanmother with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canHandle(Object source, TypeToken<?> targetTypeToken) {
    return numberConverter.canHandle(source, TypeToken.of(Long.class))
            && targetTypeToken.equals(TypeToken.of(OptionalLong.class));
}
 
Example 14
Source File: JavaOptionalConverterModule.java    From beanmother with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canHandle(Object source, TypeToken<?> targetTypeToken) {
    return numberConverter.canHandle(source, TypeToken.of(Double.class))
            && targetTypeToken.equals(TypeToken.of(OptionalDouble.class));
}
 
Example 15
Source File: JavaOptionalConverterModule.java    From beanmother with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canHandle(Object source, TypeToken<?> targetTypeToken) {
    return numberConverter.canHandle(source, TypeToken.of(Integer.class))
            && targetTypeToken.equals(TypeToken.of(OptionalInt.class));
}
 
Example 16
Source File: GuavaOptionalConverterModule.java    From beanmother with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canHandle(Object source, TypeToken<?> targetTypeToken) {
    return stringToNumberConverter.canHandle(source, TypeToken.of(Double.class))
            && targetTypeToken.equals(new TypeToken<Optional<Double>>(){});
}
 
Example 17
Source File: GuavaOptionalConverterModule.java    From beanmother with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canHandle(Object source, TypeToken<?> targetTypeToken) {
    return stringToNumberConverter.canHandle(source, TypeToken.of(Long.class))
            && targetTypeToken.equals(new TypeToken<Optional<Long>>(){});
}
 
Example 18
Source File: GuavaOptionalConverterModule.java    From beanmother with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canHandle(Object source, TypeToken<?> targetTypeToken) {
    return stringToNumberConverter.canHandle(source, TypeToken.of(Integer.class))
            && targetTypeToken.equals(new TypeToken<Optional<Integer>>(){});
}
 
Example 19
Source File: GuavaOptionalConverterModule.java    From beanmother with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canHandle(Object source, TypeToken<?> targetTypeToken) {
    return numberConverter.canHandle(source, TypeToken.of(Double.class))
            && targetTypeToken.equals(new TypeToken<Optional<Double>>(){});
}
 
Example 20
Source File: GuavaOptionalConverterModule.java    From beanmother with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canHandle(Object source, TypeToken<?> targetTypeToken) {
    return numberConverter.canHandle(source, TypeToken.of(Long.class))
            && targetTypeToken.equals(new TypeToken<Optional<Long>>(){});
}