List of usage examples for java.beans PropertyDescriptor getName
public String getName()
From source file:Main.java
public static void main(String[] argv) throws Exception { BeanInfo beanInfo = Introspector.getBeanInfo(Fruit.class); PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor pd : pds) { String propertyName = pd.getName(); System.out.println("propertyName = " + propertyName); }//from w w w . j a v a2 s . c o m }
From source file:Main.java
public static void main(String[] argv) throws Exception { BeanInfo bi = Introspector.getBeanInfo(BeanToXmlTransient.class); PropertyDescriptor[] pds = bi.getPropertyDescriptors(); for (int i = 0; i < pds.length; i++) { PropertyDescriptor propertyDescriptor = pds[i]; if (propertyDescriptor.getName().equals("itemQuantities")) { propertyDescriptor.setValue("transient", Boolean.TRUE); }//from w w w.j a v a 2s.c o m } BeanToXmlTransient bean = new BeanToXmlTransient(); bean.setId(new Long(1)); bean.setItemName("Item"); bean.setItemColour("Red"); bean.setItemQuantities(new Integer(100)); XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("BeanTransient.xml"))); encoder.writeObject(bean); encoder.close(); }
From source file:tetrad.rrd.ReflectTest.java
/** * @param args/*from ww w .java2s. c o m*/ * @throws IllegalAccessException * @throws IllegalArgumentException * @throws InvocationTargetException */ public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { // TODO Auto-generated method stub ServerStatus serverstatus = new ServerStatus(); serverstatus.setDeviceCode(12); serverstatus.setType("a"); BeanWrapper wrapper = new BeanWrapperImpl(serverstatus); PropertyDescriptor[] ps = wrapper.getPropertyDescriptors(); for (PropertyDescriptor p : ps) { String s = p.getName(); System.out.println(s + " : " + wrapper.getPropertyValue(s)); } // Field[] fields = serverstatus.getClass().getDeclaredFields(); // Method[] methods = serverstatus.getClass().getMethods(); // // int idex = 0; // for (Field field : fields) { // System.out.println("name : " + field.getName()); // System.out.println("name : " + field.getType()); // Method method = methods[idex]; // if (method.getReturnType() == String.class) { // System.out.println(method.invoke(method.getName(), "")); // } // Object objValue = field.get(Object.class); // if (objValue instanceof java.lang.String) { // System.out.println(objValue.toString()); // } else if (objValue instanceof java.lang.Integer) { // System.out.println(((java.lang.Integer) objValue).intValue()); // } else if (objValue instanceof java.lang.Double) { // System.out.println(((java.lang.Double) objValue).doubleValue()); // } else if (objValue instanceof java.lang.Float) { // System.out.println(((java.lang.Float) objValue).floatValue()); // } else { // System.out.println(objValue); // } // idex++; // } }
From source file:name.ikysil.beanpathdsl.sandbox.describe.Main.java
/** * @param args the command line arguments */// www . jav a 2 s .co m public static void main(String[] args) { PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean(); Reflections reflections = new Reflections(TestBean.class, new SubTypesScanner(false)); Set<Class<?>> classes = reflections.getSubTypesOf(Object.class); for (Class<?> clazz : classes) { System.out.println(String.format("Class name: %s", clazz.getName())); System.out.println(String.format("Class simple name: %s", clazz.getSimpleName())); System.out.println(String.format("Class canonical name: %s", clazz.getCanonicalName())); PropertyDescriptor[] pds = propertyUtilsBean.getPropertyDescriptors(clazz); for (PropertyDescriptor pd : pds) { System.out.println(String.format(" Property name: %s", pd.getName())); Class<?> pc = pd.getPropertyType(); System.out.println(String.format(" Class name: %s", pc.getName())); System.out.println(String.format(" Class simple name: %s", pc.getSimpleName())); System.out.println(String.format(" Class canonical name: %s", pc.getCanonicalName())); } } }
From source file:generators.ExpressionMetaValidator.java
public static void main(String[] args) { ClassicEngineBoot.getInstance().start(); int invalidExpressionsCounter = 0; int deprecatedExpressionsCounter = 0; final HashNMap expressionsByGroup = new HashNMap(); ExpressionRegistry expressionRegistry = ExpressionRegistry.getInstance(); final ExpressionMetaData[] allExpressions = expressionRegistry.getAllExpressionMetaDatas(); for (int i = 0; i < allExpressions.length; i++) { final ExpressionMetaData expression = allExpressions[i]; if (expression == null) { logger.warn("Null Expression encountered"); continue; }/*from w ww . j a va2s . c o m*/ missingProperties.clear(); final Class type = expression.getExpressionType(); if (type == null) { logger.warn("Expression class is null"); } logger.debug("Processing " + type); final Class resultType = expression.getResultType(); if (resultType == null) { logger.warn("Expression '" + expression.getExpressionType() + " is null"); } try { final BeanInfo beanInfo = expression.getBeanDescriptor(); if (beanInfo == null) { logger.warn( "Expression '" + expression.getExpressionType() + ": Cannot get BeanDescriptor: Null"); } } catch (IntrospectionException e) { logger.warn("Expression '" + expression.getExpressionType() + ": Cannot get BeanDescriptor", e); } final Locale locale = Locale.getDefault(); final String displayName = expression.getDisplayName(locale); if (isValid(displayName, expression.getName()) == false) { logger.warn("Expression '" + expression.getExpressionType() + ": No valid display name"); } if (expression.isDeprecated()) { deprecatedExpressionsCounter += 1; final String deprecateMessage = expression.getDeprecationMessage(locale); if (isValid(deprecateMessage, "Use a Formula instead") == false) { logger.warn("Expression '" + expression.getExpressionType() + ": No valid deprecate message"); } } final String grouping = expression.getGrouping(locale); if (isValid(grouping, "Group") == false) { logger.warn("Expression '" + expression.getExpressionType() + ": No valid grouping message"); } expressionsByGroup.add(grouping, expression); final ExpressionPropertyMetaData[] properties = expression.getPropertyDescriptions(); for (int j = 0; j < properties.length; j++) { final ExpressionPropertyMetaData propertyMetaData = properties[j]; final String name = propertyMetaData.getName(); if (StringUtils.isEmpty(name)) { logger.warn("Expression '" + expression.getExpressionType() + ": Property without a name"); } final String propertyDisplayName = propertyMetaData.getDisplayName(locale); if (isValid(propertyDisplayName, name) == false) { logger.warn("Expression '" + expression.getExpressionType() + ": Property " + propertyMetaData.getName() + ": No DisplayName"); } final String propertyGrouping = propertyMetaData.getGrouping(locale); if (isValid(propertyGrouping, "Group") == false) { logger.warn("Expression '" + expression.getExpressionType() + ": Property " + propertyMetaData.getName() + ": Grouping is not valid"); } final int groupingOrdinal = propertyMetaData.getGroupingOrdinal(locale); if (groupingOrdinal == Integer.MAX_VALUE) { if (propertyMetaData instanceof DefaultExpressionMetaData) { final DefaultExpressionPropertyMetaData demd = (DefaultExpressionPropertyMetaData) propertyMetaData; missingProperties.add(demd.getKeyPrefix() + "grouping.ordinal=1000"); } logger.warn("Expression '" + expression.getExpressionType() + ": Property " + propertyMetaData.getName() + ": Grouping ordinal is not valid"); } final int ordinal = propertyMetaData.getItemOrdinal(locale); if (groupingOrdinal == Integer.MAX_VALUE) { if (propertyMetaData instanceof DefaultExpressionMetaData) { final DefaultExpressionPropertyMetaData demd = (DefaultExpressionPropertyMetaData) propertyMetaData; missingProperties.add(demd.getKeyPrefix() + "ordinal=1000"); } logger.warn("Expression '" + expression.getExpressionType() + ": Property " + propertyMetaData.getName() + ": Ordinal is not valid"); } final String propertyDescription = propertyMetaData.getDescription(locale); if (isValid(propertyDescription, "") == false) { logger.warn("Expression '" + expression.getExpressionType() + ": Property " + propertyMetaData.getName() + ": Description is not valid"); } final String propertyDeprecated = propertyMetaData.getDeprecationMessage(locale); if (isValid(propertyDeprecated, "") == false) { logger.warn("Expression '" + expression.getExpressionType() + ": Property " + propertyMetaData.getName() + ": Deprecation is not valid"); } final String role = propertyMetaData.getPropertyRole(); if (isValid(role, "Value") == false) { logger.warn("Expression '" + expression.getExpressionType() + ": Property " + propertyMetaData.getName() + ": Role is not valid"); } final Class propertyType = propertyMetaData.getPropertyType(); if (propertyType == null) { logger.warn("Expression '" + expression.getExpressionType() + ": Property " + propertyMetaData.getName() + ": Property Type is not valid"); } // should not crash! final PropertyDescriptor propertyDescriptor = propertyMetaData.getBeanDescriptor(); if (propertyMetaData.isDeprecated()) { final String deprecateMessage = propertyMetaData.getDeprecationMessage(locale); if (isValid(deprecateMessage, "Deprecated") == false) { logger.warn("Expression '" + expression.getExpressionType() + ": Property " + propertyMetaData.getName() + ": No valid deprecate message"); } } } try { final BeanInfo beanInfo = Introspector.getBeanInfo(expression.getExpressionType()); final PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors(); for (int propIdx = 0; propIdx < descriptors.length; propIdx++) { final PropertyDescriptor descriptor = descriptors[propIdx]; final String key = descriptor.getName(); if ("runtime".equals(key)) { continue; } if ("active".equals(key)) { continue; } if ("preserve".equals(key)) { continue; } if (descriptor.getReadMethod() == null || descriptor.getWriteMethod() == null) { continue; } if (expression.getPropertyDescription(key) == null) { logger.warn("Expression '" + expression.getExpressionType() + ": No property definition for " + key); missingPropertyDefs.add(" <property name=\"" + key + "\" mandatory=\"false\" preferred=\"false\" value-role=\"Value\" expert=\"false\" hidden=\"false\"/>"); } } } catch (Throwable e) { logger.warn("Expression '" + expression.getExpressionType() + ": Cannot get BeanDescriptor", e); } System.err.flush(); try { Thread.sleep(25); } catch (InterruptedException e) { } for (int x = 0; x < missingProperties.size(); x++) { final String property = (String) missingProperties.get(x); System.out.println(property); } for (int j = 0; j < missingPropertyDefs.size(); j++) { final String def = (String) missingPropertyDefs.get(j); System.out.println(def); } if (missingProperties.isEmpty() == false || missingPropertyDefs.isEmpty() == false) { invalidExpressionsCounter += 1; missingProperties.clear(); missingPropertyDefs.clear(); } System.out.flush(); try { Thread.sleep(25); } catch (InterruptedException e) { } } logger.info("Validated " + allExpressions.length + " expressions. Invalid: " + invalidExpressionsCounter + " Deprecated: " + deprecatedExpressionsCounter); final Object[] keys = expressionsByGroup.keySet().toArray(); Arrays.sort(keys); for (int i = 0; i < keys.length; i++) { final Object key = keys[i]; logger.info("Group: '" + key + "' Size: " + expressionsByGroup.getValueCount(key)); final Object[] objects = expressionsByGroup.toArray(key); for (int j = 0; j < objects.length; j++) { ExpressionMetaData metaData = (ExpressionMetaData) objects[j]; logger.info(" " + metaData.getExpressionType()); } } }
From source file:com.eviware.soapui.monitor.PropertySupport.java
public static void applySystemProperties(Object target, String scope, ModelItem modelItem) { PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(target); DefaultPropertyExpansionContext context = new DefaultPropertyExpansionContext(modelItem); Properties properties = System.getProperties(); for (PropertyDescriptor descriptor : descriptors) { String name = descriptor.getName(); String key = scope + "." + name; if (PropertyUtils.isWriteable(target, name) && properties.containsKey(key)) { try { String value = context.expand(String.valueOf(properties.get(key))); BeanUtils.setProperty(target, name, value); SoapUI.log.info("Set property [" + name + "] to [" + value + "] in scope [" + scope + "]"); } catch (Throwable e) { SoapUI.logError(e);/* w w w . j ava2 s . c om*/ } } } }
From source file:Main.java
public static void makeTransient(Class<?> beanClass, String... pdNames) { try {/*w w w. j a v a 2 s .c o m*/ BeanInfo info = Introspector.getBeanInfo(beanClass); PropertyDescriptor[] descs = info.getPropertyDescriptors(); if (descs == null) { throw new RuntimeException("Cannot access property descriptor for class " + beanClass); } Map<String, PropertyDescriptor> mapping = new HashMap<String, PropertyDescriptor>(); for (PropertyDescriptor desc : descs) { mapping.put(desc.getName(), desc); } for (String pdName : pdNames) { PropertyDescriptor desc = mapping.get(pdName); if (desc == null) { throw new RuntimeException("Property " + pdName + " does not exist in " + beanClass); } desc.setValue("transient", Boolean.TRUE); } } catch (IntrospectionException ie) { throw new RuntimeException(ie); } }
From source file:org.zkybase.cmdb.util.ReflectionUtils.java
public static <T, U> U copySimpleProperties(T from, U to) { try {/*from w ww.j a v a 2 s.c om*/ PropertyDescriptor[] fromDescs = BeanUtils.getPropertyDescriptors(from.getClass()); for (PropertyDescriptor fromDesc : fromDescs) { String propName = fromDesc.getName(); PropertyDescriptor toDesc = BeanUtils.getPropertyDescriptor(to.getClass(), propName); if (toDesc != null) { Method readMethod = fromDesc.getReadMethod(); Object value = readMethod.invoke(from); boolean write = (value instanceof Long || value instanceof String); if (write) { Method writeMethod = toDesc.getWriteMethod(); writeMethod.invoke(to, value); } } } return to; } catch (InvocationTargetException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }
From source file:com.nesscomputing.config.ConfigMagicDynamicMBean.java
private static Map<String, Object> toMap(Object configBean) { PropertyDescriptor[] props = ReflectUtils.getBeanGetters(configBean.getClass()); Map<String, Object> result = Maps.newHashMap(); for (PropertyDescriptor prop : props) { if (CONFIG_MAGIC_CALLBACKS_NAME.equals(prop.getName())) { continue; }/*from w ww.j a v a 2 s . co m*/ try { result.put(prop.getName(), ObjectUtils.toString(prop.getReadMethod().invoke(configBean), null)); } catch (Exception e) { LOG.error(String.format("For class %s, unable to find config property %s", configBean.getClass(), prop), e); } } return result; }
From source file:Main.java
public static <T> T loadBean(Node node, Class<T> beanClass) throws IntrospectionException, InstantiationException, IllegalAccessException, IllegalArgumentException, DOMException, InvocationTargetException { T store = beanClass.newInstance();//from w w w . jav a 2 s. com Map<String, PropertyDescriptor> properties = new HashMap<String, PropertyDescriptor>(); BeanInfo beanInfo = Introspector.getBeanInfo(beanClass); for (PropertyDescriptor property : beanInfo.getPropertyDescriptors()) { properties.put(property.getName(), property); } NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node attribute = attributes.item(i); PropertyDescriptor property = properties.get(attribute.getNodeName()); if (property != null && property.getPropertyType().equals(String.class) && property.getWriteMethod() != null) { property.getWriteMethod().invoke(store, attribute.getNodeValue()); } } NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node child = childNodes.item(i); PropertyDescriptor property = properties.get(child.getNodeName()); if (property != null && property.getPropertyType().equals(String.class) && property.getWriteMethod() != null) { property.getWriteMethod().invoke(store, child.getTextContent()); } } return store; }