private <T> AnnotatedElementBuilder exposeInternal(Key<T> key) { if (privateElements == null) { addError( "Cannot expose %s on a standard binder. " + "Exposed bindings are only applicable to private binders.", key); return new AnnotatedElementBuilder() { @Override public void annotatedWith(Class<? extends Annotation> annotationType) {} @Override public void annotatedWith(Annotation annotation) {} }; } ExposureBuilder<T> builder = new ExposureBuilder<T>(this, getElementSource(), MoreTypes.canonicalizeKey(key)); privateElements.addExposureBuilder(builder); return builder; }
@Override public boolean justInTimeResolution(Injectee injectee) { Type type = injectee.getRequiredType(); Class<?> clazz = MoreTypes.getRawType(type); if (clazz != null) { Binding<?> binding = findBinding(injectee); if (binding != null) { Key<?> key = binding.getKey(); Set<Annotation> qualifiers = BindingUtils.getQualifiers(key); GuiceBindingDescriptor<?> descriptor = new GuiceBindingDescriptor<>( type, clazz, qualifiers, binding); ServiceLocatorUtilities.addOneDescriptor(locator, descriptor); return true; } } return false; }
/** * Register a Java type and produces an equivalent RAML type. It returns an existing RAML type if * the Java type was registered already. * * @param type Java type. * @return RAML type. */ public RamlType define(Type type) { if (types == null) { types = new LinkedHashMap<>(); } Type componentType = componentType(type); String typeName = MoreTypes.getRawType(componentType).getSimpleName(); RamlType ramlType = RamlType.valueOf(typeName); if (ramlType.isObject()) { RamlType existing = types.get(typeName); if (existing == null) { ModelConverters converter = ModelConverters.getInstance(); Property property = converter.readAsProperty(componentType); Map<PropertyBuilder.PropertyId, Object> args = new EnumMap<>( PropertyBuilder.PropertyId.class); for (Map.Entry<String, Model> entry : converter.readAll(componentType).entrySet()) { define(entry.getKey(), entry.getValue()); } ramlType = define(typeName, PropertyBuilder.toModel(PropertyBuilder.merge(property, args))); } else { ramlType = existing; } } return type != componentType ? ramlType.toArray() : ramlType; }
private Type extractBuilderType() { Class<?> builderClass = extractBuilderClass(); if (builderClass == null) { return null; } if (builderClass.getTypeParameters().length == 0) { return builderClass; } if (!(structType instanceof ParameterizedType)) { metadataErrors.addError("Builder class '%s' may only be generic if the type it builds ('%s') is also generic", builderClass.getName(), getStructClass().getName()); return builderClass; } if (builderClass.getTypeParameters().length != getStructClass().getTypeParameters().length) { metadataErrors.addError( "Generic builder class '%s' must have the same number of type parameters as the type it builds ('%s')", builderClass.getName(), getStructClass().getName()); return builderClass; } ParameterizedType parameterizedStructType = (ParameterizedType) structType; return new MoreTypes.ParameterizedTypeImpl(builderClass.getEnclosingClass(), builderClass, parameterizedStructType.getActualTypeArguments()); }
/** * Unsafe. Constructs a key from a manually specified type. */ @SuppressWarnings("unchecked") private Key(Type type, AnnotationStrategy annotationStrategy) { this.annotationStrategy = annotationStrategy; this.typeLiteral = MoreTypes .canonicalizeForKey((TypeLiteral<T>) TypeLiteral.get(type)); this.hashCode = computeHashCode(); this.toStringSupplier = createToStringSupplier(); }
/** Constructs a key from a manually specified type. */ private Key(TypeLiteral<T> typeLiteral, AnnotationStrategy annotationStrategy) { this.annotationStrategy = annotationStrategy; this.typeLiteral = MoreTypes.canonicalizeForKey(typeLiteral); this.hashCode = computeHashCode(); this.toStringSupplier = createToStringSupplier(); }
public void testToString() { Assert.assertEquals("java.lang.String", MoreTypes.typeToString(String.class)); assertEquals("java.util.Set<java.lang.String>[][]", MoreTypes.typeToString(setStringArray)); assertEquals("java.util.Map<java.lang.String, java.lang.Integer>", MoreTypes.typeToString(mapStringInteger)); assertEquals("java.util.List<java.util.Set<java.lang.String>[][]>", MoreTypes.typeToString(listSetStringArray)); assertEquals(innerFloatDouble.toString(), MoreTypes.typeToString(innerFloatDouble)); }
/** * Returns the method's declaring type and name in the format used in * javadoc, e.g. {@code com.bar.Foo#baz(com.bar.Foo, com.bar.Bar)}, with * resolved type parameters. * * @return string representation for this method including the declaring type */ @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(getName()).append("("); List<String> parameters = new ArrayList<String>(); for (Type parameterType : getRawParameterTypes()) { // TODO(schmitt): We are not respecting varargs here. parameters.add(MoreTypes.typeToString(parameterType)); } sb.append(join(", ", parameters)).append(")"); return String.format("%s#%s", getDeclaringType(), sb.toString()); }
/** * Constructs a new type literal. Derives represented class from type parameter. * * <p>Clients create an empty anonymous subclass. Doing so embeds the type parameter in the * anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure. */ @SuppressWarnings("unchecked") protected TypeLiteral() { this.type = getSuperclassTypeParameter(getClass()); this.rawType = (Class<? super T>) MoreTypes.getRawType(type); this.hashCode = type.hashCode(); }
/** Unsafe. Constructs a type literal manually. */ @SuppressWarnings("unchecked") TypeLiteral(Type type) { this.type = canonicalize(checkNotNull(type, "type")); this.rawType = (Class<? super T>) MoreTypes.getRawType(this.type); this.hashCode = this.type.hashCode(); }
/** Unsafe. Constructs a key from a manually specified type. */ @SuppressWarnings("unchecked") private Key(Type type, AnnotationStrategy annotationStrategy) { this.annotationStrategy = annotationStrategy; this.typeLiteral = MoreTypes.canonicalizeForKey((TypeLiteral<T>) TypeLiteral.get(type)); this.hashCode = computeHashCode(); }
@Override public <T> MembersInjector<T> getMembersInjector(final TypeLiteral<T> typeLiteral) { final MembersInjectorLookup<T> element = new MembersInjectorLookup<T>( getElementSource(), MoreTypes.canonicalizeForKey(typeLiteral)); elements.add(element); return element.getMembersInjector(); }
public void testToString() { Assert.assertEquals("java.lang.String", MoreTypes.typeToString(String.class)); assertEquals("java.util.Set<java.lang.String>[][]", MoreTypes.typeToString(setStringArray)); assertEquals( "java.util.Map<java.lang.String, java.lang.Integer>", MoreTypes.typeToString(mapStringInteger)); assertEquals( "java.util.List<java.util.Set<java.lang.String>[][]>", MoreTypes.typeToString(listSetStringArray)); assertEquals(innerFloatDouble.toString(), MoreTypes.typeToString(innerFloatDouble)); }
/** * Unsafe. Constructs a type literal manually. */ @SuppressWarnings("unchecked") TypeLiteral(Type type) { this.type = canonicalize(checkNotNull(type, "type")); this.rawType = (Class<? super T>) MoreTypes.getRawType(this.type); this.hashCode = this.type.hashCode(); }
/** * Unsafe. Constructs a key from a manually specified type. */ @SuppressWarnings("unchecked") private Key(Type type, AnnotationStrategy annotationStrategy) { this.annotationStrategy = annotationStrategy; this.typeLiteral = MoreTypes.canonicalizeForKey((TypeLiteral<T>) TypeLiteral.get(type)); this.hashCode = computeHashCode(); this.toStringSupplier = createToStringSupplier(); }
/** * List of enum values when the {@link #type()} represents an enum. * * @return Enum values. */ public List<String> enums() { return Optional.ofNullable(MoreTypes.getRawType(type)) .map(Class::getEnumConstants) .filter(Objects::nonNull) .map(values -> Arrays.asList(values).stream() .map(value -> ((Enum) value).name()) .collect(Collectors.toList()) ).orElse(Collections.emptyList()); }
private Type componentType(final Type type) { Class<?> rawType = MoreTypes.getRawType(type); if (rawType.isArray()) { return rawType.getComponentType(); } if (Collection.class.isAssignableFrom(rawType) && type instanceof ParameterizedType) { return ((ParameterizedType) type).getActualTypeArguments()[0]; } return type; }
static java.lang.reflect.Type simplifyType(final java.lang.reflect.Type type) { Class<?> rawType = MoreTypes.getRawType(type); if (Primitives.isWrapperType(rawType)) { return Primitives.unwrap(rawType); } return type; }
/** * Unsafe. Constructs a key from a manually specified type. */ @SuppressWarnings("unchecked") private Key(Type type, AnnotationStrategy annotationStrategy) { this.annotationStrategy = annotationStrategy; this.typeLiteral = MoreTypes.canonicalizeForKey((TypeLiteral<T>) TypeLiteral.get(type)); this.hashCode = computeHashCode(); }
@Override public final boolean equals(Object o) { return o instanceof TypeLiteral<?> && MoreTypes.equals(type, ((TypeLiteral) o).type); }
@Override public final String toString() { return MoreTypes.typeToString(type); }
/** Constructs a key from a manually specified type. */ private Key(TypeLiteral<T> typeLiteral, AnnotationStrategy annotationStrategy) { this.annotationStrategy = annotationStrategy; this.typeLiteral = MoreTypes.canonicalizeForKey(typeLiteral); this.hashCode = computeHashCode(); }
@Override public <T> void requestInjection(TypeLiteral<T> type, T instance) { elements.add( new InjectionRequest<T>( getElementSource(), MoreTypes.canonicalizeForKey(type), instance)); }
@Override public <T> AnnotatedBindingBuilder<T> bind(Key<T> key) { BindingBuilder<T> builder = new BindingBuilder<T>(this, elements, getElementSource(), MoreTypes.canonicalizeKey(key)); return builder; }
private Object paramValue(final ClassLoader loader, final ClassNode owner, final MethodNode method, final AbstractInsnNode n) { if (n instanceof LdcInsnNode) { Object cst = ((LdcInsnNode) n).cst; if (cst instanceof Type) { boolean typePresent = new Insn<>(method, n) .next() .filter(is(MethodInsnNode.class)) .findFirst() .map(MethodInsnNode.class::cast) .filter(mutantToSomething().or(getOrCreateKotlinClass())) .isPresent(); if (typePresent) { return null; } return loadType(loader, ((Type) cst).getClassName()); } return cst; } else if (n instanceof InsnNode) { InsnNode insn = (InsnNode) n; switch (insn.getOpcode()) { case Opcodes.ICONST_0: return 0; case Opcodes.ICONST_1: return 1; case Opcodes.ICONST_2: return 2; case Opcodes.ICONST_3: return 3; case Opcodes.ICONST_4: return 4; case Opcodes.ICONST_5: return 5; case Opcodes.LCONST_0: return 0L; case Opcodes.LCONST_1: return 1L; case Opcodes.FCONST_0: return 0f; case Opcodes.FCONST_1: return 1f; case Opcodes.FCONST_2: return 2f; case Opcodes.DCONST_0: return 0d; case Opcodes.DCONST_1: return 1d; case Opcodes.ICONST_M1: return -1; case Opcodes.ACONST_NULL: return null; } } else if (n instanceof IntInsnNode) { IntInsnNode intinsn = (IntInsnNode) n; return intinsn.operand; } else if (n instanceof FieldInsnNode) { // toEnum FieldInsnNode finsn = (FieldInsnNode) n; if (finsn.getOpcode() == GETSTATIC) { java.lang.reflect.Type possibleEnum = loadType(loader, finsn.owner); if (MoreTypes.getRawType(possibleEnum).isEnum()) { return finsn.name; } } } return n; }
/** * Constructs a new key. Derives the type from this class's type parameter. * * <p> * Clients create an empty anonymous subclass. Doing so embeds the type * parameter in the anonymous class's type hierarchy so we can reconstitute * it at runtime despite erasure. * * <p> * Example usage for a binding of type {@code Foo} annotated with * {@code @Bar}: * * <p> * {@code new Key<Foo>(Bar.class) }. */ @SuppressWarnings("unchecked") protected Key(Class<? extends Annotation> annotationType) { this.annotationStrategy = strategyFor(annotationType); this.typeLiteral = MoreTypes .canonicalizeForKey((TypeLiteral<T>) TypeLiteral .fromSuperclassTypeParameter(getClass())); this.hashCode = computeHashCode(); this.toStringSupplier = createToStringSupplier(); }
/** * Constructs a new key. Derives the type from this class's type parameter. * * <p> * Clients create an empty anonymous subclass. Doing so embeds the type * parameter in the anonymous class's type hierarchy so we can reconstitute * it at runtime despite erasure. * * <p> * Example usage for a binding of type {@code Foo} annotated with * {@code @Bar}: * * <p> * {@code new Key<Foo>(new Bar()) }. */ @SuppressWarnings("unchecked") protected Key(Annotation annotation) { // no usages, not test-covered this.annotationStrategy = strategyFor(annotation); this.typeLiteral = MoreTypes .canonicalizeForKey((TypeLiteral<T>) TypeLiteral .fromSuperclassTypeParameter(getClass())); this.hashCode = computeHashCode(); this.toStringSupplier = createToStringSupplier(); }
/** * Constructs a new key. Derives the type from this class's type parameter. * * <p> * Clients create an empty anonymous subclass. Doing so embeds the type * parameter in the anonymous class's type hierarchy so we can reconstitute * it at runtime despite erasure. * * <p> * Example usage for a binding of type {@code Foo}: * * <p> * {@code new Key<Foo>() }. */ @SuppressWarnings("unchecked") protected Key() { this.annotationStrategy = NullAnnotationStrategy.INSTANCE; this.typeLiteral = MoreTypes .canonicalizeForKey((TypeLiteral<T>) TypeLiteral .fromSuperclassTypeParameter(getClass())); this.hashCode = computeHashCode(); this.toStringSupplier = createToStringSupplier(); }
/** * Builds a TypeLiteral of a Map using the key and value types * <pre> * SIDE NOTE: This is necessary since the {@link TypeLiteral} type's public API does NOT expose * the TypeLiteral(type) constructor * </pre> * @param keyType map's key type * @param valueType map's value type * @return the {@link TypeLiteral} */ public static <K,V> TypeLiteral<Map<K,V>> mapOf(final Class<K> keyType,final Class<V> valueType) { TypeLiteral<Map<K,V>> typeLiteral = new TypeLiteral<Map<K,V>>(new MoreTypes.ParameterizedTypeImpl(null, Map.class, keyType,valueType)); return typeLiteral; }
/** * Builds a TypeLiteral of a Map using the key and value types * (this method is used when the key or value types are generics * <pre> * SIDE NOTE: This is necessary since the {@link TypeLiteral} type's public API does NOT expose * the TypeLiteral(type) constructor * </pre> * @param keyType map's key type * @param valueType map's value type * @return the {@link TypeLiteral} */ public static <K,V> TypeLiteral<Map<K,V>> mapOf(final TypeLiteral<K> keyType,final TypeLiteral<V> valueType) { TypeLiteral<Map<K,V>> typeLiteral = new TypeLiteral<Map<K,V>>(new MoreTypes.ParameterizedTypeImpl(null, Map.class, keyType.getType(),valueType.getType())); return typeLiteral; }
/** * Constructs a new key. Derives the type from this class's type parameter. * * <p>Clients create an empty anonymous subclass. Doing so embeds the type parameter in the * anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure. * * <p>Example usage for a binding of type {@code Foo} annotated with {@code @Bar}: * * <p>{@code new Key<Foo>(Bar.class) {}}. */ @SuppressWarnings("unchecked") protected Key(Class<? extends Annotation> annotationType) { this.annotationStrategy = strategyFor(annotationType); this.typeLiteral = MoreTypes.canonicalizeForKey( (TypeLiteral<T>) TypeLiteral.fromSuperclassTypeParameter(getClass())); this.hashCode = computeHashCode(); }