Java Code Examples for net.bytebuddy.description.annotation.AnnotationDescription#Loadable

The following examples show how to use net.bytebuddy.description.annotation.AnnotationDescription#Loadable . 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: SimpleMethodSignatureOffsetMappingFactory.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@Override
public Advice.OffsetMapping make(ParameterDescription.InDefinedShape target,
                                 AnnotationDescription.Loadable<SimpleMethodSignature> annotation,
                                 AdviceType adviceType) {
    return new Advice.OffsetMapping() {
        @Override
        public Target resolve(TypeDescription instrumentedType, MethodDescription instrumentedMethod, Assigner assigner,
                              Advice.ArgumentHandler argumentHandler, Sort sort) {
            final String className = instrumentedMethod.getDeclaringType().getTypeName();
            String simpleClassName = className.substring(className.lastIndexOf('$') + 1);
            simpleClassName = simpleClassName.substring(simpleClassName.lastIndexOf('.') + 1);
            final String signature = String.format("%s#%s", simpleClassName, instrumentedMethod.getName());
            return Target.ForStackManipulation.of(signature);
        }
    };
}
 
Example 2
Source File: BiDirectionalAssociationHandler.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private static String getMappedByNotManyToMany(FieldDescription target) {
	try {
		AnnotationDescription.Loadable<OneToOne> oto = EnhancerImpl.getAnnotation( target, OneToOne.class );
		if ( oto != null ) {
			return oto.getValue( new MethodDescription.ForLoadedMethod( OneToOne.class.getDeclaredMethod( "mappedBy" ) ) ).resolve( String.class );
		}

		AnnotationDescription.Loadable<OneToMany> otm = EnhancerImpl.getAnnotation( target, OneToMany.class );
		if ( otm != null ) {
			return otm.getValue( new MethodDescription.ForLoadedMethod( OneToMany.class.getDeclaredMethod( "mappedBy" ) ) ).resolve( String.class );
		}

		AnnotationDescription.Loadable<ManyToMany> mtm = EnhancerImpl.getAnnotation( target, ManyToMany.class );
		if ( mtm != null ) {
			return mtm.getValue( new MethodDescription.ForLoadedMethod( ManyToMany.class.getDeclaredMethod( "mappedBy" ) ) ).resolve( String.class );
		}
	}
	catch (NoSuchMethodException ignored) {
	}

	return null;
}
 
Example 3
Source File: TargetMethodAnnotationDrivenBinder.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public ParameterBinding<?> bind(AnnotationDescription.Loadable<S> annotation,
                                MethodDescription source,
                                ParameterDescription target,
                                Implementation.Target implementationTarget,
                                Assigner assigner,
                                Assigner.Typing typing) {
    if (!declaringType(annotation).represents(void.class)) {
        if (declaringType(annotation).isPrimitive() || declaringType(annotation).isArray()) {
            throw new IllegalStateException("A primitive type or array type cannot declare a field: " + source);
        } else if (!implementationTarget.getInstrumentedType().isAssignableTo(declaringType(annotation))) {
            return MethodDelegationBinder.ParameterBinding.Illegal.INSTANCE;
        }
    }
    FieldLocator fieldLocator = declaringType(annotation).represents(void.class)
            ? new FieldLocator.ForClassHierarchy(implementationTarget.getInstrumentedType())
            : new FieldLocator.ForExactType(declaringType(annotation), implementationTarget.getInstrumentedType());
    FieldLocator.Resolution resolution = fieldName(annotation).equals(BEAN_PROPERTY)
            ? resolveAccessor(fieldLocator, source)
            : fieldLocator.locate(fieldName(annotation));
    return resolution.isResolved() && !(source.isStatic() && !resolution.getField().isStatic())
            ? bind(resolution.getField(), annotation, source, target, implementationTarget, assigner)
            : ParameterBinding.Illegal.INSTANCE;
}
 
Example 4
Source File: BindingPriorityResolverTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testRightPrioritized() throws Exception {
    AnnotationDescription.Loadable<BindingPriority> lowPriority = AnnotationDescription.ForLoadedAnnotation.of(this.lowPriority);
    when(leftAnnotations.ofType(BindingPriority.class)).thenReturn(lowPriority);
    AnnotationDescription.Loadable<BindingPriority> highPriority = AnnotationDescription.ForLoadedAnnotation.of(this.highPriority);
    when(rightAnnotations.ofType(BindingPriority.class)).thenReturn(highPriority);
    assertThat(BindingPriority.Resolver.INSTANCE.resolve(source, left, right), is(MethodDelegationBinder.AmbiguityResolver.Resolution.RIGHT));
}
 
