List of usage examples for com.google.common.primitives Primitives allPrimitiveTypes
public static Set<Class<?>> allPrimitiveTypes()
From source file:org.akraievoy.base.proxies.Proxies.java
/** * @param interf interface class to create proxy for * @return a proxy that silently ignores all method calls */// w w w . ja va 2 s .co m @SuppressWarnings("unchecked") public static <E> E noop(Class<E> interf) { return (E) Proxy.newProxyInstance(interf.getClassLoader(), new Class[] { interf }, new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Class<?> resultType = method.getReturnType(); if (Primitives.allPrimitiveTypes().contains(resultType)) { return Defaults.defaultValue(resultType); } return null; } }); }
From source file:com.google.errorprone.bugpatterns.threadsafety.WellKnownMutability.java
private static ImmutableMap<String, ImmutableAnnotationInfo> getBootstrapClasses() { return new Builder().addAll(Primitives.allPrimitiveTypes()).addAll(Primitives.allWrapperTypes()) .add(java.lang.annotation.Annotation.class).add(java.lang.Class.class).add(java.lang.String.class) .add(java.math.BigDecimal.class).add(java.net.InetAddress.class).add(java.util.Locale.class) .add(java.util.regex.Pattern.class).add("java.util.Optional", "T").add("org.joda.time.DateTime") .add("org.joda.time.DateTimeZone").add("org.joda.time.Duration").add("org.joda.time.Instant") .add("org.joda.time.LocalDate").add("org.joda.time.LocalDateTime").add("org.joda.time.Period") .add("org.joda.time.format.DateTimeFormatter").add(com.google.common.primitives.UnsignedLong.class) .add(com.google.common.base.Converter.class).add("com.google.protobuf.ByteString") .add("com.google.protobuf.Descriptors$Descriptor") .add("com.google.protobuf.Descriptors$EnumDescriptor") .add("com.google.protobuf.Descriptors$EnumValueDescriptor") .add("com.google.protobuf.Descriptors$FieldDescriptor") .add("com.google.protobuf.Descriptors$FileDescriptor") .add("com.google.protobuf.Descriptors$ServiceDescriptor") .add("com.google.protobuf.ExtensionRegistry$ExtensionInfo").add("com.google.common.net.MediaType") .add("com.google.common.net.Uri").add("com.google.protobuf.Extension") .add("com.google.re2j.Pattern").add(com.google.common.collect.ImmutableCollection.class, "E") .add(com.google.common.collect.ImmutableSet.class, "E") .add(com.google.common.collect.ImmutableList.class, "E") .add(com.google.common.collect.ImmutableMultiset.class, "E") .add(com.google.common.collect.ImmutableMap.class, "K", "V") .add(com.google.common.collect.ImmutableBiMap.class, "K", "V") .add(com.google.common.collect.ImmutableMultimap.class, "K", "V") .add(com.google.common.collect.ImmutableListMultimap.class, "K", "V") .add(com.google.common.collect.ImmutableSetMultimap.class, "K", "V") .add(com.google.common.collect.ImmutableSortedMap.class, "K", "V") .add(com.google.common.collect.ImmutableRangeMap.class, "K", "V") .add(com.google.common.collect.ImmutableTable.class, "R", "C", "V") .add(com.google.common.base.Optional.class, "T").add(com.google.common.base.Splitter.class) .add(com.google.common.base.Joiner.class).add(com.google.common.collect.Range.class, "C") .add("android.net.Uri").add("java.time.Duration").add("java.time.Instant") .add("java.time.LocalDate").add("java.time.LocalDateTime").add("java.time.LocalTime") .add("java.time.MonthDay").add("java.time.OffsetDateTime").add("java.time.OffsetTime") .add("java.time.Period").add("java.time.Year").add("java.time.YearMonth") .add("java.time.ZonedDateTime").add("java.time.ZoneId").add("java.time.ZoneOffset") .add("java.time.chrono.AbstractChronology").add("java.time.chrono.ChronoLocalDate") .add("java.time.chrono.ChronoLocalDateTime", "D").add("java.time.chrono.Chronology") .add("java.time.chrono.ChronoPeriod").add("java.time.chrono.ChronoZonedDateTime", "D") .add("java.time.chrono.Era").add("java.time.chrono.HijrahChronology") .add("java.time.chrono.HijrahDate").add("java.time.chrono.IsoChronology") .add("java.time.chrono.JapaneseChronology").add("java.time.chrono.JapaneseDate") .add("java.time.chrono.JapaneseEra").add("java.time.chrono.MinguoChronology") .add("java.time.chrono.MinguoDate").add("java.time.chrono.ThaiBuddhistChronology") .add("java.time.chrono.ThaiBuddhistDate").add("java.time.format.DateTimeFormatter") .add("java.time.format.DecimalStyle").add("java.time.temporal.TemporalField") .add("java.time.temporal.TemporalUnit").add("java.time.temporal.ValueRange") .add("java.time.temporal.WeekFields").add("java.time.zone.ZoneOffsetTransition") .add("java.time.zone.ZoneOffsetTransitionRule").add("java.time.zone.ZoneRules") .add("java.time.zone.ZoneRulesProvider").build(); }
From source file:net.derquinse.common.meta.InterfaceMetaBuilderFactory.java
private static <T extends WithMetaClass> MetaField<? super T, ?> checkReturnType(Method method, MetaField<? super T, ?> field, TypeToken<?> fieldType) { TypeToken<?> returnType = Invokable.from(method).getReturnType(); // Check for primitive types if (Primitives.allPrimitiveTypes().contains(returnType.getRawType())) { returnType = TypeToken.of(Primitives.wrap(returnType.getRawType())); }/*from w w w. j av a 2 s. c o m*/ if (returnType.isAssignableFrom(fieldType)) { return field; } return null; }
From source file:org.openflexo.antar.binding.TypeUtils.java
public static boolean isPrimitive(@Nullable Type type) { return type != null && Primitives.allPrimitiveTypes().contains(type); }
From source file:org.apache.brooklyn.util.javalang.coerce.PrimitiveStringTypeCoercions.java
public static boolean isPrimitiveOrBoxer(Class<?> type) { // cf Boxing.isPrimitiveOrBoxerClass return Primitives.allPrimitiveTypes().contains(type) || Primitives.allWrapperTypes().contains(type); }
From source file:de.ks.flatadocdb.defaults.ReflectionLuceneDocumentExtractor.java
private boolean isValidBaseType(Class<?> type) { boolean validType = Primitives.allPrimitiveTypes().contains(Primitives.unwrap(type)); validType = validType || type.equals(String.class); validType = validType || type.equals(LocalDate.class); validType = validType || type.equals(LocalTime.class); validType = validType || type.equals(LocalDateTime.class); validType = validType || Enum.class.isAssignableFrom(type); return validType; }
From source file:org.apache.brooklyn.util.javalang.coerce.PrimitiveStringTypeCoercions.java
@SuppressWarnings("unchecked") public static <T> T stringToPrimitive(String value, Class<T> targetType) { assert Primitives.allPrimitiveTypes().contains(targetType) || Primitives.allWrapperTypes().contains(targetType) : "targetType=" + targetType; // If char, then need to do explicit conversion if (targetType == Character.class || targetType == char.class) { if (value.length() == 1) { return (T) (Character) value.charAt(0); } else if (value.length() != 1) { throw new ClassCoercionException("Cannot coerce type String to " + targetType.getCanonicalName() + " (" + value + "): adapting failed"); }/*from w w w . jav a 2 s. c om*/ } value = value.trim(); // For boolean we could use valueOf, but that returns false whereas we'd rather throw errors on bad values if (targetType == Boolean.class || targetType == boolean.class) { if ("true".equalsIgnoreCase(value)) return (T) Boolean.TRUE; if ("false".equalsIgnoreCase(value)) return (T) Boolean.FALSE; if ("yes".equalsIgnoreCase(value)) return (T) Boolean.TRUE; if ("no".equalsIgnoreCase(value)) return (T) Boolean.FALSE; if ("t".equalsIgnoreCase(value)) return (T) Boolean.TRUE; if ("f".equalsIgnoreCase(value)) return (T) Boolean.FALSE; if ("y".equalsIgnoreCase(value)) return (T) Boolean.TRUE; if ("n".equalsIgnoreCase(value)) return (T) Boolean.FALSE; throw new ClassCoercionException("Cannot coerce type String to " + targetType.getCanonicalName() + " (" + value + "): adapting failed"); } // Otherwise can use valueOf reflectively Class<?> wrappedType; if (Primitives.allPrimitiveTypes().contains(targetType)) { wrappedType = Primitives.wrap(targetType); } else { wrappedType = targetType; } try { return (T) wrappedType.getMethod("valueOf", String.class).invoke(null, value); } catch (Exception e) { ClassCoercionException tothrow = new ClassCoercionException( "Cannot coerce " + JavaStringEscapes.wrapJavaString(value) + " to " + targetType.getCanonicalName() + " (" + value + "): adapting failed"); tothrow.initCause(e); throw tothrow; } }
From source file:com.google.template.soy.jbcsrc.JbcSrcValueFactory.java
/** Returns true if the clazz is allowed as a parameter type for the given soy type. */ private boolean isValidClassForType(Class<?> clazz, SoyType type) { // Exit early if the class is primitive and the type is nullable -- that's not allowed. // Then remove null from the type. This allows us to accept precise params for nullable // types, e.g, for int|null we can allow IntegerData (which will be passed as 'null'). if (SoyTypes.isNullable(type) && Primitives.allPrimitiveTypes().contains(clazz)) { return false; }//from w w w . j a v a 2 s. co m type = SoyTypes.tryRemoveNull(type); switch (type.getKind()) { case ANY: case UNKNOWN: return UNKNOWN_TYPES.contains(clazz); case ATTRIBUTES: case CSS: case HTML: case URI: case TRUSTED_RESOURCE_URI: case JS: return SANITIZED_TYPES.contains(clazz); case BOOL: return BOOL_TYPES.contains(clazz); case FLOAT: return FLOAT_TYPES.contains(clazz); case INT: return INT_TYPES.contains(clazz); case LEGACY_OBJECT_MAP: return LEGACY_OBJECT_MAP_TYPES.contains(clazz); case LIST: return LIST_TYPES.contains(clazz); case MAP: return MAP_TYPES.contains(clazz); case RECORD: return RECORD_TYPES.contains(clazz); case STRING: return STRING_TYPES.contains(clazz); case NULL: return NULL_TYPES.contains(clazz); case PROTO: return PROTO_TYPES.contains(clazz) || matchesProtoDescriptor(Message.class, clazz, ((SoyProtoType) type).getDescriptor()); case PROTO_ENUM: return PROTO_ENUM_TYPES.contains(clazz) || (clazz.isEnum() && matchesProtoDescriptor(ProtocolMessageEnum.class, clazz, ((SoyProtoEnumType) type).getDescriptor())); case UNION: // If this is a union, make sure the type is valid for every member. // If the type isn't valid for any member, then there's no guarantee this will work // for an arbitrary template at runtime. for (SoyType member : ((UnionType) type).getMembers()) { if (!isValidClassForType(clazz, member)) { return false; } } return true; case ERROR: throw new IllegalStateException("Cannot have error type from function signature"); } throw new AssertionError("above switch is exhaustive"); }
From source file:brooklyn.util.flags.TypeCoercions.java
public static boolean isPrimitiveOrBoxer(Class<?> type) { return Primitives.allPrimitiveTypes().contains(type) || Primitives.allWrapperTypes().contains(type); }
From source file:brooklyn.util.flags.TypeCoercions.java
@SuppressWarnings("unchecked") public static <T> T stringToPrimitive(String value, Class<T> targetType) { assert Primitives.allPrimitiveTypes().contains(targetType) || Primitives.allWrapperTypes().contains(targetType) : "targetType=" + targetType; // If char, then need to do explicit conversion if (targetType == Character.class || targetType == char.class) { if (value.length() == 1) { return (T) (Character) value.charAt(0); } else if (value.length() != 1) { throw new ClassCoercionException("Cannot coerce type String to " + targetType.getCanonicalName() + " (" + value + "): adapting failed"); }//w ww . jav a 2 s .co m } value = value.trim(); // For boolean we could use valueOf, but that returns false whereas we'd rather throw errors on bad values if (targetType == Boolean.class || targetType == boolean.class) { if ("true".equalsIgnoreCase(value)) return (T) Boolean.TRUE; if ("false".equalsIgnoreCase(value)) return (T) Boolean.FALSE; if ("yes".equalsIgnoreCase(value)) return (T) Boolean.TRUE; if ("no".equalsIgnoreCase(value)) return (T) Boolean.FALSE; if ("t".equalsIgnoreCase(value)) return (T) Boolean.TRUE; if ("f".equalsIgnoreCase(value)) return (T) Boolean.FALSE; if ("y".equalsIgnoreCase(value)) return (T) Boolean.TRUE; if ("n".equalsIgnoreCase(value)) return (T) Boolean.FALSE; throw new ClassCoercionException("Cannot coerce type String to " + targetType.getCanonicalName() + " (" + value + "): adapting failed"); } // Otherwise can use valueOf reflectively Class<?> wrappedType; if (Primitives.allPrimitiveTypes().contains(targetType)) { wrappedType = Primitives.wrap(targetType); } else { wrappedType = targetType; } try { return (T) wrappedType.getMethod("valueOf", String.class).invoke(null, value); } catch (Exception e) { ClassCoercionException tothrow = new ClassCoercionException("Cannot coerce type String to " + targetType.getCanonicalName() + " (" + value + "): adapting failed"); tothrow.initCause(e); throw tothrow; } }