List of usage examples for org.springframework.util ReflectionUtils makeAccessible
@SuppressWarnings("deprecation") public static void makeAccessible(Field field)
From source file:org.springframework.core.annotation.AnnotationUtils.java
/** * Get all methods declared in the supplied {@code annotationType} that * match Java's requirements for annotation <em>attributes</em>. * <p>All methods in the returned list will be * {@linkplain ReflectionUtils#makeAccessible(Method) made accessible}. * @param annotationType the type in which to search for attribute methods * (never {@code null})/*from w w w . j a v a 2s . c o m*/ * @return all annotation attribute methods in the specified annotation * type (never {@code null}, though potentially <em>empty</em>) * @since 4.2 */ static List<Method> getAttributeMethods(Class<? extends Annotation> annotationType) { List<Method> methods = attributeMethodsCache.get(annotationType); if (methods != null) { return methods; } methods = new ArrayList<>(); for (Method method : annotationType.getDeclaredMethods()) { if (isAttributeMethod(method)) { ReflectionUtils.makeAccessible(method); methods.add(method); } } attributeMethodsCache.put(annotationType, methods); return methods; }
From source file:org.springframework.core.type.classreading.AbstractRecursiveAnnotationVisitor.java
protected Object getEnumValue(String asmTypeDescriptor, String attributeValue) { Object valueToUse = attributeValue; try {//from ww w . j av a 2s .co m Class<?> enumType = ClassUtils.forName(Type.getType(asmTypeDescriptor).getClassName(), this.classLoader); Field enumConstant = ReflectionUtils.findField(enumType, attributeValue); if (enumConstant != null) { ReflectionUtils.makeAccessible(enumConstant); valueToUse = enumConstant.get(null); } } catch (ClassNotFoundException | NoClassDefFoundError ex) { logger.debug("Failed to classload enum type while reading annotation metadata", ex); } catch (IllegalAccessException | AccessControlException ex) { logger.debug("Could not access enum value while reading annotation metadata", ex); } return valueToUse; }
From source file:org.springframework.data.document.web.bind.annotation.support.HandlerMethodInvoker.java
public final Object invokeHandlerMethod(Method handlerMethod, Object handler, NativeWebRequest webRequest, ExtendedModelMap implicitModel) throws Exception { Method handlerMethodToInvoke = BridgeMethodResolver.findBridgedMethod(handlerMethod); try {/*w w w. j av a2 s .c o m*/ boolean debug = logger.isDebugEnabled(); for (String attrName : this.methodResolver.getActualSessionAttributeNames()) { Object attrValue = this.sessionAttributeStore.retrieveAttribute(webRequest, attrName); if (attrValue != null) { implicitModel.addAttribute(attrName, attrValue); } } for (Method attributeMethod : this.methodResolver.getModelAttributeMethods()) { Method attributeMethodToInvoke = BridgeMethodResolver.findBridgedMethod(attributeMethod); Object[] args = resolveHandlerArguments(attributeMethodToInvoke, handler, webRequest, implicitModel); if (debug) { logger.debug("Invoking model attribute method: " + attributeMethodToInvoke); } String attrName = AnnotationUtils.findAnnotation(attributeMethod, ModelAttribute.class).value(); if (!"".equals(attrName) && implicitModel.containsAttribute(attrName)) { continue; } ReflectionUtils.makeAccessible(attributeMethodToInvoke); Object attrValue = attributeMethodToInvoke.invoke(handler, args); if ("".equals(attrName)) { Class resolvedType = GenericTypeResolver.resolveReturnType(attributeMethodToInvoke, handler.getClass()); attrName = Conventions.getVariableNameForReturnType(attributeMethodToInvoke, resolvedType, attrValue); } if (!implicitModel.containsAttribute(attrName)) { implicitModel.addAttribute(attrName, attrValue); } } Object[] args = resolveHandlerArguments(handlerMethodToInvoke, handler, webRequest, implicitModel); if (debug) { logger.debug("Invoking request handler method: " + handlerMethodToInvoke); } ReflectionUtils.makeAccessible(handlerMethodToInvoke); return handlerMethodToInvoke.invoke(handler, args); } catch (IllegalStateException ex) { // Internal assertion failed (e.g. invalid signature): // throw exception with full handler method context... throw new HandlerMethodInvocationException(handlerMethodToInvoke, ex); } catch (InvocationTargetException ex) { // User-defined @ModelAttribute/@InitBinder/@RequestMapping method threw an exception... ReflectionUtils.rethrowException(ex.getTargetException()); return null; } }
From source file:org.springframework.data.document.web.bind.annotation.support.HandlerMethodInvoker.java
protected void initBinder(Object handler, String attrName, WebDataBinder binder, NativeWebRequest webRequest) throws Exception { if (this.bindingInitializer != null) { this.bindingInitializer.initBinder(binder, webRequest); }/*w w w. j a v a2 s .c o m*/ if (handler != null) { Set<Method> initBinderMethods = this.methodResolver.getInitBinderMethods(); if (!initBinderMethods.isEmpty()) { boolean debug = logger.isDebugEnabled(); for (Method initBinderMethod : initBinderMethods) { Method methodToInvoke = BridgeMethodResolver.findBridgedMethod(initBinderMethod); String[] targetNames = AnnotationUtils.findAnnotation(initBinderMethod, InitBinder.class) .value(); if (targetNames.length == 0 || Arrays.asList(targetNames).contains(attrName)) { Object[] initBinderArgs = resolveInitBinderArguments(handler, methodToInvoke, binder, webRequest); if (debug) { logger.debug("Invoking init-binder method: " + methodToInvoke); } ReflectionUtils.makeAccessible(methodToInvoke); Object returnValue = methodToInvoke.invoke(handler, initBinderArgs); if (returnValue != null) { throw new IllegalStateException( "InitBinder methods must not have a return value: " + methodToInvoke); } } } } } }
From source file:org.springframework.data.gemfire.RegionFactoryBean.java
@SuppressWarnings("unchecked") private AttributesFactory<K, V> findAttrFactory(RegionFactory<K, V> regionFactory) { Field attrField = ReflectionUtils.findField(RegionFactory.class, "attrsFactory", AttributesFactory.class); ReflectionUtils.makeAccessible(attrField); return (AttributesFactory<K, V>) ReflectionUtils.getField(attrField, regionFactory); }
From source file:org.springframework.data.hadoop.mapreduce.ExecutionUtils.java
/** * Clean the LocalDirAllocator#contexts//from w ww . j a va2 s. co m */ private static void cleanHadoopLocalDirAllocator() { Field field = ReflectionUtils.findField(LocalDirAllocator.class, "contexts"); ReflectionUtils.makeAccessible(field); Map contexts = (Map) ReflectionUtils.getField(field, null); if (contexts != null) { contexts.clear(); } }
From source file:org.springframework.data.hadoop.util.PermissionUtils.java
public static void hackHadoopStagingOnWin() { // do the assignment only on Windows systems if (System.getProperty("os.name").toLowerCase().startsWith("win")) { // 0655 = -rwxr-xr-x JobSubmissionFiles.JOB_DIR_PERMISSION.fromShort((short) 0650); JobSubmissionFiles.JOB_FILE_PERMISSION.fromShort((short) 0650); if (trackerDistributedCacheManagerClass != null) { // handle jar permissions as well Field field = ReflectionUtils.findField(trackerDistributedCacheManagerClass, "PUBLIC_CACHE_OBJECT_PERM"); ReflectionUtils.makeAccessible(field); FsPermission perm = (FsPermission) ReflectionUtils.getField(field, null); perm.fromShort((short) 0650); }/* w ww. ja v a 2 s . c o m*/ } }
From source file:org.springframework.data.keyvalue.riak.RiakMappedClass.java
private void initFields() { ReflectionUtils.doWithFields(this.clazz, new FieldCallback() { @Override//from w w w . j ava 2s . co m public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { if (!field.isAccessible()) ReflectionUtils.makeAccessible(field); if (field.isAnnotationPresent(Transient.class) || field.isSynthetic() || field.getModifiers() == Modifier.FINAL || field.getModifiers() == Modifier.TRANSIENT) { return; } // Field can only have one of these, if more than one throw an // error List<Annotation> annots = org.springframework.data.keyvalue.riak.util.AnnotationUtils .getFoundAnnotation(Id.class, Column.class, Embedded.class, Version.class, ManyToOne.class, OneToMany.class, Basic.class); // Probably allow auto generation at some point, but for now // have to add one of the annotations if (annots.size() > 1) throw new IllegalArgumentException(String.format( "The field %s must have only one of the following annotations: " + "@Id, @Basic, @Column, @Embedded, @Version, @ManyToOne, @OneToMany", field.getName())); Annotation annot = annots.get(0); if (annot.annotationType().equals(Id.class)) RiakMappedClass.this.id = field; // Create a new mapped field here and then add to a list of // property MappedFields propertyFields.add(new RiakMappedField(field, annot)); } }); Map<Class<? extends Annotation>, Annotation> fieldAnnotMap = new HashMap<Class<? extends Annotation>, Annotation>(); for (Class<? extends Annotation> a : entityAnnotations) { Target target = a.getAnnotation(Target.class); if (target != null && (ArrayUtils.contains(target.value(), ElementType.FIELD) || ArrayUtils.contains(target.value(), ElementType.METHOD))) { Annotation fieldAnnot; if ((fieldAnnot = this.clazz.getAnnotation(a)) != null) { fieldAnnotMap.put(a, fieldAnnot); } } } }
From source file:org.springframework.data.keyvalue.riak.RiakTemplate.java
private List<RiakLink> getLinksFromObject(final Object val) { final List<RiakLink> listOfLinks = new ArrayList<RiakLink>(); ReflectionUtils.doWithFields(val.getClass(), new FieldCallback() { @Override/*from w ww . j av a 2 s .com*/ public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { if (!field.isAccessible()) ReflectionUtils.makeAccessible(field); org.springframework.data.keyvalue.riak.RiakLink linkAnnot = field .getAnnotation(org.springframework.data.keyvalue.riak.RiakLink.class); String property = linkAnnot.property(); Object referencedObj = field.get(val); Field prop = ReflectionUtils.findField(referencedObj.getClass(), property); if (!prop.isAccessible()) ReflectionUtils.makeAccessible(prop); listOfLinks.add(new RiakLink(field.getType().getName(), prop.get(referencedObj).toString(), linkAnnot.value())); } }, new FieldFilter() { @Override public boolean matches(Field field) { return field.isAnnotationPresent(org.springframework.data.keyvalue.riak.RiakLink.class); } }); return listOfLinks; }
From source file:org.springframework.data.solr.server.support.SolrServerUtils.java
@SuppressWarnings("unchecked") private static <T> T readField(SolrServer solrServer, String fieldName) { Field field = ReflectionUtils.findField(solrServer.getClass(), fieldName); if (field == null) { return null; }/* ww w .ja v a 2s. c om*/ ReflectionUtils.makeAccessible(field); return (T) ReflectionUtils.getField(field, solrServer); }