Example 5
Source File: AnnotationValueOffsetMappingFactory.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Override
public Advice.OffsetMapping make(final ParameterDescription.InDefinedShape target,
                                 final AnnotationDescription.Loadable<AnnotationValueExtractor> annotation,
                                 final AdviceType adviceType) {
    return new Advice.OffsetMapping() {
        @Override
        public Target resolve(TypeDescription instrumentedType, MethodDescription instrumentedMethod, Assigner assigner, Advice.ArgumentHandler argumentHandler, Sort sort) {
            return Target.ForStackManipulation.of(getAnnotationValue(instrumentedMethod, annotation.load()));
        }
    };
}
 
Example 6
Source File: BindingPriorityResolverTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testLeftPrioritized() throws Exception {
    AnnotationDescription.Loadable<BindingPriority> highPriority = AnnotationDescription.ForLoadedAnnotation.of(this.highPriority);
    when(leftAnnotations.ofType(BindingPriority.class)).thenReturn(highPriority);
    AnnotationDescription.Loadable<BindingPriority> lowPriority = AnnotationDescription.ForLoadedAnnotation.of(this.lowPriority);
    when(rightAnnotations.ofType(BindingPriority.class)).thenReturn(lowPriority);
    assertThat(BindingPriority.Resolver.INSTANCE.resolve(source, left, right), is(MethodDelegationBinder.AmbiguityResolver.Resolution.LEFT));
}
 
Example 7
Source File: ByteBuddyTutorialExamplesTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
public MethodDelegationBinder.ParameterBinding<?> bind(AnnotationDescription.Loadable<StringValue> annotation,
                                                       MethodDescription source,
                                                       ParameterDescription target,
                                                       Implementation.Target implementationTarget,
                                                       Assigner assigner,
                                                       Assigner.Typing typing) {
    if (!target.getType().asErasure().represents(String.class)) {
        throw new IllegalStateException(target + " makes wrong use of StringValue");
    }
    StackManipulation constant = new TextConstant(annotation.load().value());
    return new MethodDelegationBinder.ParameterBinding.Anonymous(constant);
}
 
Example 8
Source File: TargetMethodAnnotationDrivenBinder.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a handler for a given annotation.
 *
 * @param target          The target parameter being handled.
 * @param parameterBinder The parameter binder that should process an annotation.
 * @param annotation      An annotation instance that can be understood by this parameter binder.
 * @param typing          The typing to apply.
 * @return A handler for processing the given annotation.
 */
@SuppressWarnings("unchecked")
protected static Handler of(ParameterDescription target,
                            ParameterBinder<?> parameterBinder,
                            AnnotationDescription annotation,
                            Assigner.Typing typing) {
    return new Bound<Annotation>(target,
            (ParameterBinder<Annotation>) parameterBinder,
            (AnnotationDescription.Loadable<Annotation>) annotation.prepare(parameterBinder.getHandledType()),
            typing);
}
 
Example 9
Source File: TargetMethodAnnotationDrivenBinder.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new bound handler.
 *
 * @param target          The target parameter being handled.
 * @param parameterBinder The parameter binder that is actually responsible for binding the parameter.
 * @param annotation      The annotation value that lead to the binding of this handler.
 * @param typing          The typing to apply.
 */
protected Bound(ParameterDescription target,
                ParameterBinder<T> parameterBinder,
                AnnotationDescription.Loadable<T> annotation,
                Assigner.Typing typing) {
    this.target = target;
    this.parameterBinder = parameterBinder;
    this.annotation = annotation;
    this.typing = typing;
}
 
Example 10
Source File: BiDirectionalAssociationHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static TypeDescription.Generic target(FieldDescription persistentField) {
	AnnotationDescription.Loadable<Access> access = persistentField.getDeclaringType().asErasure().getDeclaredAnnotations().ofType( Access.class );
	if ( access != null && access.loadSilent().value() == AccessType.FIELD ) {
		return persistentField.getType();
	}
	else {
		MethodDescription getter = EnhancerImpl.getterOf( persistentField );
		if ( getter == null ) {
			return persistentField.getType();
		}
		else {
			return getter.getReturnType();
		}
	}
}
 
Example 11
Source File: JaxRsOffsetMappingFactory.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Override
public Advice.OffsetMapping make(ParameterDescription.InDefinedShape target, AnnotationDescription.Loadable<JaxRsPath> annotation, AdviceType adviceType) {
    return new Advice.OffsetMapping() {
        @Override
        public Target resolve(TypeDescription instrumentedType, MethodDescription instrumentedMethod, Assigner assigner, Advice.ArgumentHandler argumentHandler, Sort sort) {
            Object value = null;
            if (useAnnotationValueForTransactionName) {
                value = getTransactionAnnotationValueFromAnnotations(instrumentedMethod, instrumentedType);
            }
            return Target.ForStackManipulation.of(value);
        }
    };
}
 
