private static String getMappedByNotManyToMany(AnnotatedFieldDescription target) { try { AnnotationDescription.Loadable<OneToOne> oto = target.getAnnotation( OneToOne.class ); if ( oto != null ) { return oto.getValue( new MethodDescription.ForLoadedMethod( OneToOne.class.getDeclaredMethod( "mappedBy" ) ) ).resolve( String.class ); } AnnotationDescription.Loadable<OneToMany> otm = target.getAnnotation( OneToMany.class ); if ( otm != null ) { return otm.getValue( new MethodDescription.ForLoadedMethod( OneToMany.class.getDeclaredMethod( "mappedBy" ) ) ).resolve( String.class ); } AnnotationDescription.Loadable<ManyToMany> mtm = target.getAnnotation( ManyToMany.class ); if ( mtm != null ) { return mtm.getValue( new MethodDescription.ForLoadedMethod( ManyToMany.class.getDeclaredMethod( "mappedBy" ) ) ).resolve( String.class ); } } catch (NoSuchMethodException ignored) { } return null; }
/** * {@inheritDoc} */ @SuppressWarnings("deprecation") // Due to isAccessible() call. @SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "Exception should always be wrapped for clarity") public AnnotationValue<?, ?> getValue(MethodDescription.InDefinedShape property) { if (!property.getDeclaringType().represents(annotation.annotationType())) { throw new IllegalArgumentException(property + " does not represent " + annotation.annotationType()); } try { boolean accessible = property.getDeclaringType().isPublic(); // method is required to be public Method method = property instanceof MethodDescription.ForLoadedMethod ? ((MethodDescription.ForLoadedMethod) property).getLoadedMethod() : null; if (method == null || method.getDeclaringClass() != annotation.annotationType() || (!accessible && !method.isAccessible())) { method = annotation.annotationType().getMethod(property.getName()); if (!accessible) { AccessController.doPrivileged(new SetAccessibleAction<Method>(method)); } } return asValue(method.invoke(annotation), method.getReturnType()); } catch (InvocationTargetException exception) { throw new IllegalStateException("Error reading annotation property " + property, exception.getCause()); } catch (Exception exception) { throw new IllegalStateException("Cannot access annotation property " + property, exception); } }
@Override public boolean isOverridden(Object instance, Method origin) { SoftReference<MethodGraph> reference = graphs.get(instance.getClass()); MethodGraph methodGraph = reference == null ? null : reference.get(); if (methodGraph == null) { methodGraph = compiler.compile(new TypeDescription.ForLoadedType(instance.getClass())); graphs.put(instance.getClass(), new SoftReference<MethodGraph>(methodGraph)); } MethodGraph.Node node = methodGraph.locate(new MethodDescription.ForLoadedMethod(origin).asSignatureToken()); return !node.getSort().isResolved() || !node.getRepresentative().asDefined().getDeclaringType().represents(origin.getDeclaringClass()); }
for (Method method : delegate.getClass() .getDeclaredMethods()) { if (new MethodDescription.ForLoadedMethod(method) .asSignatureToken() .equals(target.asSignatureToken())) {
MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(getLongMethod)), MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(putLongMethod)) );
return Match(type).<StackManipulation>of(API.Case( $(t -> t.represents(boolean.class)), MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(BOOLEAN_VALUEOF)) ), Case( $(t -> t.represents(byte.class)), MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(BYTE_VALUEOF)) ), Case( $(t -> t.represents(char.class)), MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(CHAR_VALUEOF)) ), Case( $(t -> t.represents(short.class)), MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(SHORT_VALUEOF)) ), Case( $(t -> t.represents(int.class)), MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(INT_VALUEOF)) ), Case( $(t -> t.represents(long.class)), MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(LONG_VALUEOF)) ), Case( $(t -> t.represents(float.class)), MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(FLOAT_VALUEOF)) ), Case(
targetClass = oto.getValue( new MethodDescription.ForLoadedMethod( OneToOne.class.getDeclaredMethod( "targetEntity" ) ) ); targetClass = otm.getValue( new MethodDescription.ForLoadedMethod( OneToMany.class.getDeclaredMethod( "targetEntity" ) ) ); targetClass = mto.getValue( new MethodDescription.ForLoadedMethod( ManyToOne.class.getDeclaredMethod( "targetEntity" ) ) ); targetClass = mtm.getValue( new MethodDescription.ForLoadedMethod( ManyToMany.class.getDeclaredMethod( "targetEntity" ) ) );
MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(getMethod)), MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(putMethod)) );
Method method = methodDescription.getLoadedMethod();
private static String getMappedByNotManyToMany(AnnotatedFieldDescription target) { try { AnnotationDescription.Loadable<OneToOne> oto = target.getAnnotation( OneToOne.class ); if ( oto != null ) { return oto.getValue( new MethodDescription.ForLoadedMethod( OneToOne.class.getDeclaredMethod( "mappedBy" ) ) ).resolve( String.class ); } AnnotationDescription.Loadable<OneToMany> otm = target.getAnnotation( OneToMany.class ); if ( otm != null ) { return otm.getValue( new MethodDescription.ForLoadedMethod( OneToMany.class.getDeclaredMethod( "mappedBy" ) ) ).resolve( String.class ); } AnnotationDescription.Loadable<ManyToMany> mtm = target.getAnnotation( ManyToMany.class ); if ( mtm != null ) { return mtm.getValue( new MethodDescription.ForLoadedMethod( ManyToMany.class.getDeclaredMethod( "mappedBy" ) ) ).resolve( String.class ); } } catch (NoSuchMethodException ignored) { } return null; }
private static <InT, OutT> MethodDescription processMethodTypeFromSpec(StreamingLedgerSpec<InT, OutT> spec) { Class<? extends TransactionProcessFunction> processClass = spec.processFunction.getClass(); Iterator<Method> methods = Methods.findAnnotatedMethods(processClass, ProcessTransaction.class); if (!methods.hasNext()) { throw new IllegalArgumentException("Unable to find an annotated method on " + processClass.getSimpleName()); } Method method = methods.next(); if (methods.hasNext()) { throw new IllegalArgumentException( "Was expecting a single method annotated with a ProcessTransaction, but found more."); } return new ForLoadedMethod(method); }
@Test void testMatchInSuperInterface() throws Exception { assertThat(CustomElementMatchers.overridesOrImplementsMethodThat(isAnnotatedWith(FindMe.class)) .matches(new MethodDescription.ForLoadedMethod(TestClass.class.getDeclaredMethod("findInSuperInterface")))) .isTrue(); }
@Test void testMatchInSuperClass_NotMatchedBySuperClassMatcher() throws Exception { assertThat(CustomElementMatchers .overridesOrImplementsMethodThat(isAnnotatedWith(FindMe.class)) .onSuperClassesThat(not(nameContains("Super"))) .matches(new MethodDescription.ForLoadedMethod(TestClass.class.getDeclaredMethod("findInSuperClass")))) .isFalse(); }
@Test void testMatchInInterface() throws Exception { assertThat(CustomElementMatchers.overridesOrImplementsMethodThat(isAnnotatedWith(FindMe.class)) .matches(new MethodDescription.ForLoadedMethod(TestClass.class.getDeclaredMethod("findInInterface")))) .isTrue(); }
private void assertMatches(MethodMatcher methodMatcher, Method method) { assertThat(method).isNotNull(); assertThat(methodMatcher).isNotNull(); final TraceMethodInstrumentation methodMatcherInstrumentation = new TraceMethodInstrumentation(methodMatcher); assertThat(methodMatcherInstrumentation.getTypeMatcher().matches(TypeDescription.ForLoadedType.of(method.getDeclaringClass()))).isTrue(); assertThat(methodMatcherInstrumentation.getMethodMatcher().matches(new MethodDescription.ForLoadedMethod(method))).isTrue(); } }
private void assertDoesNotMatch(MethodMatcher methodMatcher, Method method) { assertThat(method).isNotNull(); assertThat(methodMatcher).isNotNull(); final TraceMethodInstrumentation methodMatcherInstrumentation = new TraceMethodInstrumentation(methodMatcher); assertThat( methodMatcherInstrumentation.getTypeMatcher().matches(TypeDescription.ForLoadedType.of(method.getDeclaringClass())) && methodMatcherInstrumentation.getMethodMatcher().matches(new MethodDescription.ForLoadedMethod(method))) .isFalse(); }
@Override public final StackManipulation stackManipulation() { return new StackManipulation.Compound( MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(OBJECTS_HASH)) ); } }
@Override public Size apply(final MethodVisitor mv, final Context implementationContext) { List<StackManipulation> loadTargetParameters = loadArgumentsFromVariable(); List<StackManipulation> constructorSignature = loadSignatureParametersClasess(); return new Compound( ClassConstant.of(targetType), ArrayFactory.forType(TypeDescription.OBJECT.asGenericType()).withValues(loadTargetParameters), ArrayFactory.forType(TypeDescription.CLASS.asGenericType()).withValues(constructorSignature), MethodInvocation.invoke(new ForLoadedMethod(getawayMethod)) ).apply(mv, implementationContext); } }
@Test void testMatchInInterfaceOfSuperClass() throws Exception { assertThat(CustomElementMatchers.overridesOrImplementsMethodThat(isAnnotatedWith(FindMe.class)) .matches(new MethodDescription.ForLoadedMethod(TestClass.class.getDeclaredMethod("findInInterfaceOfSuperClass")))) .isTrue(); }
@Test void testMatchInSuperClass() throws Exception { assertThat(CustomElementMatchers .overridesOrImplementsMethodThat(isAnnotatedWith(FindMe.class)) .onSuperClassesThat(nameContains("Super")) .matches(new MethodDescription.ForLoadedMethod(TestClass.class.getDeclaredMethod("findInSuperClass")))) .isTrue(); }