List of usage examples for java.lang.reflect Method getParameterTypes
@Override
public Class<?>[] getParameterTypes()
From source file:org.resthub.rpc.AMQPHessianProxy.java
/** * Handles the object invocation.//from w w w.j av a2 s . c o m * * @param proxy the proxy object to invoke * @param method the method to call * @param args the arguments to the proxy object */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String methodName = method.getName(); Class<?>[] params = method.getParameterTypes(); // equals and hashCode are special cased if (methodName.equals("equals") && params.length == 1 && params[0].equals(Object.class)) { Object value = args[0]; if (value == null || !Proxy.isProxyClass(value.getClass())) { return Boolean.FALSE; } AMQPHessianProxy handler = (AMQPHessianProxy) Proxy.getInvocationHandler(value); return _factory.equals(handler._factory); } else if (methodName.equals("hashCode") && params.length == 0) { return _factory.hashCode(); } else if (methodName.equals("toString") && params.length == 0) { return "[HessianProxy " + proxy.getClass() + "]"; } ConnectionFactory connectionFactory = _factory.getConnectionFactory(); try { Message response = sendRequest(connectionFactory, method, args); if (response == null) { throw new TimeoutException(); } MessageProperties props = response.getMessageProperties(); boolean compressed = "deflate".equals(props.getContentEncoding()); AbstractHessianInput in; InputStream is = new ByteArrayInputStream(response.getBody()); if (compressed) { is = new InflaterInputStream(is, new Inflater(true)); } int code = is.read(); if (code == 'H') { int major = is.read(); int minor = is.read(); in = _factory.getHessian2Input(is); return in.readReply(method.getReturnType()); } else if (code == 'r') { int major = is.read(); int minor = is.read(); in = _factory.getHessianInput(is); in.startReplyBody(); Object value = in.readObject(method.getReturnType()); in.completeReply(); return value; } else { throw new HessianProtocolException("'" + (char) code + "' is an unknown code"); } } catch (HessianProtocolException e) { throw new HessianRuntimeException(e); } }
From source file:org.piraso.replacer.spring.remoting.XStreamTest.java
public void testXML() throws Exception { RemoteInvocation remote = new RemoteInvocation(); Method method = String.class.getMethod("valueOf", Object.class); Map<String, String> map = new LinkedHashMap<String, String>(); map.put("a", "b"); map.put("c", "d"); map.put("e", "f"); remote.setMethodName(method.getName()); remote.setParameterTypes(method.getParameterTypes()); remote.setArguments(//from ww w . j a v a2 s . c o m new Object[] { map, "Alvin", "de", "Leon", new TestNestedBean(new TestNestedBean(null)) }); System.out.println(XStreamUtils.XSTREAM.toXML(remote)); }
From source file:org.craftercms.commons.security.permissions.annotations.HasPermissionAnnotationHandler.java
protected Method getActualMethod(ProceedingJoinPoint pjp) { MethodSignature ms = (MethodSignature) pjp.getSignature(); Method method = ms.getMethod(); if (method.getDeclaringClass().isInterface()) { Class<?> targetClass = pjp.getTarget().getClass(); try {//from ww w. j ava 2 s .c o m method = targetClass.getDeclaredMethod(method.getName(), method.getParameterTypes()); } catch (NoSuchMethodException e) { // Should NEVER happen throw new RuntimeException(e); } } return method; }
From source file:com.fuzhepan.arpc.client.ProxyHandler.java
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { log.debug("invoke was called!"); if (method.getName().equals("toString")) { return "toString method was called"; }/*from w w w.ja v a 2 s . c om*/ RpcContext rpcContext = new RpcContext(interfaceName, method.getName(), method.getParameterTypes(), args); //get service info and load balance List<HostPortPair> serviceList = RpcConfig.getServiceList(serviceName); if (serviceList == null || serviceList.size() == 0) throw new ClassNotFoundException("not find service : " + serviceName); int index = requestCount.get() % serviceList.size(); if (requestCount.get() > 100) requestCount.set(0); else requestCount.getAndIncrement(); HostPortPair hostPort = serviceList.get(index); Socket socket = new Socket(hostPort.host, hostPort.port); ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream()); objectOutputStream.writeObject(rpcContext); objectOutputStream.flush(); ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream()); Object response = objectInputStream.readObject(); objectInputStream.close(); objectOutputStream.close(); socket.close(); Class methodReturnType = method.getReturnType(); return methodReturnType.cast(response); }
From source file:com.baidu.cc.FilterableConfigItemChangedCallable.java
/** * default constructor//from w ww. java 2 s .c o m * * @param method target method * @param target target bean * @param filerKeyStr filter keys set from annotation */ public FilterableConfigItemChangedCallable(Method method, Object target, String filerKeyStr) { super(); Assert.notNull(method, "property 'method' should not be null"); this.method = method; //check method Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes == null || parameterTypes.length != 1 || !List.class.equals(parameterTypes[0])) { throw new RuntimeException("Invalid method. @ConfigChangeNotifer method only " + "has one parameter and type is List<'" + ChangedConfigItem.class.getName() + ">'"); } this.target = target; filerKeys = new HashSet<String>(); if (StringUtils.isNotBlank(filerKeyStr)) { String[] split = StringUtils.split(filerKeyStr, ","); for (String string : split) { filerKeys.add(StringUtils.trim(string)); } } }
From source file:eu.qualityontime.commons.DefaultBeanIntrospector.java
/** * This method fixes an issue where IndexedPropertyDescriptor behaves * differently in different versions of the JDK for 'indexed' properties * which use java.util.List (rather than an array). It implements a * workaround for Bug 28358. If you have a Bean with the following * getters/setters for an indexed property: * * <pre>/* w ww . jav a 2 s . c o m*/ * public List getFoo() * public Object getFoo(int index) * public void setFoo(List foo) * public void setFoo(int index, Object foo) * </pre> * * then the IndexedPropertyDescriptor's getReadMethod() and getWriteMethod() * behave as follows: * <ul> * <li>JDK 1.3.1_04: returns valid Method objects from these methods.</li> * <li>JDK 1.4.2_05: returns null from these methods.</li> * </ul> * * @param beanClass * the current class to be inspected * @param descriptors * the array with property descriptors */ private void handleIndexedPropertyDescriptors(final Class<?> beanClass, final PropertyDescriptor[] descriptors) { for (final PropertyDescriptor pd : descriptors) { if (pd instanceof IndexedPropertyDescriptor) { final IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) pd; final String propName = descriptor.getName().substring(0, 1).toUpperCase() + descriptor.getName().substring(1); if (descriptor.getReadMethod() == null) { final String methodName = descriptor.getIndexedReadMethod() != null ? descriptor.getIndexedReadMethod().getName() : "get" + propName; final Method readMethod = MethodUtils.getMatchingAccessibleMethod(beanClass, methodName, EMPTY_CLASS_PARAMETERS); if (readMethod != null) { try { descriptor.setReadMethod(readMethod); } catch (final Exception e) { log.error("Error setting indexed property read method", e); } } } if (descriptor.getWriteMethod() == null) { final String methodName = descriptor.getIndexedWriteMethod() != null ? descriptor.getIndexedWriteMethod().getName() : "set" + propName; Method writeMethod = MethodUtils.getMatchingAccessibleMethod(beanClass, methodName, LIST_CLASS_PARAMETER); if (writeMethod == null) { for (final Method m : beanClass.getMethods()) { if (m.getName().equals(methodName)) { final Class<?>[] parameterTypes = m.getParameterTypes(); if (parameterTypes.length == 1 && List.class.isAssignableFrom(parameterTypes[0])) { writeMethod = m; break; } } } } if (writeMethod != null) { try { descriptor.setWriteMethod(writeMethod); } catch (final Exception e) { log.error("Error setting indexed property write method", e); } } } } } }
From source file:net.projectmonkey.spring.acl.enhancement.identity.strategy.method.DefaultMethodInvocationObjectIdRetrievalStrategy.java
/** * Extension Point: Locate the secure object mapping from the method invocation. * This is the main purpose of the class so overriding this allows users to move * completely away from the {@link SecuredAgainst} and {@link SecuredId} annotations. * @param invocation// ww w .j a v a2 s . c o m * @return */ protected SecureObjectMapping locateSecureObjectMapping(final MethodInvocation invocation) { SecureObjectMapping matchingSecuredIdParamValue = null; SecureObjectMapping matchingAssignable = null; Method method = invocation.getMethod(); Class<?> securedClass = resolveSecuredClass(method); Class<?>[] parameterTypes = method.getParameterTypes(); Object[] arguments = invocation.getArguments(); Annotation[][] parameterAnnotations = method.getParameterAnnotations(); if (arguments.length == 1) { Object argument = arguments[0]; Annotation[] annotations = parameterAnnotations[0]; SecuredId parameterAnnotation = locateAnnotation(annotations); String internalMethod = this.internalMethod; if (parameterAnnotation != null) { internalMethod = resolveInternalMethod(parameterAnnotation); } matchingSecuredIdParamValue = new SecureObjectMappingWithInternalMethod(argument, securedClass, internalMethod); } else { for (int i = 0; i < arguments.length && matchingSecuredIdParamValue == null; i++) { Object argument = arguments[i]; Annotation[] annotations = parameterAnnotations[i]; SecuredId parameterAnnotation = locateAnnotation(annotations); if (parameterAnnotation != null) { String internalMethod = resolveInternalMethod(parameterAnnotation); matchingSecuredIdParamValue = new SecureObjectMappingWithInternalMethod(argument, securedClass, internalMethod); break; // we've found a parameter which specifies it provides the id, so break; } Class<?> parameterType = parameterTypes[i]; if (matchingAssignable == null && securedClass.isAssignableFrom(parameterType)) { //here we use the actual argument type as the secured class since we are an instance of the required type. matchingAssignable = new SecureObjectMappingWithInternalMethod(argument, internalMethod); } } } return firstNonNull(matchingSecuredIdParamValue, matchingAssignable); }
From source file:org.grails.datastore.mapping.proxy.JavassistProxyFactory.java
protected Class getProxyClass(Class type) { Class proxyClass = PROXY_FACTORIES.get(type); if (proxyClass == null) { javassist.util.proxy.ProxyFactory pf = new ProxyFactory(); pf.setSuperclass(type);//w w w . j a va2s.c o m pf.setInterfaces(new Class[] { EntityProxy.class }); pf.setFilter(new MethodFilter() { public boolean isHandled(Method method) { final String methodName = method.getName(); if (methodName.indexOf("super$") > -1) { return false; } if (method.getParameterTypes().length == 0 && (methodName.equals("finalize"))) { return false; } if (EXCLUDES.contains(methodName) || method.isSynthetic() || method.isBridge()) { return false; } return true; } }); proxyClass = pf.createClass(); PROXY_FACTORIES.put(type, proxyClass); } return proxyClass; }
From source file:net.abhinavsarkar.spelhelper.ImplicitMethodResolver.java
@Override public MethodExecutor resolve(final EvaluationContext context, final Object targetObject, final String name, final List<TypeDescriptor> argumentTypes) throws AccessException { if (targetObject == null) { return null; }//from ww w . ja v a2s .c o m Class<?> type = targetObject.getClass(); String cacheKey = type.getName() + "." + name; if (CACHE.containsKey(cacheKey)) { MethodExecutor executor = CACHE.get(cacheKey); return executor == NULL_ME ? null : executor; } Method method = lookupMethod(context, type, name); if (method != null) { int modifiers = method.getModifiers(); if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)) { Class<?>[] parameterTypes = method.getParameterTypes(); Class<?> firstParamType = parameterTypes[0]; if (parameterTypes.length > 0 && firstParamType.isAssignableFrom(type)) { List<TypeDescriptor> newArgumentTypes = new ArrayList<TypeDescriptor>(); newArgumentTypes.add(TypeDescriptor.valueOf(firstParamType)); newArgumentTypes.addAll(argumentTypes); MethodExecutor executor = delegate.resolve(context, method.getDeclaringClass(), name, newArgumentTypes); MethodExecutor wrappedExecutor = executor == null ? null : new ImplicitMethodExecutor(executor); if (wrappedExecutor == null) { CACHE.putIfAbsent(cacheKey, NULL_ME); } return wrappedExecutor; } } } CACHE.putIfAbsent(cacheKey, NULL_ME); return null; }