1 package org.objectweb.celtix.jaxb; 2 3 4 import java.lang.reflect.Field ; 5 import java.lang.reflect.InvocationTargetException ; 6 import java.lang.reflect.Method ; 7 import java.util.Collection ; 8 import java.util.List ; 9 10 import javax.xml.bind.annotation.XmlElement; 11 12 13 public final class WrapperHelper { 14 15 private WrapperHelper() { 16 } 18 19 20 public static void setWrappedPart(String partName, Object wrapperType, Object part) 21 throws IllegalAccessException , NoSuchMethodException , InvocationTargetException { 22 23 if (part instanceof List ) { 24 setWrappedListProperty(partName, wrapperType, part); 25 } else { 26 String fieldName = partName; 27 if (JAXBUtils.isJavaKeyword(partName)) { 28 fieldName = JAXBUtils.nameToIdentifier( 29 partName, 30 JAXBUtils.IdentifierType.VARIABLE); 31 } 32 33 XmlElement el = null; 34 for (Field field : wrapperType.getClass().getDeclaredFields()) { 35 if (field.getName().equals(fieldName)) { 36 el = field.getAnnotation(XmlElement.class); 38 assert el != null; 39 } 40 } 41 42 if (part == null) { 43 if (!el.nillable()) { 44 throw new IllegalArgumentException ("null value for field not permitted."); 45 } 46 return; 47 } 48 49 String modifier = 50 JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.SETTER); 51 52 boolean setInvoked = false; 53 for (Method method : wrapperType.getClass().getMethods()) { 54 if (method.getParameterTypes() != null 55 && method.getParameterTypes().length == 1 56 && modifier.equals(method.getName())) { 57 58 Class <?> clazz = method.getParameterTypes()[0]; 59 if (method.getParameterTypes()[0].isPrimitive()) { 60 clazz = el.type(); 61 } 62 63 if (clazz.isAssignableFrom(part.getClass())) { 64 method.invoke(wrapperType, part); 65 setInvoked = true; 66 break; 67 } 68 } 69 } 70 71 if (!setInvoked) { 72 throw new IllegalArgumentException ("Could not find a modifier method on Wrapper Type"); 73 } 74 } 75 } 76 77 private static void setWrappedListProperty(String partName, Object wrapperType, Object part) 78 throws IllegalAccessException , NoSuchMethodException , InvocationTargetException { 79 80 String accessorName = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.GETTER); 81 for (Method method : wrapperType.getClass().getMethods()) { 82 if (accessorName.equals(method.getName()) 83 && List .class.isAssignableFrom(method.getReturnType())) { 84 85 Object ret = method.invoke(wrapperType); 86 Method addAll = ret.getClass().getMethod("addAll", Collection .class); 87 addAll.invoke(ret, part); 88 break; 89 } 90 } 91 } 92 93 public static Object getWrappedPart(String partName, Object wrapperType, Class <?> partClazz) 94 throws IllegalAccessException , NoSuchMethodException , InvocationTargetException { 95 96 String accessor = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.GETTER); 97 98 if (partClazz.equals(boolean.class) || partClazz.equals(Boolean .class)) { 99 accessor = accessor.replaceFirst("get", "is"); 101 } 102 103 for (Method method : wrapperType.getClass().getMethods()) { 104 if (method.getParameterTypes().length == 0 105 && accessor.equals(method.getName())) { 106 107 Class <?> clazz = method.getReturnType(); 108 109 if (clazz.isPrimitive() && !partClazz.isPrimitive()) { 110 for (Field field : wrapperType.getClass().getDeclaredFields()) { 111 if (JAXBUtils.isJavaKeyword(partName)) { 112 partName = JAXBUtils.nameToIdentifier( 113 partName, 114 JAXBUtils.IdentifierType.VARIABLE); 115 } 116 if (field.getName().equals(partName)) { 117 clazz = field.getAnnotation(XmlElement.class).type(); 119 } 120 } 121 } 122 if (clazz.isAssignableFrom(partClazz)) { 124 return method.invoke(wrapperType); 125 } 126 } 127 } 128 return null; 129 } 130 } 131 | Popular Tags |