Example 12
Source File: TargetMethodAnnotationDrivenBinder.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
@Override
protected Object bind(AnnotationDescription.Loadable<U> annotation, MethodDescription source, ParameterDescription target) {
    return value;
}
 
Example 13
Source File: TargetMethodAnnotationDrivenBinder.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public ParameterBinding<?> bind(AnnotationDescription.Loadable<S> annotation,
                                MethodDescription source,
                                ParameterDescription target,
                                Implementation.Target implementationTarget,
                                Assigner assigner,
                                Assigner.Typing typing) {
    Object value = bind(annotation, source, target);
    if (value == null) {
        return new ParameterBinding.Anonymous(DefaultValue.of(target.getType()));
    }
    StackManipulation stackManipulation;
    TypeDescription suppliedType;
    if (value instanceof Boolean) {
        stackManipulation = IntegerConstant.forValue((Boolean) value);
        suppliedType = TypeDescription.ForLoadedType.of(boolean.class);
    } else if (value instanceof Byte) {
        stackManipulation = IntegerConstant.forValue((Byte) value);
        suppliedType = TypeDescription.ForLoadedType.of(byte.class);
    } else if (value instanceof Short) {
        stackManipulation = IntegerConstant.forValue((Short) value);
        suppliedType = TypeDescription.ForLoadedType.of(short.class);
    } else if (value instanceof Character) {
        stackManipulation = IntegerConstant.forValue((Character) value);
        suppliedType = TypeDescription.ForLoadedType.of(char.class);
    } else if (value instanceof Integer) {
        stackManipulation = IntegerConstant.forValue((Integer) value);
        suppliedType = TypeDescription.ForLoadedType.of(int.class);
    } else if (value instanceof Long) {
        stackManipulation = LongConstant.forValue((Long) value);
        suppliedType = TypeDescription.ForLoadedType.of(long.class);
    } else if (value instanceof Float) {
        stackManipulation = FloatConstant.forValue((Float) value);
        suppliedType = TypeDescription.ForLoadedType.of(float.class);
    } else if (value instanceof Double) {
        stackManipulation = DoubleConstant.forValue((Double) value);
        suppliedType = TypeDescription.ForLoadedType.of(double.class);
    } else if (value instanceof String) {
        stackManipulation = new TextConstant((String) value);
        suppliedType = TypeDescription.STRING;
    } else if (value instanceof Class) {
        stackManipulation = ClassConstant.of(TypeDescription.ForLoadedType.of((Class<?>) value));
        suppliedType = TypeDescription.CLASS;
    } else if (value instanceof TypeDescription) {
        stackManipulation = ClassConstant.of((TypeDescription) value);
        suppliedType = TypeDescription.CLASS;
    } else if (JavaType.METHOD_HANDLE.isInstance(value)) {
        stackManipulation = new JavaConstantValue(JavaConstant.MethodHandle.ofLoaded(value));
        suppliedType = JavaType.METHOD_HANDLE.getTypeStub();
    } else if (value instanceof JavaConstant.MethodHandle) {
        stackManipulation = new JavaConstantValue((JavaConstant.MethodHandle) value);
        suppliedType = JavaType.METHOD_HANDLE.getTypeStub();
    } else if (JavaType.METHOD_TYPE.isInstance(value)) {
        stackManipulation = new JavaConstantValue(JavaConstant.MethodType.ofLoaded(value));
        suppliedType = JavaType.METHOD_HANDLE.getTypeStub();
    } else if (value instanceof JavaConstant.MethodType) {
        stackManipulation = new JavaConstantValue((JavaConstant.MethodType) value);
        suppliedType = JavaType.METHOD_HANDLE.getTypeStub();
    } else {
        throw new IllegalStateException("Not able to save in class's constant pool: " + value);
    }
    return new ParameterBinding.Anonymous(new StackManipulation.Compound(
            stackManipulation,
            assigner.assign(suppliedType.asGenericType(), target.getType(), typing)
    ));
}
 
