001: /*
002: *
003: * Copyright 2007 Giordano Maestro (giordano.maestro@assetdata.it)
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License. You may obtain a copy of
007: * the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017: package org.romaframework.aspect.reporting.jr;
018:
019: import java.lang.reflect.InvocationTargetException;
020: import java.lang.reflect.Method;
021: import java.util.Collection;
022: import java.util.Map;
023: import java.util.Set;
024:
025: import net.sf.jasperreports.engine.JRException;
026:
027: import org.romaframework.core.schema.SchemaField;
028:
029: public class ReflectionHelper {
030:
031: /**
032: * Return true if the clazz is a primitive type
033: *
034: * @param clazz
035: * @return
036: */
037: public static boolean isPrimitive(Class clazz) {
038: if (Boolean.class.isAssignableFrom(clazz)) {
039: return true;
040: }
041: if (Double.class.isAssignableFrom(clazz)) {
042: return true;
043: }
044: if (String.class.isAssignableFrom(clazz)) {
045: return true;
046: }
047: if (Float.class.isAssignableFrom(clazz)) {
048: return true;
049: }
050: if (Long.class.isAssignableFrom(clazz)) {
051: return true;
052: }
053: if (Short.class.isAssignableFrom(clazz)) {
054: return true;
055: }
056: if (Integer.class.isAssignableFrom(clazz)) {
057: return true;
058: }
059: return false;
060: }
061:
062: /**
063: * Return true if the clazz is a collection, a map or a set
064: *
065: * @param clazz
066: * @return
067: */
068: public static boolean isCollectionMapSet(Class clazz) {
069: if (Collection.class.isAssignableFrom(clazz)) {
070: return true;
071: }
072: if (Map.class.isAssignableFrom(clazz)) {
073: return true;
074: }
075: if (Set.class.isAssignableFrom(clazz)) {
076: return true;
077: }
078: return false;
079: }
080:
081: /**
082: * Return true if the clazz is a collection
083: *
084: * @param clazz
085: * @return
086: */
087: public static boolean isCollection(Class clazz) {
088: if (Collection.class.isAssignableFrom(clazz)) {
089: return true;
090: }
091: return false;
092: }
093:
094: /**
095: * Return true if the clazz is a collection
096: *
097: * @param clazz
098: * @return
099: */
100: public static boolean isMap(Class clazz) {
101: if (Map.class.isAssignableFrom(clazz)) {
102: return true;
103: }
104: return false;
105: }
106:
107: /**
108: * Return the value of the field
109: *
110: * @param schemaField the field to get
111: * @param obj the object where it must be searched
112: * @return
113: * @throws JRException
114: */
115: public static Object getObjectValue(SchemaField schemaField,
116: Object obj) throws JRException {
117: if (obj == null) {
118: return null;
119: }
120:
121: final Method method = schemaField.getGetterMethod();
122:
123: try {
124: if (JRDesignHelper.getIsVisibleField(schemaField)) {
125: return method.invoke(obj, new Object[0]);
126: } else {
127: return null;
128: }
129: } catch (final IllegalArgumentException e) {
130: throw new JRException(e);
131: } catch (final IllegalAccessException e) {
132: throw new JRException(e);
133: } catch (final InvocationTargetException e) {
134: throw new JRException(e);
135: }
136: }
137:
138: /**
139: * Return the string with the first letter to uppercase
140: *
141: * @param s
142: * @return
143: */
144: public static String firstToUppercase(String s) {
145: if (s == null) {
146: return null;
147: }
148:
149: if (s.length() > 1) {
150: return s.substring(0, 1).toUpperCase() + s.substring(1);
151: }
152:
153: return s.toUpperCase();
154: }
155:
156: }
|