/** * {@inheritDoc} */ public TerminationHandler make(TypeDescription instrumentedType) { TypeDefinition current = instrumentedType; do { FieldList<?> candidates = current.getDeclaredFields().filter(isAccessibleTo(instrumentedType).and(matcher)); if (candidates.size() == 1) { return new FieldSetting(candidates.getOnly()); } else if (candidates.size() == 2) { throw new IllegalStateException(matcher + " is ambigous and resolved: " + candidates); } current = current.getSuperClass(); } while (current != null); throw new IllegalStateException(matcher + " does not locate any accessible fields for " + instrumentedType); } }
@Override protected FieldList<?> locate(ElementMatcher<? super FieldDescription> matcher) { for (TypeDefinition typeDefinition : typeDescription) { FieldList<?> candidates = typeDefinition.getDeclaredFields().filter(matcher); if (!candidates.isEmpty()) { return candidates; } } return new FieldList.Empty<FieldDescription>(); }
TypePool.Resolution resolution = typePool.describe(owner.replace('/', '.')); if (resolution.isResolved()) { FieldList<FieldDescription.InDefinedShape> candidates = resolution.resolve().getDeclaredFields().filter(strict ? ElementMatchers.<FieldDescription>named(internalName).and(hasDescriptor(descriptor)) : ElementMatchers.<FieldDescription>failSafe(named(internalName).and(hasDescriptor(descriptor)))); if (!candidates.isEmpty()) { Replacement.Binding binding = replacement.bind(candidates.getOnly(), opcode == Opcodes.PUTFIELD || opcode == Opcodes.PUTSTATIC); if (binding.isBound()) { TypeList.Generic parameters; switch (opcode) { case Opcodes.PUTFIELD: parameters = new TypeList.Generic.Explicit(candidates.getOnly().getDeclaringType(), candidates.getOnly().getType()); result = TypeDescription.Generic.VOID; break; case Opcodes.PUTSTATIC: parameters = new TypeList.Generic.Explicit(candidates.getOnly().getType()); result = TypeDescription.Generic.VOID; break; case Opcodes.GETFIELD: parameters = new TypeList.Generic.Explicit(candidates.getOnly().getDeclaringType()); result = candidates.getOnly().getType(); break; case Opcodes.GETSTATIC: parameters = new TypeList.Generic.Empty(); result = candidates.getOnly().getType(); break; default:
@Override protected FieldList<?> locate(ElementMatcher<? super FieldDescription> matcher) { return accessingType.getDeclaredFields().filter(matcher); }
private static String getREntityIdFieldName(Object o) throws Exception { return Introspectior .getFieldsWithAnnotation(o.getClass().getSuperclass(), RId.class) .getOnly() .getName(); }
/** * {@inheritDoc} */ public int size() { return instrumentedType.getDeclaredFields().size(); } }
/** {@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()); } },
private void visitFields(Set<Class<?>> types, AnnotationCache cache, TypeDescription typeDescription, boolean inheriting) { typeDescription.getDeclaredFields().forEach(f -> { Consumer<Annotation> addToCache = a -> types.forEach(t -> cache.addFieldAnnotation(t, f.getName(), a)); // Regular field annotations f.getDeclaredAnnotations() .forEach(a -> cacheSupportedAnnotations(a, types, cache, addToCache, inheriting)); // Type-use annotations f.getType().getDeclaredAnnotations() .forEach(a -> cacheSupportedAnnotations(a, types, cache, addToCache, inheriting)); }); typeDescription.getDeclaredMethods() .filter(m -> m.getName().startsWith("get") && m.getName().length() > 3) .forEach(m -> { String methodName = m.getName(); String correspondingFieldName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4); Consumer<Annotation> addToCache = a -> types.forEach(t -> cache.addFieldAnnotation(t, correspondingFieldName, a)); m.getDeclaredAnnotations() .forEach(a -> cacheSupportedAnnotations(a, types, cache, addToCache, inheriting)); }); }
@Override protected FieldList<?> locate(ElementMatcher<? super FieldDescription> matcher) { return typeDescription.getDeclaredFields().filter(matcher); }
private static String getREntityIdFieldName(Object o) throws Exception { return Introspectior .getFieldsWithAnnotation(o.getClass().getSuperclass(), RId.class) .getOnly() .getName(); }
/** * {@inheritDoc} */ public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) { FieldList<?> fieldList = instrumentedType.getDeclaredFields(); List<StackManipulation> fieldLoadings = new ArrayList<StackManipulation>(fieldList.size()); for (FieldDescription fieldDescription : fieldList) { fieldLoadings.add(new StackManipulation.Compound(MethodVariableAccess.loadThis(), FieldAccess.forField(fieldDescription).read())); } StackManipulation.Size stackSize = new StackManipulation.Compound( new StackManipulation.Compound(fieldLoadings), MethodInvocation.invoke(accessorMethod), assigner.assign(accessorMethod.getReturnType(), instrumentedMethod.getReturnType(), Assigner.Typing.DYNAMIC), MethodReturn.of(instrumentedMethod.getReturnType()) ).apply(methodVisitor, implementationContext); return new Size(stackSize.getMaximalSize(), instrumentedMethod.getStackSize()); } }
/** * Creates an accessor to read an enumeration value. * * @param enumerationDescription The description of the enumeration. * @return A stack manipulation for reading the enumeration. */ public static StackManipulation forEnumeration(EnumerationDescription enumerationDescription) { FieldList<FieldDescription.InDefinedShape> fieldList = enumerationDescription.getEnumerationType() .getDeclaredFields() .filter(named(enumerationDescription.getValue())); return fieldList.size() != 1 || !fieldList.getOnly().isStatic() || !fieldList.getOnly().isPublic() || !fieldList.getOnly().isEnum() ? StackManipulation.Illegal.INSTANCE : STATIC.new AccessDispatcher(fieldList.getOnly()).read(); }
public static FieldList<FieldDescription.InDefinedShape> getFieldsWithAnnotation(Class<?> c, Class<? extends Annotation> a) { return getAllFields(c) .filter(ElementMatchers.isAnnotatedWith(a)); }
private String getRIdFieldName(Class<?> cls) { return Introspectior.getFieldsWithAnnotation(cls, RId.class) .getOnly() .getName(); }
/** * {@inheritDoc} */ public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) { FieldList<?> fieldList = instrumentedType.getDeclaredFields(); StackManipulation[] fieldLoading = new StackManipulation[fieldList.size()]; int index = 0; for (FieldDescription fieldDescription : fieldList) { fieldLoading[index] = new StackManipulation.Compound( MethodVariableAccess.loadThis(), MethodVariableAccess.load(instrumentedMethod.getParameters().get(index)), FieldAccess.forField(fieldDescription).write() ); index++; } StackManipulation.Size stackSize = new StackManipulation.Compound( MethodVariableAccess.loadThis(), MethodInvocation.invoke(ConstructorCall.INSTANCE.objectTypeDefaultConstructor), new StackManipulation.Compound(fieldLoading), MethodReturn.VOID ).apply(methodVisitor, implementationContext); return new Size(stackSize.getMaximalSize(), instrumentedMethod.getStackSize()); } }