/** Returns the Class object representing the class or interface that declares the field. */ public Class getDeclaringClass () { return field.getDeclaringClass(); }
/** Returns the Class object representing the class or interface that declares the field. */ public Class getDeclaringClass () { return field.getDeclaringClass(); }
public FieldTypeProvider(Field field) { this.fieldName = field.getName(); this.declaringClass = field.getDeclaringClass(); this.field = field; }
/** * Create a new descriptor for a field. * @param field the field to wrap * @param required whether the dependency is required * @param eager whether this dependency is 'eager' in the sense of * eagerly resolving potential target beans for type matching */ public DependencyDescriptor(Field field, boolean required, boolean eager) { super(field); this.declaringClass = field.getDeclaringClass(); this.fieldName = field.getName(); this.required = required; this.eager = eager; }
public Serialization(Field f) { clazz = f.getDeclaringClass(); name = f.getName(); } }
public Serialization(Field f) { clazz = f.getDeclaringClass(); name = f.getName(); } }
/** * Make the given field accessible, explicitly setting it accessible if * necessary. The {@code setAccessible(true)} method is only called * when actually necessary, to avoid unnecessary conflicts with a JVM * SecurityManager (if active). * @param field the field to make accessible * @see java.lang.reflect.Field#setAccessible */ @SuppressWarnings("deprecation") // on JDK 9 public static void makeAccessible(Field field) { if ((!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers()) || Modifier.isFinal(field.getModifiers())) && !field.isAccessible()) { field.setAccessible(true); } }
/** * {@inheritDoc} */ public TypeDescription getDeclaringType() { return TypeDescription.ForLoadedType.of(field.getDeclaringClass()); }
static void logDeserializeError(Field field, Object obj, Object value, Throwable e) throws IOException { String fieldName = (field.getDeclaringClass().getName() + "." + field.getName()); if (e instanceof HessianFieldException) throw (HessianFieldException) e; else if (e instanceof IOException) throw new HessianFieldException(fieldName + ": " + e.getMessage(), e); if (value != null) throw new HessianFieldException(fieldName + ": " + value.getClass().getName() + " (" + value + ")" + " cannot be assigned to '" + field.getType().getName() + "'", e); else throw new HessianFieldException(fieldName + ": " + field.getType().getName() + " cannot be assigned from null", e); }
/** * Parses the field data from the existing field. * * @param field field to parse * @return field data */ public static FieldData parse(Field field) { return new FieldData(field, computeOffset(field), field.getDeclaringClass().getSimpleName(), field.getName(), field.getType().getSimpleName()); }
protected Object unmarshalField(final UnmarshallingContext context, final Object result, Class type, Field field) { Converter converter = mapper.getLocalConverter(field.getDeclaringClass(), field.getName()); return context.convertAnother(result, type, converter); }
protected void marshallField(final MarshallingContext context, Object newObj, Field field) { Converter converter = mapper.getLocalConverter(field.getDeclaringClass(), field.getName()); context.convertAnother(newObj, converter); }
/** * Return a {@link ResolvableType} for the specified {@link Field} with a given * implementation. * <p>Use this variant when the class that declares the field includes generic * parameter variables that are satisfied by the implementation type. * @param field the source field * @param implementationType the implementation type * @return a {@link ResolvableType} for the specified field * @see #forField(Field) */ public static ResolvableType forField(Field field, @Nullable ResolvableType implementationType) { Assert.notNull(field, "Field must not be null"); ResolvableType owner = (implementationType != null ? implementationType : NONE); owner = owner.as(field.getDeclaringClass()); return forType(null, new FieldTypeProvider(field), owner.asVariableResolver()); }
public static MockitoException cannotInjectDependency(Field field, Object matchingMock, Exception details) { return new MockitoException(join( "Mockito couldn't inject mock dependency '" + MockUtil.getMockName(matchingMock) + "' on field ", "'" + field + "'", "whose type '" + field.getDeclaringClass().getCanonicalName() + "' was annotated by @InjectMocks in your test.", "Also I failed because: " + exceptionCauseMessageIfAvailable(details), "" ), details); }
/** * Return a {@link ResolvableType} for the specified {@link Field} with a given * implementation. * <p>Use this variant when the class that declares the field includes generic * parameter variables that are satisfied by the implementation class. * @param field the source field * @param implementationClass the implementation class * @return a {@link ResolvableType} for the specified field * @see #forField(Field) */ public static ResolvableType forField(Field field, Class<?> implementationClass) { Assert.notNull(field, "Field must not be null"); ResolvableType owner = forType(implementationClass).as(field.getDeclaringClass()); return forType(null, new FieldTypeProvider(field), owner.asVariableResolver()); }
/** * Return a {@link ResolvableType} for the specified {@link Field} with a given * implementation and the given nesting level. * <p>Use this variant when the class that declares the field includes generic * parameter variables that are satisfied by the implementation class. * @param field the source field * @param nestingLevel the nesting level (1 for the outer level; 2 for a nested * generic type; etc) * @param implementationClass the implementation class * @return a {@link ResolvableType} for the specified field * @see #forField(Field) */ public static ResolvableType forField(Field field, int nestingLevel, @Nullable Class<?> implementationClass) { Assert.notNull(field, "Field must not be null"); ResolvableType owner = forType(implementationClass).as(field.getDeclaringClass()); return forType(null, new FieldTypeProvider(field), owner.asVariableResolver()).getNested(nestingLevel); }
/** * Return whether this dependency is required. * <p>Optional semantics are derived from Java 8's {@link java.util.Optional}, * any variant of a parameter-level {@code Nullable} annotation (such as from * JSR-305 or the FindBugs set of annotations), or a language-level nullable * type declaration in Kotlin. */ public boolean isRequired() { if (!this.required) { return false; } if (this.field != null) { return !(this.field.getType() == Optional.class || hasNullableAnnotation() || (KotlinDetector.isKotlinReflectPresent() && KotlinDetector.isKotlinType(this.field.getDeclaringClass()) && KotlinDelegate.isNullable(this.field))); } else { return !obtainMethodParameter().isOptional(); } }
@Test public void testCustomAnnotationRequiredFieldResourceInjectionFailsWhenNoDependencyFound() { bpp.setAutowiredAnnotationType(MyAutowired.class); bpp.setRequiredParameterName("optional"); bpp.setRequiredParameterValue(false); bf.registerBeanDefinition("customBean", new RootBeanDefinition( CustomAnnotationRequiredFieldResourceInjectionBean.class)); try { bf.getBean("customBean"); fail("Should have thrown UnsatisfiedDependencyException"); } catch (UnsatisfiedDependencyException ex) { // expected assertSame(CustomAnnotationRequiredFieldResourceInjectionBean.class, ex.getInjectionPoint().getField().getDeclaringClass()); } }
@Test public void testCustomAnnotationRequiredFieldResourceInjectionFailsWhenMultipleDependenciesFound() { bpp.setAutowiredAnnotationType(MyAutowired.class); bpp.setRequiredParameterName("optional"); bpp.setRequiredParameterValue(false); bf.registerBeanDefinition("customBean", new RootBeanDefinition( CustomAnnotationRequiredFieldResourceInjectionBean.class)); TestBean tb1 = new TestBean(); bf.registerSingleton("testBean1", tb1); TestBean tb2 = new TestBean(); bf.registerSingleton("testBean2", tb2); try { bf.getBean("customBean"); fail("Should have thrown UnsatisfiedDependencyException"); } catch (UnsatisfiedDependencyException ex) { // expected assertSame(CustomAnnotationRequiredFieldResourceInjectionBean.class, ex.getInjectionPoint().getField().getDeclaringClass()); } }
@Test public void testCustomAnnotationOptionalFieldResourceInjectionWhenMultipleDependenciesFound() { bpp.setAutowiredAnnotationType(MyAutowired.class); bpp.setRequiredParameterName("optional"); bpp.setRequiredParameterValue(false); bf.registerBeanDefinition("customBean", new RootBeanDefinition( CustomAnnotationOptionalFieldResourceInjectionBean.class)); TestBean tb1 = new TestBean(); bf.registerSingleton("testBean1", tb1); TestBean tb2 = new TestBean(); bf.registerSingleton("testBean2", tb2); try { bf.getBean("customBean"); fail("Should have thrown UnsatisfiedDependencyException"); } catch (UnsatisfiedDependencyException ex) { // expected assertSame(CustomAnnotationOptionalFieldResourceInjectionBean.class, ex.getInjectionPoint().getField().getDeclaringClass()); } }