@Override public boolean execute(String action, JSONArray args, CallbackContext cbContext) throws JSONException { try { Method[] list = this.getClass().getMethods(); Method methodToExecute = null; for (Method method : list) { if (method.getName().equals(action)) { Type[] types = method.getGenericParameterTypes(); if (args.length() + 1 == types.length) { // +1 is the cbContext boolean isValid = true; for (int i = 0; i < args.length(); i++) { Class arg = args.get(i).getClass(); if (types[i] == arg) { isValid = true; } else { isValid = false; break; } } if (isValid) { methodToExecute = method; break; } } } } if (methodToExecute != null) { Type[] types = methodToExecute.getGenericParameterTypes(); Object[] variableArgs = new Object[types.length]; for (int i = 0; i < args.length(); i++) { variableArgs[i] = args.get(i); } variableArgs[variableArgs.length - 1] = cbContext; Class<?> r = methodToExecute.getReturnType(); if (r == boolean.class) { return (Boolean) methodToExecute.invoke(this, variableArgs); } else { methodToExecute.invoke(this, variableArgs); return true; } } else { return false; } } catch (IllegalAccessException e) { e.printStackTrace(); return false; } catch (IllegalArgumentException e) { e.printStackTrace(); return false; } catch (InvocationTargetException e) { e.printStackTrace(); return false; } }
/** * Get the DBus member input signature. * * @param method The method. */ public static String getInputSig(Method method) throws AnnotationBusException { BusMethod busMethod = method.getAnnotation(BusMethod.class); if (busMethod != null && busMethod.signature().length() > 0) { return Signature.typeSig(method.getGenericParameterTypes(), busMethod.signature()); } BusSignal busSignal = method.getAnnotation(BusSignal.class); if (busSignal != null && busSignal.signature().length() > 0) { return Signature.typeSig(method.getGenericParameterTypes(), busSignal.signature()); } return Signature.typeSig(method.getGenericParameterTypes(), null); }
/** * Find a method on a class given an existing method. * * <p>If there exists a public method on the class that has the same name and parameters as the * existing method then that public method is returned. * * <p>Otherwise, if there exists a public method on the class that has the same name and the same * number of parameters as the existing method, and each generic parameter type, in order, of the * public method is equal to the generic parameter type, in the same order, of the existing method * or is an instance of {@link TypeVariable} then that public method is returned. * * @param c the class to search for a public method * @param m the method to find * @return the found public method. */ public static Method findMethodOnClass(Class c, Method m) { try { return c.getMethod(m.getName(), m.getParameterTypes()); } catch (NoSuchMethodException ex) { for (Method _m : c.getMethods()) { if (_m.getName().equals(m.getName()) && _m.getParameterTypes().length == m.getParameterTypes().length) { if (compareParameterTypes(m.getGenericParameterTypes(), _m.getGenericParameterTypes())) { return _m; } } } } return null; }
/** * Checks if the two types of methods are the same. * * @param jDiffMethod the jDiffMethod to compare * @param method the reflected method to compare * @return true, if both methods are the same */ private boolean matches(JDiffMethod jDiffMethod, Method method) { // If the method names aren't equal, the methods can't match. if (jDiffMethod.mName.equals(method.getName())) { String jdiffReturnType = jDiffMethod.mReturnType; String reflectionReturnType = typeToString(method.getGenericReturnType()); List<String> jdiffParamList = jDiffMethod.mParamList; // Next, compare the return types of the two methods. If // they aren't equal, the methods can't match. if (jdiffReturnType.equals(reflectionReturnType)) { Type[] params = method.getGenericParameterTypes(); // Next, check the method parameters. If they have // different number of parameters, the two methods // can't match. if (jdiffParamList.size() == params.length) { // If any of the parameters don't match, the // methods can't match. for (int i = 0; i < jdiffParamList.size(); i++) { if (!compareParam(jdiffParamList.get(i), params[i])) { return false; } } // We've passed all the tests, the methods do // match. return true; } } } return false; }
/** * To make life easier for the user we will figure out the type of {@link StreamListener} the user * passed and based on that setup the correct stream analyzer etc. * * <p>{@inheritDoc} */ @SuppressWarnings("unchecked") @Override public void addStreamListener(final StreamListener<? extends Packet> listener) { try { final Method method = listener.getClass().getMethod("endStream", Stream.class); final ParameterizedType parameterizedType = (ParameterizedType) method.getGenericParameterTypes()[0]; final Type[] parameterArgTypes = parameterizedType.getActualTypeArguments(); // TODO: could actually be more. final Type parameterArgType = parameterArgTypes[0]; final Class<?> parameterArgClass = (Class<?>) parameterArgType; if (parameterArgClass.equals(SipPacket.class)) { if (this.sipStreamHandler == null) { this.sipStreamHandler = new SipStreamHandler(this.framerManager); } this.sipStreamHandler.addListener((StreamListener<SipPacket>) listener); } } catch (final ArrayIndexOutOfBoundsException e) { throw new RuntimeException("Unable to figure out the paramterized type", e); } catch (final SecurityException e) { throw new RuntimeException( "Unable to access method information due to security constraints", e); } catch (final NoSuchMethodException e) { throw new RuntimeException("The startStream method doesn't exist. Signature changed?", e); } catch (final ClassCastException e) { // means that the user had not parameterized the StreamListener // interface, which means that we cannot actually detect streams. throw new IllegalArgumentException("The supplied listener has not been parameterized"); } }
/** Get Generic types of method's parameters. */ public static Class<?>[][] getGenericMethodParameterTypes(Method method) { Type[] genericType = method.getGenericParameterTypes(); int len = genericType.length; Class<?>[][] genericTypes = new Class<?>[len][]; for (int i = 0; i < len; i++) genericTypes[i] = getGenericTypes(genericType[i]); return genericTypes; }
private void BuildSignature() { Var[] vars = new Var[samMethod.getParameterTypes().length]; lambdaApplyParameters = new Class<?>[samMethod.getParameterTypes().length]; for (int i = 0; i < vars.length; i++) { //noinspection unchecked Type paramType = samMethod.getGenericParameterTypes()[i]; if (paramType instanceof Class) vars[i] = new Var((Class<?>) paramType); else if (paramType instanceof TypeVariable) vars[i] = new Var(findMaterializedType(((TypeVariable) paramType).getName())); else throw new LambdaException( "unexpected param generic type [%s] for SAM [%s]", paramType, samType.getName()); lambdaApplyParameters[i] = Object.class; } Class<?> materializedRetType; Type genericRetType = samMethod.getGenericReturnType(); if (genericRetType instanceof Class<?>) materializedRetType = samMethod.getReturnType(); else if (genericRetType instanceof TypeVariable) materializedRetType = findMaterializedType(((TypeVariable) genericRetType).getName()); else throw new LambdaException( "unexpected generic return type [%s] for SAM [%s]", samMethod.getReturnType(), samType.getName()); this.lambdaSignature = new LambdaSignature<Object>(materializedRetType, vars); }
public static void unProxyObject(Object object) throws ClassNotFoundException, IllegalAccessException, HibernateException, IllegalArgumentException, InvocationTargetException { Method[] methods = object.getClass().getMethods(); for (Method method : methods) { if (method.getName().startsWith("get") && method.getGenericParameterTypes().length == 0) { Class returnType = method.getReturnType(); Object attribute = method.invoke(object, (Object[]) null); if (attribute instanceof HibernateProxy) { HibernateProxy proxy = (HibernateProxy) attribute; attribute = loadLazyObject(proxy); Object newValue = returnType.cast(attribute); IntrospectUtil.invokeSetter( object.getClass(), object, method.getName().substring(3), newValue); } else if (attribute instanceof ListProxy) { ListProxy listProxy = (ListProxy) attribute; List<Object> newList = new ArrayList<>(); for (Object object1 : listProxy) { newList.add(object1); } IntrospectUtil.invokeSetter( object.getClass(), object, method.getName().substring(3), newList); } } } }
@SuppressWarnings("rawtypes") @Override public final E map(R record) { try { E result = instance != null ? instance : constructor.newInstance(); for (int i = 0; i < fields.length; i++) { for (java.lang.reflect.Field member : members[i]) { // [#935] Avoid setting final fields if ((member.getModifiers() & Modifier.FINAL) == 0) { map(record, result, member, i); } } for (java.lang.reflect.Method method : methods[i]) { Class<?> mType = method.getParameterTypes()[0]; Object value = record.getValue(i, mType); // [#3082] Map nested collection types if (value instanceof Collection && List.class.isAssignableFrom(mType)) { Class componentType = (Class) ((ParameterizedType) method.getGenericParameterTypes()[0]) .getActualTypeArguments()[0]; method.invoke(result, Convert.convert((Collection) value, componentType)); } // Default reference types (including arrays) else { method.invoke(result, record.getValue(i, mType)); } } } for (Entry<String, List<RecordMapper<R, Object>>> entry : nested.entrySet()) { String prefix = entry.getKey(); for (RecordMapper<R, Object> mapper : entry.getValue()) { Object value = mapper.map(record); for (java.lang.reflect.Field member : getMatchingMembers(configuration, type, prefix)) { // [#935] Avoid setting final fields if ((member.getModifiers() & Modifier.FINAL) == 0) { map(value, result, member); } } for (Method method : getMatchingSetters(configuration, type, prefix)) { method.invoke(result, value); } } } return result; } catch (Exception e) { throw new MappingException("An error ocurred when mapping record to " + type, e); } }
public Type getGenericType() { if (m_method != null) { return m_method.getGenericParameterTypes()[0]; } else { return m_field.getGenericType(); } }
private List<WebsocketParameterDescriptor> getArguments(Method executedMethod) { List<WebsocketParameterDescriptor> descriptors = parametersCache.get(executedMethod); if (descriptors != null) { return descriptors; } Type[] parameterTypes = executedMethod.getGenericParameterTypes(); Annotation[][] allAnnotations = executedMethod.getParameterAnnotations(); descriptors = new ArrayList<>(parameterTypes.length); for (int i = 0; i < parameterTypes.length; i++) { Type type = parameterTypes[i]; String name = null; JsonPolicyDef.Policy jsonPolicy = null; for (Annotation currentParamAnnotation : allAnnotations[i]) { if (currentParamAnnotation instanceof WsParam) { name = ((WsParam) currentParamAnnotation).value(); } if (currentParamAnnotation instanceof JsonPolicyApply) { jsonPolicy = ((JsonPolicyApply) currentParamAnnotation).value(); } } descriptors.add(new WebsocketParameterDescriptor(name, type, jsonPolicy)); } parametersCache.put(executedMethod, descriptors); return descriptors; }
/** * @param elementName the name of the element * @param descriptor the descriptor in which to add the element * @param method the method associated to the element * @throws IncompatibleFilterException when passed method is not compatible with matching * filter(s) */ private void addElement(String elementName, FilterDescriptor descriptor, Method method) throws IncompatibleFilterException { String lowerElementName = elementName.toLowerCase(); FilterElementDescriptor element = descriptor.getElements().get(lowerElementName); Type[] methodTypes = method.getGenericParameterTypes(); if (element == null || methodTypes.length > element.getParameters().length) { FilterElementParameterDescriptor<?>[] parameters = new FilterElementParameterDescriptor<?>[methodTypes.length]; for (int i = 0; i < methodTypes.length; ++i) { parameters[i] = createFilterElementParameter(method, i, methodTypes[i]); } // Make sure those parameters are compatible with any other matching element if (element != null) { checkCompatible(element, parameters); } element = new FilterElementDescriptor(elementName, parameters); descriptor.getElements().put(lowerElementName, element); } addMethod(element, method); }
/** * Returns the resolved generic parameter types of {@code methodOrConstructor}. * * @param methodOrConstructor a method or constructor defined by this or any supertype. * @since 2.0 */ public List<TypeLiteral<?>> getParameterTypes(Member methodOrConstructor) { Type[] genericParameterTypes; if (methodOrConstructor instanceof Method) { Method method = (Method) methodOrConstructor; Assert.checkArgument( method.getDeclaringClass().isAssignableFrom(rawType), "%s is not defined by a supertype of %s", method, type); genericParameterTypes = method.getGenericParameterTypes(); } else if (methodOrConstructor instanceof Constructor) { Constructor<?> constructor = (Constructor<?>) methodOrConstructor; Assert.checkArgument( constructor.getDeclaringClass().isAssignableFrom(rawType), "%s does not construct a supertype of %s", constructor, type); genericParameterTypes = constructor.getGenericParameterTypes(); } else { throw new IllegalArgumentException("Not a method or a constructor: " + methodOrConstructor); } return resolveAll(genericParameterTypes); }
protected void listVirtualMethods(Class<?> type, List<VirtMeth> out) { if (!CPPObject.class.isAssignableFrom(type)) { return; } Class<?> sup = type.getSuperclass(); if (sup != CPPObject.class) { listVirtualMethods(sup, out); } int nParentMethods = out.size(); Map<Integer, VirtMeth> newVirtuals = new TreeMap<Integer, VirtMeth>(); methods: for (Method method : type.getDeclaredMethods()) { String methodName = method.getName(); Type[] methodParameterTypes = method.getGenericParameterTypes(); for (int iParentMethod = 0; iParentMethod < nParentMethods; iParentMethod++) { VirtMeth pvm = out.get(iParentMethod); Method parentMethod = pvm.definition; if (parentMethod.getDeclaringClass() == type) continue; // was just added in the same listVirtualMethods call ! // if (parentMethod.getAnnotation(Virtual.class) == null) // continue; // not a virtual method, too bad if (parentMethod.getName().equals(methodName) && isOverridenSignature( parentMethod.getGenericParameterTypes(), methodParameterTypes, 0)) { VirtMeth vm = new VirtMeth(); vm.definition = pvm.definition; vm.implementation = method; out.set(iParentMethod, vm); continue methods; } } Virtual virtual = method.getAnnotation(Virtual.class); if (virtual != null) { VirtMeth vm = new VirtMeth(); vm.definition = vm.implementation = method; newVirtuals.put(virtual.value(), vm); } } out.addAll(newVirtuals.values()); }
private void handleMethod( String prefix, Collection<DetailedLink> results, Method m, Class<?> resource, Map<String, Type> parametersMap) throws ClassNotFoundException { if (isRequiresDescription(m)) { Type genericReturnType = m.getGenericReturnType(); Class<?> concreteReturnType = findConcreteType(genericReturnType, resource, parametersMap); if (concreteReturnType == null) { concreteReturnType = m.getReturnType(); } Type[] genericParameterTypes = m.getGenericParameterTypes(); Class<?>[] concreteParameterTypes = m.getParameterTypes(); for (int i = 0; i < concreteParameterTypes.length; i++) { Class<?> concreteParameterType = findConcreteType(genericParameterTypes[i], resource, parametersMap); if (concreteParameterType != null) { concreteParameterTypes[i] = concreteParameterType; } } if (m.isAnnotationPresent(javax.ws.rs.GET.class)) { handleGet(prefix, results, concreteReturnType); } else if (m.isAnnotationPresent(PUT.class)) { handlePut(prefix, results, concreteReturnType); } else if (m.isAnnotationPresent(javax.ws.rs.DELETE.class)) { handleDelete(prefix, results, m); } else if (m.isAnnotationPresent(Path.class)) { String path = m.getAnnotation(Path.class).value(); if (isAction(m)) { handleAction(prefix, results, path, m); } else { if (isSingleEntityResource(m)) { path = "{" + getSingleForm(prefix) + ":id}"; } if (m.getGenericReturnType() instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) m.getGenericReturnType(); addToGenericParamsMap( resource, parameterizedType.getActualTypeArguments(), m.getReturnType().getTypeParameters(), parametersMap); } results.addAll( describe( concreteReturnType, prefix + "/" + path, new HashMap<String, Type>(parametersMap))); } } else { if (m.getName().equals(ADD)) { handleAdd(prefix, results, concreteParameterTypes); } } } }
/** * メソッドの引数型を表現する{@link ParameterizedClassDesc}を作成して返します。 * * @param method メソッド。{@literal null}であってはいけません * @param index 引数の位置 * @param map パラメータ化された型が持つ型変数をキー、型引数を値とする{@link Map}。{@literal null} であってはいけません * @return メソッドの引数型を表現する{@link ParameterizedClassDesc} */ public static ParameterizedClassDesc createParameterizedClassDesc( final Method method, final int index, final Map<TypeVariable<?>, Type> map) { assertArgumentNotNull("method", method); assertArgumentArrayIndex("index", index, method.getParameterTypes().length); assertArgumentNotNull("map", map); return createParameterizedClassDesc(method.getGenericParameterTypes()[index], map); }
public void init(Method method) { this.method = method; argTypes = method.getParameterTypes(); injs = new ParamInjector[argTypes.length]; Annotation[][] annss = method.getParameterAnnotations(); Type[] types = method.getGenericParameterTypes(); for (int i = 0; i < annss.length; i++) { Annotation[] anns = annss[i]; Param param = null; Attr attr = null; IocObj iocObj = null; ReqHeader reqHeader = null; // find @Param & @Attr & @IocObj in current annotations for (int x = 0; x < anns.length; x++) if (anns[x] instanceof Param) { param = (Param) anns[x]; break; } else if (anns[x] instanceof Attr) { attr = (Attr) anns[x]; break; } else if (anns[x] instanceof IocObj) { iocObj = (IocObj) anns[x]; break; } else if (anns[x] instanceof ReqHeader) { reqHeader = (ReqHeader) anns[x]; break; } // If has @Attr if (null != attr) { injs[i] = evalInjectorByAttrScope(attr); continue; } // If has @IocObj if (null != iocObj) { injs[i] = new IocObjInjector(method.getParameterTypes()[i], iocObj.value()); continue; } if (null != reqHeader) { injs[i] = new ReqHeaderInjector(reqHeader.value(), argTypes[i]); continue; } // And eval as default suport types injs[i] = evalInjectorByParamType(argTypes[i]); if (null != injs[i]) continue; // Eval by sub-classes injs[i] = evalInjector(types[i], param); // 子类也不能确定,如何适配这个参数,那么做一个标记,如果 // 这个参数被 ParamInjector 适配到,就会抛错。 // 这个设计是因为了 "路径参数" if (null == injs[i]) { injs[i] = paramNameInject(method, i); } } }
public void addSetter(Method method, List<String> fields) { check(implInterface || !dataTypeIn.isInterface()); checkNotNull(method); checkNotNull(fields); check(!isPrivate(method.getModifiers()), "Setter cannot be private: %s", method); check(method.getGenericParameterTypes().length == fields.size()); check(!setters.containsKey(method)); setters.put(method, fields); }
@Nullable public static <T> ModelType<T> paramType(Method method, int i) { Type[] parameterTypes = method.getGenericParameterTypes(); if (i < parameterTypes.length) { return new Simple<T>(parameterTypes[i]); } else { return null; } }
public boolean matchesSignature(Method method) { if (getArgumentsStackSize() != null && getArgumentsStackSize().intValue() != getArgumentsStackSize(method)) { return false; } if (getMemberName() != null && !getMemberName().toString().equals(getMethodName(method))) { return false; } if (getValueType() != null && !getValueType().matches(method.getReturnType(), annotations(method))) { return false; } Template temp = method.getAnnotation(Template.class); Annotation[][] anns = method.getParameterAnnotations(); // Class<?>[] methodArgTypes = method.getParameterTypes(); Type[] parameterTypes = method.getGenericParameterTypes(); // boolean hasThisAsFirstArgument = BridJ.hasThisAsFirstArgument(method);//methodArgTypes, // anns, true); if (paramTypes != null && !matchesArgs( parameterTypes, anns, temp == null ? 0 : temp.value().length)) // /*, hasThisAsFirstArgument*/)) { return false; } // int thisDirac = hasThisAsFirstArgument ? 1 : 0; /* switch (type) { case Constructor: case Destructor: Annotation ann = method.getAnnotation(type == SpecialName.Constructor ? Constructor.class : Destructor.class); if (ann == null) return false; if (!hasThisAsFirstArgument) return false; if (methodArgTypes.length - thisDirac != 0 ) return false; break; case InstanceMethod: if (!hasThisAsFirstArgument) return false; break; case StaticMethod: if (hasThisAsFirstArgument) return false; break; }*/ return true; }
/** * Populate model from given reflect with method * * @param method the method to analyze * @param methodModel the model to update */ protected void analyzeDtoWithMethod(Method method, MethodModel methodModel) { methodModel.setWith(true); // add the parameter Type fieldType = method.getGenericParameterTypes()[0]; String fieldName = getWithFieldName(method); fieldAttributes.put(fieldName, fieldType); methodModel.setFieldName(fieldName); methodModel.setFieldType(convertType(fieldType)); }
private void validateType() { Type[] types = method.getGenericParameterTypes(); if (types == null || types.length != 1) { throw createInvalidOptionSpecificationException( "Wrong number of arguments, expecting exactly one"); } Type type = types[0]; int listLevel = getListLevel(); verifyType(listLevel, getInnerArgumentType(), type, type); }
private static ParameterizedType getParameterType(Method paramMethod, int paramInt) { paramMethod = paramMethod.getGenericParameterTypes(); if (paramMethod.length > paramInt) { paramMethod = paramMethod[paramInt]; if ((paramMethod instanceof ParameterizedType)) { return (ParameterizedType) paramMethod; } } return null; }
@Test public void get_generic_type_of_interface_method_parameters() throws Exception { Method method = Interface.class.getDeclaredMethod("stringListMethod", List.class); Type[] types = method.getGenericParameterTypes(); ParameterizedType type = (ParameterizedType) types[0]; assertEquals(List.class, type.getRawType()); assertArrayEquals(new Type[] {String.class}, type.getActualTypeArguments()); assertEquals(null, type.getOwnerType()); }
private void walkReferences(Class<?> cls) { if (cls == null) { return; } if (cls.getName().startsWith("java.") || cls.getName().startsWith("javax.")) { return; } // walk the public fields/methods to try and find all the classes. JAXB will only load the // EXACT classes in the fields/methods if they are in a different package. Thus, // subclasses won't be found and the xsi:type stuff won't work at all. // We'll grab the public field/method types and then add the ObjectFactory stuff // as well as look for jaxb.index files in those packages. XmlAccessType accessType = Utils.getXmlAccessType(cls); if (accessType != XmlAccessType.PROPERTY) { // only look for fields if we are instructed to // fields are accessible even if not public, must look at the declared fields // then walk to parents declared fields, etc... Field fields[] = ReflectionUtil.getDeclaredFields(cls); for (Field f : fields) { if (isFieldAccepted(f, accessType)) { XmlJavaTypeAdapter xjta = Utils.getFieldXJTA(f); if (xjta != null) { Type t = Utils.getTypeFromXmlAdapter(xjta); if (t != null) { addType(t); continue; } } addType(f.getGenericType()); } } walkReferences(cls.getSuperclass()); } if (accessType != XmlAccessType.FIELD) { // only look for methods if we are instructed to Method methods[] = ReflectionUtil.getDeclaredMethods(cls); for (Method m : methods) { if (isMethodAccepted(m, accessType)) { XmlJavaTypeAdapter xjta = Utils.getMethodXJTA(m); if (xjta != null) { Type t = Utils.getTypeFromXmlAdapter(xjta); if (t != null) { addType(t); continue; } } addType(m.getGenericReturnType()); for (Type t : m.getGenericParameterTypes()) { addType(t); } } } } }
public void setFactory(Method methodFactory, List<String> fields) { check(implInterface || !dataTypeIn.isInterface()); checkNotNull(methodFactory); checkNotNull(fields); check(this.factory == null, "Factory is already set: %s", this.factory); check(!isPrivate(methodFactory.getModifiers()), "Factory cannot be private: %s", methodFactory); check(isStatic(methodFactory.getModifiers()), "Factory must be static: %s", methodFactory); check(methodFactory.getGenericParameterTypes().length == fields.size()); this.factory = methodFactory; this.factoryParams = fields; }
public static void unLazyObjectRecursively(Object object, int level) throws ClassNotFoundException, IllegalAccessException, HibernateException, IllegalArgumentException, InvocationTargetException { Class clazz = object.getClass(); Annotation ano = clazz.getAnnotation(Entity.class); // if (ano!=null){ for (Method method : clazz.getMethods()) { if (method.getName().startsWith("get") && method.getGenericParameterTypes().length == 0) { try { String field = method.getName().substring(3); ano = null; for (Field field_ : clazz.getDeclaredFields()) { if (field_.getName().equalsIgnoreCase(field)) { ano = field_.getAnnotation(XStreamOmitField.class); } } Object attribute = method.invoke(object, (Object[]) null); if (ano != null) { IntrospectUtil.invokeSetter(clazz, object, field, null); } else if (attribute instanceof HibernateProxy || attribute instanceof ListProxy) { if (attribute instanceof HibernateProxy) { attribute = loadLazyObject((HibernateProxy) attribute); } IntrospectUtil.invokeSetter(clazz, object, field, attribute); } else if (attribute != null && attribute.getClass().getName().startsWith("org.gaia") && !attribute.equals(object) && level > 0) { unLazyObjectRecursively(attribute, level - 1); } else if (attribute != null && attribute instanceof Collection) { if (level > 0) { Collection collec = (Collection) attribute; try { for (Object obj : collec) { unLazyObjectRecursively(obj, level - 1); } } catch (Exception e) { IntrospectUtil.invokeSetter(clazz, object, field, null); } } else { // IntrospectUtil.invokeSetter(clazz, object, field, // null); } } } catch (Exception e) { logger.error( "Error while calling method " + method.getName() + " on " + object.toString()); logger.error(StringUtils.formatErrorMessage(e)); } } } // } }
public PluginPoint createFrom(ServerPlugin plugin, Method method, Class<?> discovery) { ResultConverter result = ResultConverter.get(method.getGenericReturnType()); Type[] types = method.getGenericParameterTypes(); Annotation[][] annotations = method.getParameterAnnotations(); SourceExtractor sourceExtractor = null; DataExtractor[] extractors = new DataExtractor[types.length]; for (int i = 0; i < types.length; i++) { Description description = null; Parameter param = null; Source source = null; for (Annotation annotation : annotations[i]) { if (annotation instanceof Description) { description = (Description) annotation; } else if (annotation instanceof Parameter) { param = (Parameter) annotation; } else if (annotation instanceof Source) { source = (Source) annotation; } } if (param != null && source != null) { throw new IllegalStateException( String.format( "Method parameter %d of %s cannot be retrieved as both Parameter and Source", Integer.valueOf(i), method)); } else if (source != null) { if (types[i] != discovery) { throw new IllegalStateException( "Source parameter type (" + types[i] + ") must equal the discovery type (" + discovery.getName() + ")."); } if (sourceExtractor != null) { throw new IllegalStateException( "Server Extension methods may have at most one Source parameter."); } extractors[i] = sourceExtractor = new SourceExtractor(source, description); } else if (param != null) { extractors[i] = parameterExtractor(types[i], param, description); } else { throw new IllegalStateException( "Parameters of Server Extension methods must be annotated as either Source or Parameter."); } } return new PluginMethod( nameOf(method), discovery, plugin, result, method, extractors, method.getAnnotation(Description.class)); }
@Override public void compile(boolean result, IEnvironmentMethod environment) { if (!result) return; Method method = interfaceClass.getMethods()[0]; // generate class String clsName = environment.makeClassName(); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); cw.visit( Opcodes.V1_6, Opcodes.ACC_PUBLIC, clsName, null, "java/lang/Object", new String[] {internal(interfaceClass)}); MethodOutput constructor = new MethodOutput(cw, Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); constructor.start(); constructor.loadObject(0); constructor.invokeSpecial("java/lang/Object", "<init>", "()V"); constructor.ret(); constructor.end(); MethodOutput output = new MethodOutput(cw, Opcodes.ACC_PUBLIC, method.getName(), descriptor(method), null, null); IEnvironmentClass environmentClass = new EnvironmentClass(cw, environment); IEnvironmentMethod environmentMethod = new EnvironmentMethod(output, environmentClass); for (int i = 0; i < arguments.size(); i++) { environmentMethod.putValue( arguments.get(i).getName(), new SymbolArgument(i + 1, environment.getType(method.getGenericParameterTypes()[i])), getPosition()); } output.start(); for (Statement statement : statements) { statement.compile(environmentMethod); } output.ret(); output.end(); environment.putClass(clsName, cw.toByteArray()); // make class instance environment.getOutput().newObject(clsName); environment.getOutput().dup(); environment.getOutput().construct(clsName); }
/** * Get the DBus property signature. * * @param method the method */ public static String getPropertySig(Method method) throws AnnotationBusException { Type type = null; if (method.getName().startsWith("get")) { type = method.getGenericReturnType(); } else if (method.getName().startsWith("set")) { type = method.getGenericParameterTypes()[0]; } BusProperty busProperty = method.getAnnotation(BusProperty.class); if (busProperty != null && busProperty.signature().length() > 0) { return Signature.typeSig(type, busProperty.signature()); } return Signature.typeSig(type, null); }