/** * {@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); }
/** * {@inheritDoc} */ public StackManipulation resolve(TypeDescription targetType, ByteCodeElement target, TypeList.Generic parameters, TypeDescription.Generic result) { List<StackManipulation> stackManipulations = new ArrayList<StackManipulation>(parameters.size()); for (int index = parameters.size() - 1; index >= 0; index--) { stackManipulations.add(Removal.of(parameters.get(index))); } return new StackManipulation.Compound(CompoundList.of(stackManipulations, DefaultValue.of(result.asErasure()))); } }
/** * {@inheritDoc} */ public MethodDescription resolve(TypeDescription targetType, ByteCodeElement target, TypeList.Generic parameters, TypeDescription.Generic result) { if (parameters.isEmpty()) { throw new IllegalStateException("Cannot substitute parameterless instruction with " + target); } else if (parameters.get(0).isPrimitive() || parameters.get(0).isArray()) { throw new IllegalStateException("Cannot invoke method on primitive or array type for " + target); } TypeDefinition typeDefinition = parameters.get(0); List<MethodDescription> candidates = CompoundList.<MethodDescription>of(methodGraphCompiler.compile(typeDefinition, instrumentedType) .listNodes() .asMethodList() .filter(matcher), typeDefinition.getDeclaredMethods().filter(isPrivate().<MethodDescription>and(isVisibleTo(instrumentedType)).and(matcher))); if (candidates.size() == 1) { return candidates.get(0); } else { throw new IllegalStateException("Not exactly one method that matches " + matcher + ": " + candidates); } } }
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); 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 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)); } }
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(); }
/** * {@inheritDoc} */ public StackManipulation resolve(TypeDescription targetType, ByteCodeElement target, TypeList.Generic parameters, TypeDescription.Generic result) { MethodDescription methodDescription = methodResolver.resolve(targetType, target, parameters, result); if (!methodDescription.isAccessibleTo(instrumentedType)) { throw new IllegalStateException(instrumentedType + " cannot access " + methodDescription); } TypeList.Generic mapped = methodDescription.isStatic() ? methodDescription.getParameters().asTypeList() : new TypeList.Generic.Explicit(CompoundList.of(methodDescription.getDeclaringType(), methodDescription.getParameters().asTypeList())); if (!methodDescription.getReturnType().asErasure().isAssignableTo(result.asErasure())) { throw new IllegalStateException("Cannot assign return value of " + methodDescription + " to " + result); } else if (mapped.size() != parameters.size()) { throw new IllegalStateException("Cannot invoke " + methodDescription + " on " + parameters); } for (int index = 0; index < mapped.size(); index++) { if (!mapped.get(index).asErasure().isAssignableTo(parameters.get(index).asErasure())) { throw new IllegalStateException("Cannot invoke " + methodDescription + " on " + parameters); } } return methodDescription.isVirtual() ? MethodInvocation.invoke(methodDescription).virtual(mapped.get(THIS_REFERENCE).asErasure()) : MethodInvocation.invoke(methodDescription); }
/** * {@inheritDoc} */ public StackManipulation resolve(TypeDescription targetType, ByteCodeElement target, TypeList.Generic parameters, TypeDescription.Generic result) { FieldDescription fieldDescription = fieldResolver.resolve(targetType, target, parameters, result); if (!fieldDescription.isAccessibleTo(instrumentedType)) { throw new IllegalStateException(instrumentedType + " cannot access " + fieldDescription); } else if (result.represents(void.class)) { if (parameters.size() != (fieldDescription.isStatic() ? 1 : 2)) { throw new IllegalStateException("Cannot set " + fieldDescription + " with " + parameters); } else if (!fieldDescription.isStatic() && !parameters.get(0).asErasure().isAssignableTo(fieldDescription.getDeclaringType().asErasure())) { throw new IllegalStateException("Cannot set " + fieldDescription + " on " + parameters.get(0)); } else if (!parameters.get(fieldDescription.isStatic() ? 0 : 1).asErasure().isAssignableTo(fieldDescription.getType().asErasure())) { throw new IllegalStateException("Cannot set " + fieldDescription + " to " + parameters.get(fieldDescription.isStatic() ? 0 : 1)); } return FieldAccess.forField(fieldDescription).write(); } else { if (parameters.size() != (fieldDescription.isStatic() ? 0 : 1)) { throw new IllegalStateException("Cannot set " + fieldDescription + " with " + parameters); } else if (!fieldDescription.isStatic() && !parameters.get(0).asErasure().isAssignableTo(fieldDescription.getDeclaringType().asErasure())) { throw new IllegalStateException("Cannot get " + fieldDescription + " on " + parameters.get(0)); } else if (!fieldDescription.getType().asErasure().isAssignableTo(result.asErasure())) { throw new IllegalStateException("Cannot get " + fieldDescription + " as " + result); } return FieldAccess.forField(fieldDescription).read(); } }
/** * {@inheritDoc} */ public FieldDescription resolve(TypeDescription targetType, ByteCodeElement target, TypeList.Generic parameters, TypeDescription.Generic result) { if (parameters.isEmpty()) { throw new IllegalStateException("Cannot substitute parameterless instruction with " + target); } else if (parameters.get(0).isPrimitive() || parameters.get(0).isArray()) { throw new IllegalStateException("Cannot access field on primitive or array type for " + target); } TypeDefinition current = parameters.get(0); do { FieldList<?> fields = current.getDeclaredFields().filter(not(isStatic()).<FieldDescription>and(isVisibleTo(instrumentedType)).and(matcher)); if (fields.size() == 1) { return fields.getOnly(); } else if (fields.size() > 1) { throw new IllegalStateException("Ambiguous field location of " + fields); } current = current.getSuperClass(); } while (current != null); throw new IllegalStateException("Cannot locate field matching " + matcher + " on " + targetType); } }
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(); }
/** * {@inheritDoc} */ public Generic findBindingOf(Generic typeVariable) { Generic typeDescription = this; do { TypeList.Generic typeArguments = typeDescription.getTypeArguments(), typeVariables = typeDescription.asErasure().getTypeVariables(); for (int index = 0; index < Math.min(typeArguments.size(), typeVariables.size()); index++) { if (typeVariable.equals(typeVariables.get(index))) { return typeArguments.get(index); } } typeDescription = typeDescription.getOwnerType(); } while (typeDescription != null && typeDescription.getSort().isParameterized()); return Generic.UNDEFINED; }
/** * {@inheritDoc} */ public Generic get(int index) { return new ClassLoadingTypeProjection(delegate.get(index), classLoader, classLoadingDelegate); }
@Override @CachedReturnPlugin.Enhance("resolved") protected Generic resolve() { return delegate.resolve().getInterfaces().get(index); } }
/** * {@inheritDoc} */ public TypeDescription asErasure() { TypeList.Generic upperBounds = getUpperBounds(); return upperBounds.isEmpty() ? TypeDescription.OBJECT : upperBounds.get(0).asErasure(); }
/** * {@inheritDoc} */ public TypeDescription onTypeVariable(Generic typeVariable) { for (TypeVariableToken typeVariableToken : typeVariableTokens) { if (typeVariable.getSymbol().equals(typeVariableToken.getSymbol())) { return typeVariableToken.getBounds().get(0).accept(this); } } return TargetType.resolve(declaringType.findVariable(typeVariable.getSymbol()).asErasure(), declaringType); }
/** * {@inheritDoc} */ public Generic getType() { return signatureResolution.resolveParameterTypes(parameterTypeDescriptors, typePool, parameterTypeAnnotationTokens, LazyMethodDescription.this).get(index); }
/** * {@inheritDoc} */ public Generic get(int index) { return new LazyInterfaceType(delegate, index, rawInterfaces.get(index)); }