/** * {@inheritDoc} */ public boolean isBridgeCompatible(TypeToken typeToken) { List<TypeDescription> types = getParameters().asTypeList().asErasures(), bridgeTypes = typeToken.getParameterTypes(); if (types.size() != bridgeTypes.size()) { return false; } for (int index = 0; index < types.size(); index++) { if (!types.get(index).equals(bridgeTypes.get(index)) && (types.get(index).isPrimitive() || bridgeTypes.get(index).isPrimitive())) { return false; } } TypeDescription returnType = getReturnType().asErasure(), bridgeReturnType = typeToken.getReturnType(); return returnType.equals(bridgeReturnType) || (!returnType.isPrimitive() && !bridgeReturnType.isPrimitive()); }
/** * {@inheritDoc} */ public WithFlexibleName withTypeVariables(ElementMatcher<? super Generic> matcher, Transformer<TypeVariableToken> transformer) { List<TypeVariableToken> typeVariables = new ArrayList<TypeVariableToken>(this.typeVariables.size()); int index = 0; for (TypeVariableToken typeVariableToken : this.typeVariables) { typeVariables.add(matcher.matches(getTypeVariables().get(index++)) ? transformer.transform(this, typeVariableToken) : typeVariableToken); } return new Default(name, modifiers, superClass, typeVariables, interfaceTypes, fieldTokens, methodTokens, annotationDescriptions, typeInitializer, loadedTypeInitializer, declaringType, enclosingMethod, enclosingType, declaredTypes, anonymousClass, localClass, nestHost, nestMembers); }
if (!typeVariables.isEmpty() && isAssignableTo(Throwable.class)) { throw new IllegalStateException("Cannot define throwable " + this + " to be generic");
TypeList.Generic upperBounds = typeArgument.getUpperBounds(); if (lowerBounds.size() > 0) { if (upperBounds.size() != 1 || !upperBounds.get(0).represents(Object.class)) { throw new IllegalArgumentException(String.format( "Lower bounded type argument '%s' must have Object as upper bound", typeArgument)); appendReferenceTypeSignature(typeArgument.getLowerBounds().get(0), Nullness.UNDEFINED, output); else if (upperBounds.size() > 0) { if (upperBounds.size() > 1) { throw new IllegalArgumentException( String.format("Upper bounded type argument '%s' cannot have more than one upper bound", if (upperBounds.get(0).represents(Object.class)) { output.append('*'); appendReferenceTypeSignature(upperBounds.get(0), Nullness.UNDEFINED, output);
TypeList.Generic upperBounds = typeArgument.getUpperBounds(); if (lowerBounds.size() > 0) { if (upperBounds.size() != 1 || !upperBounds.get(0).represents(Object.class)) { throw new IllegalArgumentException(String.format( "Lower bounded type argument '%s' must have Object as upper bound", typeArgument)); appendReferenceTypeSignature(typeArgument.getLowerBounds().get(0), Nullness.UNDEFINED, output); else if (upperBounds.size() > 0) { if (upperBounds.size() > 1) { throw new IllegalArgumentException( String.format("Upper bounded type argument '%s' cannot have more than one upper bound", if (upperBounds.get(0).represents(Object.class)) { output.append('*'); appendReferenceTypeSignature(upperBounds.get(0), Nullness.UNDEFINED, output);
/** * Append a type parameter * * @param typeParameter Type parameters to append * @param output Output to append to * @param <A> Type of output * @return Supplied output to append to */ public <A extends Appendable> A appendTypeParameter(Generic typeParameter, A output) { try { output.append(typeParameter.getSymbol()); TypeList.Generic upperBounds = typeParameter.getUpperBounds(); if (upperBounds.size() == 0) { throw new IllegalArgumentException( String.format("Type parameter '%s' must have upper bounds", typeParameter)); } if (upperBounds.get(0).isInterface()) { output.append(':'); } for (Generic upperBound : upperBounds) { appendClassOrInterfaceBound(upperBound, output); } return output; } catch (Exception e) { throw new RuntimeException(String.format("Could not write type parameter %s", typeParameter)); } }
/** * Append a method signature * * @param method Method to append signature of * @param output Output to append to * @param <A> Type of output * @return Supplied output to append to */ public <A extends Appendable> A appendMethodSignature(MethodDescription method, A output) { try { if (!method.getTypeVariables().isEmpty()) { appendTypeParameters(method.getTypeVariables(), output); } output.append('('); for (ParameterDescription parameter : method.getParameters()) { Nullness nullable = this.nullability.getParameterNullability(parameter); appendJavaTypeSignature(parameter.getType(), nullable, output); } output.append(')'); Nullness nullable = this.nullability.getReturnValueNullability(method); appendJavaTypeSignature(method.getReturnType(), nullable, output); return output; } catch (Exception e) { throw new RuntimeException(String.format("Could not write method signature %s", method)); } }
/** * Append a type parameter * * @param typeParameter Type parameters to append * @param output Output to append to * @param <A> Type of output * @return Supplied output to append to */ public <A extends Appendable> A appendTypeParameter(Generic typeParameter, A output) { try { output.append(typeParameter.getSymbol()); TypeList.Generic upperBounds = typeParameter.getUpperBounds(); if (upperBounds.size() == 0) { throw new IllegalArgumentException( String.format("Type parameter '%s' must have upper bounds", typeParameter)); } if (upperBounds.get(0).isInterface()) { output.append(':'); } for (Generic upperBound : upperBounds) { appendClassOrInterfaceBound(upperBound, output); } return output; } catch (Exception e) { throw new RuntimeException(String.format("Could not write type parameter %s", typeParameter)); } }
/** * Append a method signature * * @param method Method to append signature of * @param output Output to append to * @param <A> Type of output * @return Supplied output to append to */ public <A extends Appendable> A appendMethodSignature(MethodDescription method, A output) { try { if (!method.getTypeVariables().isEmpty()) { appendTypeParameters(method.getTypeVariables(), output); } output.append('('); for (ParameterDescription parameter : method.getParameters()) { Nullness nullable = this.nullability.getParameterNullability(parameter); appendJavaTypeSignature(parameter.getType(), nullable, output); } output.append(')'); Nullness nullable = this.nullability.getReturnValueNullability(method); appendJavaTypeSignature(method.getReturnType(), nullable, output); return output; } catch (Exception e) { throw new RuntimeException(String.format("Could not write method signature %s", method)); } }
/** * Append a type signature * * @param buf Buffer to append to * @param role Role (class or super) * @param type Type to append signature of * @param annotatedSignatureWriter Signature writer * @return <code>true</code> if any nullability information was written * @throws IOException if we stumble and fall... */ protected boolean appendTypeSignature(StringBuilder buf, String role, Generic type, MethodSignatureWriter annotatedSignatureWriter) throws IOException { boolean annotated = false; buf.append(role).append(' ').append(type.asErasure().getInternalName()).append("\n"); TypeList.Generic typeVariables = type.asErasure().getTypeVariables(); if (!typeVariables.isEmpty()) { if (appendTypeParameters(buf, typeVariables, annotatedSignatureWriter)) { annotated = true; } } return annotated; }
/** * Append a type signature * * @param buf Buffer to append to * @param role Role (class or super) * @param type Type to append signature of * @param annotatedSignatureWriter Signature writer * @return <code>true</code> if any nullability information was written * @throws IOException if we stumble and fall... */ protected boolean appendTypeSignature(StringBuilder buf, String role, Generic type, MethodSignatureWriter annotatedSignatureWriter) throws IOException { boolean annotated = false; buf.append(role).append(' ').append(type.asErasure().getInternalName()).append("\n"); TypeList.Generic typeVariables = type.asErasure().getTypeVariables(); if (!typeVariables.isEmpty()) { if (appendTypeParameters(buf, typeVariables, annotatedSignatureWriter)) { annotated = true; } } return annotated; }
@Override public final boolean matches(final TypeDescription target) { return target .getInterfaces() .isEmpty(); } }
private static TypeDescription entityType(TypeDescription.Generic type) { if ( type.getSort().isParameterized() ) { if ( type.asErasure().isAssignableTo( Collection.class ) ) { return type.getTypeArguments().get( 0 ).asErasure(); } if ( type.asErasure().isAssignableTo( Map.class ) ) { return type.getTypeArguments().get( 1 ).asErasure(); } } return type.asErasure(); }
if (!exceptionTypes.isEmpty()) { stringBuilder.append(" throws "); first = true;
generic = generic || !returnType.getSort().isNonGeneric(); TypeList.Generic exceptionTypes = getExceptionTypes(); if (!exceptionTypes.filter(not(ofSort(TypeDefinition.Sort.NON_GENERIC))).isEmpty()) { for (TypeDescription.Generic exceptionType : exceptionTypes) { exceptionType.accept(new TypeDescription.Generic.Visitor.ForSignatureVisitor(signatureWriter.visitExceptionType()));
/** * Creates a binder by installing a single proxy type where annotating a parameter with {@link FieldProxy} allows * getting and setting values for a given field. * * @param typeDescription A type which declares exactly one abstract getter and an abstract setter for the {@link Object} * type. The type is allowed to be generic. * @return A binder for the {@link FieldProxy} annotation. */ public static TargetMethodAnnotationDrivenBinder.ParameterBinder<FieldProxy> install(TypeDescription typeDescription) { if (!typeDescription.isInterface()) { throw new IllegalArgumentException(typeDescription + " is not an interface"); } else if (!typeDescription.getInterfaces().isEmpty()) { throw new IllegalArgumentException(typeDescription + " must not extend other interfaces"); } else if (!typeDescription.isPublic()) { throw new IllegalArgumentException(typeDescription + " is not public"); } MethodList<MethodDescription.InDefinedShape> methodCandidates = typeDescription.getDeclaredMethods().filter(isAbstract()); if (methodCandidates.size() != 2) { throw new IllegalArgumentException(typeDescription + " does not declare exactly two non-abstract methods"); } MethodList<MethodDescription.InDefinedShape> getterCandidates = methodCandidates.filter(isGetter(Object.class)); if (getterCandidates.size() != 1) { throw new IllegalArgumentException(typeDescription + " does not declare a getter with an Object type"); } MethodList<MethodDescription.InDefinedShape> setterCandidates = methodCandidates.filter(isSetter(Object.class)); if (setterCandidates.size() != 1) { throw new IllegalArgumentException(typeDescription + " does not declare a setter with an Object type"); } return new Binder(typeDescription, getterCandidates.getOnly(), setterCandidates.getOnly()); }
/** * Creates a parameterized type. * * @param rawType A raw version of the type to describe as a parameterized type. * @param ownerType The owner type of the parameterized type. * @param parameters The type arguments to attach to the raw type as parameters. * @return A builder for creating a parameterized type. */ public static Builder parameterizedType(TypeDescription rawType, Generic ownerType, Collection<? extends TypeDefinition> parameters) { TypeDescription declaringType = rawType.getDeclaringType(); if (ownerType == null && declaringType != null && rawType.isStatic()) { ownerType = declaringType.asGenericType(); } if (!rawType.represents(TargetType.class)) { if (!rawType.isGenerified()) { throw new IllegalArgumentException(rawType + " is not a parameterized type"); } else if (ownerType == null && declaringType != null && !rawType.isStatic()) { throw new IllegalArgumentException(rawType + " requires an owner type"); } else if (ownerType != null && !ownerType.asErasure().equals(declaringType)) { throw new IllegalArgumentException(ownerType + " does not represent required owner for " + rawType); } else if (ownerType != null && (rawType.isStatic() ^ ownerType.getSort().isNonGeneric())) { throw new IllegalArgumentException(ownerType + " does not define the correct parameters for owning " + rawType); } else if (rawType.getTypeVariables().size() != parameters.size()) { throw new IllegalArgumentException(parameters + " does not contain number of required parameters for " + rawType); } } return new Builder.OfParameterizedType(rawType, ownerType, new TypeList.Generic.Explicit(new ArrayList<TypeDefinition>(parameters))); }
/** {@inheritDoc} */ public InstrumentedType.WithFlexibleName represent(TypeDescription typeDescription) { return new InstrumentedType.Default(typeDescription.getName(), typeDescription.getModifiers(), typeDescription.getSuperClass(), typeDescription.getTypeVariables().asTokenList(is(typeDescription)), typeDescription.getInterfaces().accept(Generic.Visitor.Substitutor.ForDetachment.of(typeDescription)), typeDescription.getDeclaredFields().asTokenList(is(typeDescription)), typeDescription.getDeclaredMethods().asTokenList(is(typeDescription)), typeDescription.getDeclaredAnnotations(), TypeInitializer.None.INSTANCE, LoadedTypeInitializer.NoOp.INSTANCE, typeDescription.getDeclaringType(), typeDescription.getEnclosingMethod(), typeDescription.getEnclosingType(), typeDescription.getDeclaredTypes(), typeDescription.isAnonymousType(), typeDescription.isLocalType(), typeDescription.isNestHost() ? TargetType.DESCRIPTION : typeDescription.getNestHost(), typeDescription.isNestHost() ? typeDescription.getNestMembers().filter(not(is(typeDescription))) : Collections.<TypeDescription>emptyList()); } },