/** * Gets the named method with a method signature matching classTypes from the given insance * and returns it as an object. * @param instance the object instance * @param methodName the method name * @param classTypes the method argument types */ public static Method getMethod(Object instance, String methodName, Class[] classTypes) throws NoSuchMethodException { Method accessMethod = getMethod(instance.getClass(), methodName, classTypes); accessMethod.setAccessible(true); return accessMethod; }
/** * Sets the value of the named static field. * * @param c the class containing the static field * @param fieldName the name of the field */ public static void setStaticValue(Class c, String fieldName, Object value) throws IllegalAccessException, NoSuchFieldException { Field field = getField(c, fieldName); field.setAccessible(true); field.set(null, value); }
/** * Calls a method on the given object instance with the given argument. * * @param instance the object instance * @param methodName the name of the method to invoke * @param arg the argument to pass to the method * @see PrivilegedAccessor#invokeMethod(Object,String,Object[]) */ public static Object invokeMethod(Object instance, String methodName, Object arg) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Object[] args = new Object[1]; args[0] = arg; return invokeMethod(instance, methodName, args); }
Method m = PrivilegedAccessor.getMethod((DefaultSingleTargetProductDialog) this, "updateValueSet", new Class[]{PropertyDescriptor.class, Product.class}); Object obj = PrivilegedAccessor.getValue(this, "operatorDescriptor"); OperatorDescriptor opDescriptor = (OperatorDescriptor) obj; System.out.println(opDescriptor.getName());
if (args == null) args = new Object[] {}; Class[] classTypes = getClassArray(args); Class c = Class.forName(classname); Constructor[] constructors = c.getConstructors(); Constructor constructor = constructors[i]; Class[] paramTypes = constructor.getParameterTypes(); if (compare(paramTypes, args)) { return constructor.newInstance(args);
public static void registerNativePaths(String... paths) { if (paths == null || paths.length == 0) return; String propertyValue = System.getProperty(ENV_LIB_PATH); StringBuilder builder = new StringBuilder(); for (String path : paths) { if (!StringUtils.isNullOrEmpty(propertyValue) && (!propertyValue.contains(path) || propertyValue.contains(path + File.separator))) { builder.append(path).append(File.pathSeparator); } } if (!StringUtils.isNullOrEmpty(propertyValue)) { propertyValue = builder.toString() + propertyValue; } else { propertyValue = builder.toString(); } System.setProperty(ENV_LIB_PATH, propertyValue); try { PrivilegedAccessor.setStaticValue(ClassLoader.class, "sys_paths", null); } catch (Exception e) { throw new RuntimeException(e); } }
if (args == null) args = new Object[] {}; Class[] classTypes = getClassArray(args); Method[] methods = instance.getClass().getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; Class[] paramTypes = method.getParameterTypes(); if (method.getName().equals(methodName) && compare(paramTypes, args)) { return method.invoke(instance, args);
/** * Return the named method with a method signature matching classTypes from the given class. * @param thisClass the class containing the method * @param methodName the name of the method * @param classTypes the method argument types */ public static Method getMethod(Class thisClass, String methodName, Class[] classTypes) throws NoSuchMethodException { if (thisClass == null) throw new NoSuchMethodException("Invalid method : " + methodName); try { return thisClass.getDeclaredMethod(methodName, classTypes); } catch (NoSuchMethodException e) { return getMethod(thisClass.getSuperclass(), methodName, classTypes); } }
/** * Gets the value of the named static field and returns it as an object. * * @param c the class containing the static field * @param fieldName the name of the field * @return an object representing the value of the field */ public static Object getStaticValue(Class c, String fieldName) throws IllegalAccessException, NoSuchFieldException { Field field = getField(c, fieldName); field.setAccessible(true); return field.get(null); }
/** * Sets values according to the selected product. */ private void processSelectedProducts(int productId, String propertyName) { DefaultIOParametersPanel ioParametersPanel = getDefaultIOParametersPanel(); List<SourceProductSelector> sourceProductSelectorList = ioParametersPanel.getSourceProductSelectorList(); Product selectedProduct = sourceProductSelectorList.get(productId).getSelectedProduct(); try { if (selectedProduct != null) { //String bands = String.join(",", selectedProduct.getBandGroup().getNodeNames()); BindingContext bindingContext = getBindingContext(); PropertySet propertySet = bindingContext.getPropertySet(); //PrivilegedAccessor.invokeMethod((DefaultSingleTargetProductDialog)this, "updateValueSet", new Object[]{selectedProduct, propertySet.getDescriptor(propertyName)}); Method m = PrivilegedAccessor.getMethod((DefaultSingleTargetProductDialog) this, "updateValueSet", new Class[]{PropertyDescriptor.class, Product.class}); m.invoke(null, new Object[]{propertySet.getDescriptor(propertyName), selectedProduct}); //propertySet.getDescriptor(propertyName)//.getDescriptor().setValueSet(ValueSet.parseValueSet(bands, String.class)); } } catch (Exception ex) { throw new OperatorException("Could not update band list as parameter: " + ex.getMessage()); } //update also the target product name according to slave if (productId != SLAVE_PRODUCT_ID) { selectedProduct = sourceProductSelectorList.get(SLAVE_PRODUCT_ID).getSelectedProduct(); } if (selectedProduct != null) { TargetProductSelectorModel targetProductSelectorModel = getTargetProductSelector().getModel(); targetProductSelectorModel.setProductName(selectedProduct.getName() + getTargetProductNameSuffix()); } }
public static void setValue(Object instance, String fieldName, Object value) throws IllegalAccessException, NoSuchFieldException { Field field = getField(instance.getClass(), fieldName); field.setAccessible(true); field.set(instance, value); }
/** * Gets the value of the named field and returns it as an object. * * @param instance the object instance * @param fieldName the name of the field * @return an object representing the value of the field */ public static Object getValue(Object instance, String fieldName) throws IllegalAccessException, NoSuchFieldException { Field field = getField(instance.getClass(), fieldName); field.setAccessible(true); return field.get(instance); }
/** * Return the named field from the given class. * @param thisClass The class * @param fieldName The field name */ public static Field getField(Class thisClass, String fieldName) throws NoSuchFieldException { if (thisClass == null) throw new NoSuchFieldException("Invalid field : " + fieldName); try { return thisClass.getDeclaredField(fieldName); } catch (NoSuchFieldException e) { return getField(thisClass.getSuperclass(), fieldName); } }