List of usage examples for org.apache.commons.beanutils PropertyUtils getPropertyDescriptors
public static PropertyDescriptor[] getPropertyDescriptors(Object bean)
Retrieve the property descriptors for the specified bean, introspecting and caching them the first time a particular bean class is encountered.
For more details see PropertyUtilsBean
.
From source file:org.ajax4jsf.application.DebugOutputMaker.java
/** * Out component with properties and it's facets and childrens * @param context TODO// w w w .j a v a 2 s . c o m * @param out * @param viewRoot */ private void writeComponent(FacesContext context, PrintWriter out, UIComponent component, String facetName) { String clientId = "_tree:" + component.getClientId(context); out.println("<dt onclick=\"toggle('" + clientId + "')\" class='tree'>"); writeToggleMark(out, clientId); // out component name if (null != facetName) { out.print("Facet:'" + facetName + "' "); } out.println("<code>" + component.getClass().getName() + "</code> Id:[" + component.getId() + "]"); out.println("</dt>"); out.println("<dd id='" + clientId + "' style='display:none;' class='tree' ><ul class='tree'>"); // out bean properties try { PropertyDescriptor propertyDescriptors[] = PropertyUtils.getPropertyDescriptors(component); for (int i = 0; i < propertyDescriptors.length; i++) { if (PropertyUtils.isReadable(component, propertyDescriptors[i].getName())) { String name = propertyDescriptors[i].getName(); ValueBinding vb = component.getValueBinding(name); if (vb != null) { writeAttribute(out, name, vb.getExpressionString()); } else { if (!IGNORE_ATTRIBUTES.contains(name)) { try { String value = BeanUtils.getProperty(component, name); writeAttribute(out, name, value); } catch (Exception e) { writeAttribute(out, name, null); } } } } } } catch (Exception e) { // Do nothing - we in error page. } // out bindings // out attributes map for (Iterator it = component.getAttributes().entrySet().iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); writeAttribute(out, (String) entry.getKey(), entry.getValue()); } // out listeners out.println("</ul></dd>"); if (component.getFacetsAndChildren().hasNext()) { out.println("<dd class='tree_childs'><dl class='tree_childs'>"); // out childs of this component // facets for (Iterator facetEntry = component.getFacets().entrySet().iterator(); facetEntry.hasNext();) { Map.Entry entry = (Map.Entry) facetEntry.next(); writeComponent(context, out, (UIComponent) entry.getValue(), (String) entry.getKey()); } // childs components for (Iterator childIter = component.getChildren().iterator(); childIter.hasNext();) { UIComponent child = (UIComponent) childIter.next(); writeComponent(context, out, child, null); } out.println("</dl></dd>"); } }
From source file:org.ajax4jsf.application.DebugOutputMaker.java
private void writeVariables(PrintWriter out, Map vars, String caption) { out.print("<table><caption>"); out.print(caption);/*from ww w . j av a 2s .c o m*/ out.println( "</caption><thead><tr><th style=\"width: 10%; \">Name</th><th style=\"width: 90%; \">Value</th></tr></thead><tbody>"); boolean written = false; if (!vars.isEmpty()) { SortedMap map = new TreeMap(vars); Map.Entry entry = null; String key = null; for (Iterator itr = map.entrySet().iterator(); itr.hasNext();) { entry = (Map.Entry) itr.next(); key = entry.getKey().toString(); if (key.indexOf('.') == -1) { out.println("<tr><td>"); out.println(key.replaceAll("<", LT).replaceAll(">", GT)); out.println("</td><td><span class='value'>"); Object value = entry.getValue(); out.println(value.toString().replaceAll("<", LT).replaceAll(">", GT)); out.println("</span>"); try { PropertyDescriptor propertyDescriptors[] = PropertyUtils.getPropertyDescriptors(value); if (propertyDescriptors.length > 0) { out.print("<div class='properties'><ul class=\'properties\'>"); for (int i = 0; i < propertyDescriptors.length; i++) { String beanPropertyName = propertyDescriptors[i].getName(); if (PropertyUtils.isReadable(value, beanPropertyName)) { out.print("<li class=\'properties\'>"); out.print(beanPropertyName + " = " + BeanUtils.getProperty(value, beanPropertyName)); out.print("</li>"); } } out.print("</ul></div>"); } } catch (Exception e) { // TODO: log exception } out.println("</td></tr>"); written = true; } } } if (!written) { out.println("<tr><td colspan=\"2\"><em>None</em></td></tr>"); } out.println("</tbody></table>"); }
From source file:org.ajax4jsf.builder.config.BuilderConfig.java
private Map<String, PropertyDescriptor> getPropertyDescriptors(Class<?> clazz) { if (clazz.equals(Object.class)) { return Collections.emptyMap(); }// www.j a va 2s . com Map<String, PropertyDescriptor> m = new TreeMap<String, PropertyDescriptor>(); Class<?> superclass = clazz.getSuperclass(); if (superclass != null) { m.putAll(getPropertyDescriptors(superclass)); } Class<?>[] interfaces = clazz.getInterfaces(); if (interfaces != null) { for (Class<?> intrfc : interfaces) { m.putAll(getPropertyDescriptors(intrfc)); } } PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(clazz); for (PropertyDescriptor propertyDescriptor : descriptors) { m.put(propertyDescriptor.getName(), propertyDescriptor); } return m; }
From source file:org.ajax4jsf.builder.config.ComponentBaseBean.java
/** * Subclasses should extend this method to provide specifc checks * //from www.ja va2s .com * Check existing and default properties * For properties filled from configuration, attempt to set additional parameters. * If base class have any bean properties, append it to configured * @throws ConfigurationException */ public void checkProperties() throws ParsingException { try { getLog().debug("Parse properties for Component " + getName() + " with superclass " + getSuperclass()); if (getSuperclass() != null) { Class<?> superClass = getLoader().loadClass(getSuperclass()); new ClassWalkingLogic(superClass).walk(new ClassVisitor() { public void visit(Class<?> clazz) { checkPropertiesForClass(clazz); } }); } } catch (ClassNotFoundException e) { getLog().error("superclass not found for component " + getName(), e); } if (null != getTag()) { try { Class superClass = getLoader().loadClass(getTag().getSuperclass()); PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(superClass); // for all properties, add it to component. If property have not abstract getter/setter , // add it with exist = true . If property in list of hidden names, set hidden = true. for (int i = 0; i < properties.length; i++) { PropertyDescriptor descriptor = properties[i]; Method writeMethod = descriptor.getWriteMethod(); if (containProperty(descriptor.getName())) { if (null != writeMethod && !Modifier.isAbstract(writeMethod.getModifiers()) && Modifier.isPublic(writeMethod.getModifiers())) { ((PropertyBean) this.properties.get(descriptor.getName())).setExistintag(true); } } else if (null != writeMethod && Modifier.isPublic(writeMethod.getModifiers())) { if (Arrays.asList(enabledTagProperties).contains(descriptor.getName())) { Class type = descriptor.getPropertyType(); getLog().debug("Register tag property " + descriptor.getName() + " with type name " + type.getCanonicalName()); PropertyBean property = new PropertyBean(); property.setName(descriptor.getName()); property.setDescription(descriptor.getShortDescription()); property.setDisplayname(descriptor.getDisplayName()); property.setClassname(descriptor.getPropertyType().getCanonicalName()); property.setExist(true); if (!Modifier.isAbstract(writeMethod.getModifiers())) { property.setExistintag(true); } addProperty(property); } } } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block getLog().error("superclass not found for tag " + getTag().getName(), e); } } }
From source file:org.ajax4jsf.builder.config.ComponentBaseBean.java
/** * @param superClass// ww w.ja v a2 s . co m */ private void checkPropertiesForClass(Class<?> superClass) { getLog().debug("Check properties for class " + superClass.getName()); // get all property descriptors PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(superClass); // for all properties, add it to component. If property have not abstract getter/setter , // add it with exist = true . If property in list of hidden names, set hidden = true. PropertyBean property; for (int i = 0; i < properties.length; i++) { PropertyDescriptor descriptor = properties[i]; if (!containProperty(descriptor.getName())) { if (isIgnorableProperty(superClass, descriptor.getName())) { continue; } Class<?> type = descriptor.getPropertyType(); getLog().debug("Register property " + descriptor.getName() + " with type name " + type.getCanonicalName()); property = new PropertyBean(); property.setName(descriptor.getName()); property.setDescription(descriptor.getShortDescription()); property.setDisplayname(descriptor.getDisplayName()); property.setClassname(descriptor.getPropertyType().getCanonicalName()); property.setExist(true); addProperty(property); } else { // Load and check property. getLog().debug("Check property " + descriptor.getName()); property = (PropertyBean) this.properties.get(descriptor.getName()); if (property.getClassname() == null) { property.setClassname(descriptor.getPropertyType().getCanonicalName()); } else { if (!property.getClassname().equals(descriptor.getPropertyType().getCanonicalName())) { String message = "Class " + property.getClassname() + " for property " + property.getName() + " not equals with real bean property type: " + descriptor.getPropertyType().getCanonicalName(); getLog().error(message); //throw new IllegalArgumentException(message); } } if (property.getDescription() == null) { property.setDescription(descriptor.getShortDescription()); } if (property.getDisplayname() == null) { property.setDisplayname(descriptor.getDisplayName()); } property.setExist(true); } Method getter = descriptor.getReadMethod(); Method setter = descriptor.getWriteMethod(); // Abstract methods if (null != setter && null != getter) { if ((Modifier.isAbstract(getter.getModifiers()) && Modifier.isAbstract(setter.getModifiers())) || superClass.isInterface()) { getLog().debug("Detect as abstract property " + descriptor.getName()); property.setExist(false); } } if (null == setter || (!Modifier.isPublic(setter.getModifiers()))) { getLog().debug("Detect as hidden property " + descriptor.getName()); property.setHidden(true); } if (isAttachedProperty(property)) { property.setAttachedstate(true); } if (property.isInstanceof("javax.faces.el.MethodBinding") || property.isInstanceof("javax.faces.el.ValueBinding")) { property.setElonly(true); } } }
From source file:org.ajax4jsf.builder.mojo.CompileMojo.java
/** * Convert any Java Object to JavaScript representation ( as possible ). * /*from w ww .j ava 2 s. c om*/ * @param obj * @return * @throws MojoExecutionException */ public String toLog(Object obj) throws MojoExecutionException { if (null == obj) { return "null"; } else if (obj.getClass().isArray()) { StringBuffer ret = new StringBuffer("["); boolean first = true; for (int i = 0; i < Array.getLength(obj); i++) { Object element = Array.get(obj, i); if (!first) { ret.append(','); } ret.append(toLog(element)); first = false; } return ret.append("]\n").toString(); } else if (obj instanceof Collection) { // Collections put as JavaScript array. Collection collection = (Collection) obj; StringBuffer ret = new StringBuffer("["); boolean first = true; for (Iterator iter = collection.iterator(); iter.hasNext();) { Object element = iter.next(); if (!first) { ret.append(','); } ret.append(toLog(element)); first = false; } return ret.append("]\n").toString(); } else if (obj instanceof Map) { // Maps put as JavaScript hash. Map map = (Map) obj; StringBuffer ret = new StringBuffer("{"); boolean first = true; for (Iterator iter = map.keySet().iterator(); iter.hasNext();) { Object key = (Object) iter.next(); if (!first) { ret.append(','); } ret.append(key); ret.append(":"); ret.append(toLog(map.get(key))); first = false; } return ret.append("}\n").toString(); } else if (obj instanceof Number || obj instanceof Boolean) { // numbers and boolean put as-is, without conversion return obj.toString(); } else if (obj instanceof String) { // all other put as encoded strings. StringBuffer ret = new StringBuffer(); addEncodedString(ret, obj); return ret.append("\n").toString(); } // All other objects threaded as Java Beans. try { StringBuffer ret = new StringBuffer("{"); PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(obj); boolean first = true; for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor propertyDescriptor = propertyDescriptors[i]; String key = propertyDescriptor.getName(); if ("class".equals(key) || propertyDescriptor.getReadMethod() == null) { continue; } if (!first) { ret.append(",\n\t"); } addEncodedString(ret, key); ret.append(":"); try { ret.append(String.valueOf(PropertyUtils.getProperty(obj, key))); } catch (InvocationTargetException e) { ret.append("Not ACCESIBLE"); } // ret.append(toLog(PropertyUtils.getProperty(obj, key))); first = false; } return ret.append("}\n").toString(); } catch (Exception e) { throw new MojoExecutionException("Error in conversion Java Object to String", e); } }
From source file:org.ajax4jsf.javascript.ScriptUtils.java
/** * Convert any Java Object to JavaScript representation ( as possible ). * @param obj/*from w w w. j a v a 2 s. c o m*/ * @return */ public static String toScript(Object obj) { if (null == obj) { return "null"; } else if (obj instanceof ScriptString) { return ((ScriptString) obj).toScript(); } else if (obj.getClass().isArray()) { StringBuilder ret = new StringBuilder("["); boolean first = true; for (int i = 0; i < Array.getLength(obj); i++) { Object element = Array.get(obj, i); if (!first) { ret.append(','); } ret.append(toScript(element)); first = false; } return ret.append("] ").toString(); } else if (obj instanceof Collection) { // Collections put as JavaScript array. @SuppressWarnings("unchecked") Collection<Object> collection = (Collection<Object>) obj; StringBuilder ret = new StringBuilder("["); boolean first = true; for (Iterator<Object> iter = collection.iterator(); iter.hasNext();) { Object element = iter.next(); if (!first) { ret.append(','); } ret.append(toScript(element)); first = false; } return ret.append("] ").toString(); } else if (obj instanceof Map) { // Maps put as JavaScript hash. @SuppressWarnings("unchecked") Map<Object, Object> map = (Map<Object, Object>) obj; StringBuilder ret = new StringBuilder("{"); boolean first = true; for (Map.Entry<Object, Object> entry : map.entrySet()) { if (!first) { ret.append(','); } addEncodedString(ret, entry.getKey()); ret.append(":"); ret.append(toScript(entry.getValue())); first = false; } return ret.append("} ").toString(); } else if (obj instanceof Number || obj instanceof Boolean) { // numbers and boolean put as-is, without conversion return obj.toString(); } else if (obj instanceof String) { // all other put as encoded strings. StringBuilder ret = new StringBuilder(); addEncodedString(ret, obj); return ret.toString(); } else if (obj instanceof Enum) { // all other put as encoded strings. StringBuilder ret = new StringBuilder(); addEncodedString(ret, obj); return ret.toString(); } else if (obj.getClass().getName().startsWith("java.sql.")) { StringBuilder ret = new StringBuilder("{"); boolean first = true; for (PropertyDescriptor propertyDescriptor : PropertyUtils.getPropertyDescriptors(obj)) { String key = propertyDescriptor.getName(); if ("class".equals(key)) { continue; } Object value = null; try { value = PropertyUtils.getProperty(obj, key); } catch (Exception e) { continue; } if (!first) { ret.append(','); } addEncodedString(ret, key); ret.append(":"); ret.append(toScript(value)); first = false; } return ret.append("} ").toString(); } // All other objects threaded as Java Beans. try { StringBuilder ret = new StringBuilder("{"); PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(obj); boolean first = true; for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { String key = propertyDescriptor.getName(); if ("class".equals(key)) { continue; } if (!first) { ret.append(','); } addEncodedString(ret, key); ret.append(":"); ret.append(toScript(PropertyUtils.getProperty(obj, key))); first = false; } return ret.append("} ").toString(); } catch (Exception e) { throw new FacesException("Error in conversion Java Object to JavaScript", e); } }
From source file:org.ajax4jsf.templatecompiler.builder.AbstractCompilationContext.java
private Map<String, PropertyDescriptor> resolveProperties(Class<?> clazz) { final Map<String, PropertyDescriptor> descriptors = new HashMap<String, PropertyDescriptor>(); new ClassWalkingLogic(clazz).walk(new ClassVisitor() { public void visit(Class<?> clazz) { PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(clazz); for (PropertyDescriptor descriptor : pds) { descriptors.put(descriptor.getName(), descriptor); }/*from ww w . j ava 2 s . c om*/ } }); return descriptors; }
From source file:org.ajax4jsf.templatecompiler.elements.vcp.FCallTemplateElement.java
/** * /* w w w .j ava 2 s .com*/ * @param clazz * @param propertyName * @return */ private PropertyDescriptor getPropertyDescriptor(Class clazz, String propertyName) { PropertyDescriptor returnValue = null; PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(clazz); for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor descriptor = propertyDescriptors[i]; if (descriptor.getName().equals(propertyName)) { returnValue = descriptor; break; } } return returnValue; }
From source file:org.apache.bval.util.PropertyAccess.java
private static Method getPropertyReadMethod(String propertyName, Class<?> beanClass) { for (PropertyDescriptor each : PropertyUtils.getPropertyDescriptors(beanClass)) { if (each.getName().equals(propertyName)) { return each.getReadMethod(); }// w w w .ja v a 2 s . c om } return null; }