Example 14
Source File: BiDirectionalAssociationHandler.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public static TypeDescription getTargetEntityClass(TypeDescription managedCtClass, FieldDescription persistentField) {
	try {
		AnnotationDescription.Loadable<OneToOne> oto = EnhancerImpl.getAnnotation( persistentField, OneToOne.class );
		AnnotationDescription.Loadable<OneToMany> otm = EnhancerImpl.getAnnotation( persistentField, OneToMany.class );
		AnnotationDescription.Loadable<ManyToOne> mto = EnhancerImpl.getAnnotation( persistentField, ManyToOne.class );
		AnnotationDescription.Loadable<ManyToMany> mtm = EnhancerImpl.getAnnotation( persistentField, ManyToMany.class );

		if ( oto == null && otm == null && mto == null && mtm == null ) {
			return null;
		}

		AnnotationValue<?, ?> targetClass = null;
		if ( oto != null ) {
			targetClass = oto.getValue( new MethodDescription.ForLoadedMethod( OneToOne.class.getDeclaredMethod( "targetEntity" ) ) );
		}
		if ( otm != null ) {
			targetClass = otm.getValue( new MethodDescription.ForLoadedMethod( OneToMany.class.getDeclaredMethod( "targetEntity" ) ) );
		}
		if ( mto != null ) {
			targetClass = mto.getValue( new MethodDescription.ForLoadedMethod( ManyToOne.class.getDeclaredMethod( "targetEntity" ) ) );
		}
		if ( mtm != null ) {
			targetClass = mtm.getValue( new MethodDescription.ForLoadedMethod( ManyToMany.class.getDeclaredMethod( "targetEntity" ) ) );
		}

		if ( targetClass == null ) {
			log.infof(
					"Could not find type of bi-directional association for field [%s#%s]",
					managedCtClass.getName(),
					persistentField.getName()
			);
			return null;
		}
		else if ( !targetClass.resolve( TypeDescription.class ).represents( void.class ) ) {
			return targetClass.resolve( TypeDescription.class );
		}
	}
	catch (NoSuchMethodException ignored) {
	}

	return entityType( target( persistentField ) );
}
 
Example 15
Source File: TargetMethodAnnotationDrivenBinder.java    From byte-buddy with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a parameter binding for the given target parameter.
 *
 * @param fieldDescription     The field for which this binder binds a value.
 * @param annotation           The annotation that was cause for the delegation to this argument binder.
 * @param source               The intercepted source method.
 * @param target               Tge target parameter that is subject to be bound to
 *                             intercepting the {@code source} method.
 * @param implementationTarget The target of the current implementation that is subject to this binding.
 * @param assigner             An assigner that can be used for applying the binding.
 * @return A parameter binding for the requested target method parameter.
 */
protected abstract ParameterBinding<?> bind(FieldDescription fieldDescription,
                                            AnnotationDescription.Loadable<S> annotation,
                                            MethodDescription source,
                                            ParameterDescription target,
                                            Implementation.Target implementationTarget,
                                            Assigner assigner);
 
Example 16
Source File: TargetMethodAnnotationDrivenBinder.java    From byte-buddy with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a parameter binding for the given target parameter.
 *
 * @param annotation           The annotation that was cause for the delegation to this argument binder.
 * @param source               The intercepted source method.
 * @param target               Tge target parameter that is subject to be bound to
 *                             intercepting the {@code source} method.
 * @param implementationTarget The target of the current implementation that is subject to this binding.
 * @param assigner             An assigner that can be used for applying the binding.
 * @param typing               The typing to apply.
 * @return A parameter binding for the requested target method parameter.
 */
ParameterBinding<?> bind(AnnotationDescription.Loadable<T> annotation,
                         MethodDescription source,
                         ParameterDescription target,
                         Implementation.Target implementationTarget,
                         Assigner assigner,
                         Assigner.Typing typing);
 
Example 17
Source File: TargetMethodAnnotationDrivenBinder.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Resolves a value for the given annotation on a parameter that is processed by a {@link net.bytebuddy.implementation.MethodDelegation}.
 *
 * @param annotation The annotation that triggered this binding.
 * @param source     The method for which a delegation is currently bound.
 * @param target     The parameter for which a value is bound.
 * @return The constant pool value that is bound to this parameter or {@code null} for binding this value.
 */
protected abstract Object bind(AnnotationDescription.Loadable<S> annotation, MethodDescription source, ParameterDescription target);
 
Example 18
Source File: TargetMethodAnnotationDrivenBinder.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Extracts the field name from an annotation.
 *
 * @param annotation The annotation from which to extract the field name.
 * @return The field name defined by the handled annotation.
 */
protected abstract String fieldName(AnnotationDescription.Loadable<S> annotation);
 
Example 19
Source File: TargetMethodAnnotationDrivenBinder.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Extracts the declaring type from an annotation.
 *
 * @param annotation The annotation from which to extract the declaring type.
 * @return The declaring type defined by the handled annotation.
 */
protected abstract TypeDescription declaringType(AnnotationDescription.Loadable<S> annotation);