private static void validate(Class target, String[] getters, String[] setters, Class[] types, Method[] getters_out, Method[] setters_out) { int i = -1; if (setters.length != types.length || getters.length != types.length) { throw new BulkBeanException("accessor array length must be equal type array length", i); } try { for (i = 0; i < types.length; i++) { if (getters[i] != null) { Method method = ReflectUtils.findDeclaredMethod(target, getters[i], null); if (method.getReturnType() != types[i]) { throw new BulkBeanException("Specified type " + types[i] + " does not match declared type " + method.getReturnType(), i); } if (Modifier.isPrivate(method.getModifiers())) { throw new BulkBeanException("Property is private", i); } getters_out[i] = method; } if (setters[i] != null) { Method method = ReflectUtils.findDeclaredMethod(target, setters[i], new Class[]{ types[i] }); if (Modifier.isPrivate(method.getModifiers()) ){ throw new BulkBeanException("Property is private", i); } setters_out[i] = method; } } } catch (NoSuchMethodException e) { throw new BulkBeanException("Cannot find specified property", i); } }
public void callValidator(final AQuery $, final Ajax ajax) throws VolleyError { Map<Annotation, java.lang.reflect.Method> method = ReflectUtils.getMethodsByAnnotation(Handler.class, getClass()); for (Map.Entry<Annotation, java.lang.reflect.Method> entry : method.entrySet()) { try { entry .getValue() .invoke( this, ReflectUtils.fillParamsByAnnotations( entry.getValue(), new ReflectUtils.ParamInjector() { @Override public Object onInject( Class paramType, List<? extends Annotation> annotations, int position) { try { return scanAnnotation($, paramType, annotations.get(0), ajax); } catch (Exception e) { $.log.i(e); return null; } } })); } catch (InvocationTargetException e) { if (e.getTargetException() instanceof VolleyError) { throw ((VolleyError) e.getTargetException()); } $.log.i(e.getTargetException()); } catch (Exception e) { $.log.i(e); } } }
@Override public Object transform(final Object value) { if (value instanceof Method) { return ReflectUtils.getMethodInfo((Method) value); } else if (value instanceof Constructor) { return ReflectUtils.getMethodInfo((Constructor) value); } else { throw new IllegalArgumentException("cannot get method info for " + value); } }
public void callModelValidator(final AQuery $, final Ajax ajax) throws VolleyError { Map<Annotation, java.lang.reflect.Method> methodMap = ReflectUtils.getMethodsByAnnotation(Handler.class, getClass()); for (Map.Entry<Annotation, java.lang.reflect.Method> entry : methodMap.entrySet()) { java.lang.reflect.Method method = entry.getValue(); for (int i = 0; i < method.getParameterTypes().length; i++) { Class type = method.getParameterTypes()[i]; if (method.getParameterAnnotations()[i][0] instanceof ResponseBody && type != String.class && type != byte[].class && Checker.class.isAssignableFrom(type)) { try { Checker checker = (Checker) new Gson().fromJson(new String(ajax.getResponseBody(), "utf-8"), type); if (checker.getValidator().validate()) { $.log.i("has warning"); } } catch (UnsupportedEncodingException e) { $.log.i(e); throw new IllegalDataError("Unsupported encoding bytes"); } } } } }
@Override public void updateUI() { if (getUI() == null || !(getUI() instanceof WebComboBoxUI)) { try { setUI((WebComboBoxUI) ReflectUtils.createInstance(WebLookAndFeel.comboBoxUI)); } catch (final Throwable e) { Log.error(this, e); setUI(new WebComboBoxUI()); } } else { setUI(getUI()); } }
public static void tryToSetProperty(Object object, String propertyName, String propertyValue) { String setterName = ReflectUtils.getSetterMethodName(propertyName); Method[] methods = object.getClass().getMethods(); for (Method method : methods) { if (!method.getName().equals(setterName)) { continue; } Class[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length != 1) { continue; } invokeMethod(object, method, parameterTypes[0], propertyValue); } }
private static Component createSourceButton(final WebLookAndFeelDemo owner, Example example) { final Class classType = example.getClass(); WebButton sourceButton = WebButton.createIconWebButton(JarEntry.javaIcon); TooltipManager.setTooltip( sourceButton, JarEntry.javaIcon, ReflectUtils.getJavaClassName(classType), TooltipWay.up); sourceButton.setRolloverDecoratedOnly(true); sourceButton.setFocusable(false); sourceButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { owner.showSource(classType); } }); return new CenterPanel(sourceButton, false, true); }
private void generateSet(final Class target, final Method[] setters) { // setPropertyValues CodeEmitter e = begin_method(Constants.ACC_PUBLIC, SET_PROPERTY_VALUES, null); if (setters.length > 0) { Local index = e.make_local(Type.INT_TYPE); e.push(0); e.store_local(index); e.load_arg(0); e.checkcast(Type.getType(target)); e.load_arg(1); Block handler = e.begin_block(); int lastIndex = 0; for (int i = 0; i < setters.length; i++) { if (setters[i] != null) { MethodInfo setter = ReflectUtils.getMethodInfo(setters[i]); int diff = i - lastIndex; if (diff > 0) { e.iinc(index, diff); lastIndex = i; } e.dup2(); e.aaload(i); e.unbox(setter.getSignature().getArgumentTypes()[0]); e.invoke(setter); } } handler.end(); e.return_value(); e.catch_exception(handler, Constants.TYPE_THROWABLE); e.new_instance(BULK_BEAN_EXCEPTION); e.dup_x1(); e.swap(); e.load_local(index); e.invoke_constructor(BULK_BEAN_EXCEPTION, CSTRUCT_EXCEPTION); e.athrow(); } else { e.return_value(); } e.end_method(); }
private void generateGet(final Class target, final Method[] getters) { CodeEmitter e = begin_method(Constants.ACC_PUBLIC, GET_PROPERTY_VALUES, null); if (getters.length >= 0) { e.load_arg(0); e.checkcast(Type.getType(target)); Local bean = e.make_local(); e.store_local(bean); for (int i = 0; i < getters.length; i++) { if (getters[i] != null) { MethodInfo getter = ReflectUtils.getMethodInfo(getters[i]); e.load_arg(1); e.push(i); e.load_local(bean); e.invoke(getter); e.box(getter.getSignature().getReturnType()); e.aastore(); } } } e.return_value(); e.end_method(); }
public static JarStructure createJarStructure(final WebProgressDialog progress) { // Download listener in case of remote jar-file (for e.g. demo loaded from .jnlp) FileDownloadListener listener = new FileDownloadListener() { private int totalSize = 0; @Override public void sizeDetermined(int totalSize) { // Download started this.totalSize = totalSize; updateProgress(0); } @Override public void partDownloaded(int totalBytesDownloaded) { // Some part loaded updateProgress(totalBytesDownloaded); } @Override public boolean shouldStopDownload() { return false; } private void updateProgress(int downloaded) { // Updating progress text progress.setText( "<html>Loading source files... <b>" + FileUtils.getFileSizeString(downloaded, 1) + "</b> of <b>" + FileUtils.getFileSizeString(totalSize, 1) + "</b> done</html>"); } @Override public void fileDownloaded(File file) { // Updating progress text progress.setText("Creating source files structure..."); } @Override public void fileDownloadFailed(Throwable e) { // Updating progress text progress.setText("Filed to download source files"); } }; // Creating structure using any of classes contained inside jar progress.setText("Creating source files structure..."); List<String> extensions = Arrays.asList(".java", ".png", ".gif", ".jpg", ".txt", ".xml"); List<String> packages = Arrays.asList("com/alee", "licenses"); JarStructure jarStructure = ReflectUtils.getJarStructure(ExamplesManager.class, extensions, packages, listener); // Updating some of package icons jarStructure.setPackageIcon( WebLookAndFeelDemo.class.getPackage(), new ImageIcon(WebLookAndFeel.getImages().get(0))); for (ExampleGroup exampleGroup : getExampleGroups()) { jarStructure.setClassIcon(exampleGroup.getClass(), (ImageIcon) exampleGroup.getGroupIcon()); } return jarStructure; }
public void generateClass(ClassVisitor v) { ClassEmitter ce = new ClassEmitter(v); Method newInstance = ReflectUtils.findNewInstance(keyInterface); if (!newInstance.getReturnType().equals(Object.class)) { throw new IllegalArgumentException("newInstance method must return Object"); } Type[] parameterTypes = TypeUtils.getTypes(newInstance.getParameterTypes()); ce.begin_class( Constants.V1_2, Constants.ACC_PUBLIC, getClassName(), KEY_FACTORY, new Type[] {Type.getType(keyInterface)}, Constants.SOURCE_FILE); EmitUtils.null_constructor(ce); EmitUtils.factory_method(ce, ReflectUtils.getSignature(newInstance)); int seed = 0; CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, TypeUtils.parseConstructor(parameterTypes), null); e.load_this(); e.super_invoke_constructor(); e.load_this(); for (int i = 0; i < parameterTypes.length; i++) { seed += parameterTypes[i].hashCode(); ce.declare_field( Constants.ACC_PRIVATE | Constants.ACC_FINAL, getFieldName(i), parameterTypes[i], null); e.dup(); e.load_arg(i); e.putfield(getFieldName(i)); } e.return_value(); e.end_method(); // hash code e = ce.begin_method(Constants.ACC_PUBLIC, HASH_CODE, null); int hc = (constant != 0) ? constant : PRIMES[(int) (Math.abs(seed) % PRIMES.length)]; int hm = (multiplier != 0) ? multiplier : PRIMES[(int) (Math.abs(seed * 13) % PRIMES.length)]; e.push(hc); for (int i = 0; i < parameterTypes.length; i++) { e.load_this(); e.getfield(getFieldName(i)); EmitUtils.hash_code(e, parameterTypes[i], hm, customizer); } e.return_value(); e.end_method(); // equals e = ce.begin_method(Constants.ACC_PUBLIC, EQUALS, null); Label fail = e.make_label(); e.load_arg(0); e.instance_of_this(); e.if_jump(e.EQ, fail); for (int i = 0; i < parameterTypes.length; i++) { e.load_this(); e.getfield(getFieldName(i)); e.load_arg(0); e.checkcast_this(); e.getfield(getFieldName(i)); EmitUtils.not_equals(e, parameterTypes[i], fail, customizer); } e.push(1); e.return_value(); e.mark(fail); e.push(0); e.return_value(); e.end_method(); // toString e = ce.begin_method(Constants.ACC_PUBLIC, TO_STRING, null); e.new_instance(Constants.TYPE_STRING_BUFFER); e.dup(); e.invoke_constructor(Constants.TYPE_STRING_BUFFER); for (int i = 0; i < parameterTypes.length; i++) { if (i > 0) { e.push(", "); e.invoke_virtual(Constants.TYPE_STRING_BUFFER, APPEND_STRING); } e.load_this(); e.getfield(getFieldName(i)); EmitUtils.append_string(e, parameterTypes[i], EmitUtils.DEFAULT_DELIMITERS, customizer); } e.invoke_virtual(Constants.TYPE_STRING_BUFFER, TO_STRING); e.return_value(); e.end_method(); ce.end_class(); }
protected Object firstInstance(Class type) { return ReflectUtils.newInstance(type); }
public void generateClass(ClassVisitor v) throws NoSuchMethodException { Method proxy = ReflectUtils.findInterfaceMethod(iface); final Method method = targetClass.getMethod(methodName, proxy.getParameterTypes()); if (!proxy.getReturnType().isAssignableFrom(method.getReturnType())) { throw new IllegalArgumentException("incompatible return types"); } MethodInfo methodInfo = ReflectUtils.getMethodInfo(method); boolean isStatic = TypeUtils.isStatic(methodInfo.getModifiers()); if ((target == null) ^ isStatic) { throw new IllegalArgumentException( "Static method " + (isStatic ? "not " : "") + "expected"); } ClassEmitter ce = new ClassEmitter(v); CodeEmitter e; ce.begin_class( Constants.V1_2, Constants.ACC_PUBLIC, getClassName(), METHOD_DELEGATE, new Type[] {Type.getType(iface)}, Constants.SOURCE_FILE); ce.declare_field(Constants.PRIVATE_FINAL_STATIC, "eqMethod", Constants.TYPE_STRING, null); EmitUtils.null_constructor(ce); // generate proxied method MethodInfo proxied = ReflectUtils.getMethodInfo(iface.getDeclaredMethods()[0]); int modifiers = Constants.ACC_PUBLIC; if ((proxied.getModifiers() & Constants.ACC_VARARGS) == Constants.ACC_VARARGS) { modifiers |= Constants.ACC_VARARGS; } e = EmitUtils.begin_method(ce, proxied, modifiers); e.load_this(); e.super_getfield("target", Constants.TYPE_OBJECT); e.checkcast(methodInfo.getClassInfo().getType()); e.load_args(); e.invoke(methodInfo); e.return_value(); e.end_method(); // newInstance e = ce.begin_method(Constants.ACC_PUBLIC, NEW_INSTANCE, null); e.new_instance_this(); e.dup(); e.dup2(); e.invoke_constructor_this(); e.getfield("eqMethod"); e.super_putfield("eqMethod", Constants.TYPE_STRING); e.load_arg(0); e.super_putfield("target", Constants.TYPE_OBJECT); e.return_value(); e.end_method(); // static initializer e = ce.begin_static(); e.push(methodInfo.getSignature().toString()); e.putfield("eqMethod"); e.return_value(); e.end_method(); ce.end_class(); }
protected Object firstInstance(Class type) { return ((MethodDelegate) ReflectUtils.newInstance(type)).newInstance(target); }
protected ProtectionDomain getProtectionDomain() { return ReflectUtils.getProtectionDomain(targetClass); }