1 16 package org.apache.cocoon.portal.aspect.impl; 17 18 import java.lang.reflect.Constructor ; 19 20 import org.apache.cocoon.portal.aspect.AspectDescription; 21 import org.apache.cocoon.util.ClassUtils; 22 23 24 25 32 public class AspectUtil { 33 34 37 public static Object createNewInstance(AspectDescription desc) { 38 try { 39 Class clazz = ClassUtils.loadClass(desc.getClassName()); 40 if ( clazz.getName().startsWith("java.lang.")) { 41 Constructor constructor = clazz.getConstructor(new Class [] {String .class}); 42 String value = (desc.getDefaultValue() == null ? "0" : desc.getDefaultValue()); 43 return constructor.newInstance(new String [] {value}); 44 } 45 if ( desc.getDefaultValue() != null ) { 46 Constructor constructor = clazz.getConstructor(new Class [] {String .class}); 47 return constructor.newInstance(new String [] {desc.getDefaultValue()}); 48 } 49 return clazz.newInstance(); 50 } catch (Exception ignore) { 51 return null; 52 } 53 } 54 55 public static Object convert(AspectDescription desc, Object value) { 56 try { 57 Class clazz = ClassUtils.loadClass(desc.getClassName()); 58 if ( clazz.getName().startsWith("java.lang.")) { 59 if ( !clazz.equals(value.getClass())) { 60 Constructor constructor = clazz.getConstructor(new Class [] {String .class}); 61 return constructor.newInstance(new String [] {value.toString()}); 62 } 63 return value; 64 } 65 if ( !value.getClass().equals(clazz) ) { 66 throw new RuntimeException ("Class of aspect doesn't match description."); 68 } 69 return value; 70 } catch (Exception ignore) { 71 return value; 73 } 74 } 75 } 76 | Popular Tags |