private void readObject(ObjectInputStream inputStream) throws IOException, ClassNotFoundException { inputStream.defaultReadObject(); try { this.field = this.declaringClass.getDeclaredField(this.fieldName); } catch (Throwable ex) { throw new IllegalStateException("Could not find original class structure", ex); } } }
static <T> FieldSetter<T> getFieldSetter(final Class<T> clazz, String fieldName) { try { Field field = clazz.getDeclaredField(fieldName); return new FieldSetter<T>(field); } catch (NoSuchFieldException e) { throw new AssertionError(e); // programmer error } }
private static Map<?, ?> getSerializableFactoryMap() throws Exception { Field field = DefaultListableBeanFactory.class.getDeclaredField("serializableFactories"); field.setAccessible(true); return (Map<?, ?>) field.get(DefaultListableBeanFactory.class); }
@SuppressWarnings("unchecked") public Map<String, String> getMap(Object target) { try { Field f = target.getClass().getDeclaredField(mapName); return (Map<String, String>) f.get(target); } catch (Exception ex) { } return null; }
@SuppressWarnings("unchecked") private static Map<ClassLoader, WebApplicationContext> getCurrentContextPerThreadFromContextLoader() { try { Field field = ContextLoader.class.getDeclaredField("currentContextPerThread"); field.setAccessible(true); return (Map<ClassLoader, WebApplicationContext>) field.get(null); } catch (Exception ex) { throw new IllegalStateException(ex); } }
private void checkHelperVersion(ClassLoader classLoader, String expectedHelperClassName) throws Exception { // Make sure we are actually running with the expected helper implementation Class<?> abstractFutureClass = classLoader.loadClass(AbstractFuture.class.getName()); Field helperField = abstractFutureClass.getDeclaredField("ATOMIC_HELPER"); helperField.setAccessible(true); assertEquals(expectedHelperClassName, helperField.get(null).getClass().getSimpleName()); }
private void checkHelperVersion(ClassLoader classLoader, String expectedHelperClassName) throws Exception { // Make sure we are actually running with the expected helper implementation Class<?> abstractFutureClass = classLoader.loadClass(AggregateFutureState.class.getName()); Field helperField = abstractFutureClass.getDeclaredField("ATOMIC_HELPER"); helperField.setAccessible(true); assertEquals(expectedHelperClassName, helperField.get(null).getClass().getSimpleName()); }
@GwtIncompatible // reflection private static int bucketsOf(HashMap<?, ?> hashMap) throws Exception { Field tableField = HashMap.class.getDeclaredField("table"); tableField.setAccessible(true); Object[] table = (Object[]) tableField.get(hashMap); // In JDK8, table is set lazily, so it may be null. return table == null ? 0 : table.length; }
private void assertIsCompiled(Expression expression) { try { Field field = SpelExpression.class.getDeclaredField("compiledAst"); field.setAccessible(true); Object object = field.get(expression); assertNotNull(object); } catch (Exception ex) { fail(ex.toString()); } }
@Before public void setUp() throws Exception { ExpressionWithConversionTests.typeDescriptorForListOfString = new TypeDescriptor(ExpressionWithConversionTests.class.getDeclaredField("listOfString")); ExpressionWithConversionTests.typeDescriptorForListOfInteger = new TypeDescriptor(ExpressionWithConversionTests.class.getDeclaredField("listOfInteger")); }
@Test public void defaultRefreshCheckDelay() throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext(CONFIG); Advised advised = (Advised) context.getBean("testBean"); AbstractRefreshableTargetSource targetSource = ((AbstractRefreshableTargetSource) advised.getTargetSource()); Field field = AbstractRefreshableTargetSource.class.getDeclaredField("refreshCheckDelay"); field.setAccessible(true); long delay = ((Long) field.get(targetSource)).longValue(); assertEquals(5000L, delay); }
static Element field(String name) throws Exception { Element element = new Element(A.class.getDeclaredField(name)); assertEquals(name, element.getName()); assertEquals(A.class, element.getDeclaringClass()); return element; }
@Test public void convertArrayToCollectionGenericTypeConversion() throws Exception { @SuppressWarnings("unchecked") List<Integer> result = (List<Integer>) conversionService.convert(new String[] {"1", "2", "3"}, TypeDescriptor .valueOf(String[].class), new TypeDescriptor(getClass().getDeclaredField("genericList"))); assertEquals(Integer.valueOf(1), result.get(0)); assertEquals(Integer.valueOf(2), result.get(1)); assertEquals(Integer.valueOf(3), result.get(2)); }
public void testWildcardCaptured_field_upperBound() throws Exception { TypeToken<Holder<?>> type = new TypeToken<Holder<?>>() {}; TypeToken<?> matrixType = type.resolveType(Holder.class.getDeclaredField("matrix").getGenericType()); assertEquals(List[].class, matrixType.getRawType()); assertThat(matrixType.getType()).isNotEqualTo(new TypeToken<List<?>[]>() {}.getType()); }
public void testWildcardCaptured_typeVariableDeclaresTypeBound_wildcardHasNoExplicitUpperBound() throws Exception { TypeToken<Counter<?>> type = new TypeToken<Counter<?>>() {}; TypeToken<?> fieldType = type.resolveType(Counter.class.getDeclaredField("counts").getGenericType()); Type[] typeArgs = ((ParameterizedType) fieldType.getType()).getActualTypeArguments(); assertThat(typeArgs).asList().hasSize(1); TypeVariable<?> captured = (TypeVariable<?>) typeArgs[0]; assertThat(captured.getBounds()).asList().containsExactly(Number.class); assertThat(new TypeToken<List<? extends Number>>() {}.isSupertypeOf(fieldType)).isTrue(); assertThat(new TypeToken<List<? extends Iterable<?>>>() {}.isSupertypeOf(fieldType)).isFalse(); }
public void testWildcardCaptured_typeVariableDeclaresTypeBound_wildcardHasExplicitUpperBound() throws Exception { TypeToken<Counter<? extends Number>> type = new TypeToken<Counter<? extends Number>>() {}; TypeToken<?> fieldType = type.resolveType(Counter.class.getDeclaredField("counts").getGenericType()); Type[] typeArgs = ((ParameterizedType) fieldType.getType()).getActualTypeArguments(); assertThat(typeArgs).asList().hasSize(1); TypeVariable<?> captured = (TypeVariable<?>) typeArgs[0]; assertThat(captured.getBounds()).asList().containsExactly(Number.class); assertThat(new TypeToken<List<? extends Number>>() {}.isSupertypeOf(fieldType)).isTrue(); assertThat(new TypeToken<List<? extends Iterable<?>>>() {}.isSupertypeOf(fieldType)).isFalse(); }
@Test public void fieldList() throws Exception { TypeDescriptor typeDescriptor = new TypeDescriptor(TypeDescriptorTests.class.getDeclaredField("listOfString")); assertFalse(typeDescriptor.isArray()); assertEquals(List.class, typeDescriptor.getType()); assertEquals(String.class, typeDescriptor.getElementTypeDescriptor().getType()); assertEquals("java.util.List<java.lang.String>", typeDescriptor.toString()); }
@Test public void fieldComplexTypeDescriptor() throws Exception { TypeDescriptor typeDescriptor = new TypeDescriptor(TypeDescriptorTests.class.getDeclaredField("arrayOfListOfString")); assertTrue(typeDescriptor.isArray()); assertEquals(List.class,typeDescriptor.getElementTypeDescriptor().getType()); assertEquals(String.class, typeDescriptor.getElementTypeDescriptor().getElementTypeDescriptor().getType()); assertEquals("java.util.List<java.lang.String>[]",typeDescriptor.toString()); }
@Test public void fieldListOfListOfString() throws Exception { TypeDescriptor typeDescriptor = new TypeDescriptor(TypeDescriptorTests.class.getDeclaredField("listOfListOfString")); assertFalse(typeDescriptor.isArray()); assertEquals(List.class, typeDescriptor.getType()); assertEquals(List.class, typeDescriptor.getElementTypeDescriptor().getType()); assertEquals(String.class, typeDescriptor.getElementTypeDescriptor().getElementTypeDescriptor().getType()); assertEquals("java.util.List<java.util.List<java.lang.String>>", typeDescriptor.toString()); }
@Test public void fieldComplexTypeDescriptor2() throws Exception { TypeDescriptor typeDescriptor = new TypeDescriptor(TypeDescriptorTests.class.getDeclaredField("nestedMapField")); assertTrue(typeDescriptor.isMap()); assertEquals(String.class,typeDescriptor.getMapKeyTypeDescriptor().getType()); assertEquals(List.class, typeDescriptor.getMapValueTypeDescriptor().getType()); assertEquals(Integer.class, typeDescriptor.getMapValueTypeDescriptor().getElementTypeDescriptor().getType()); assertEquals("java.util.Map<java.lang.String, java.util.List<java.lang.Integer>>", typeDescriptor.toString()); }