1 28 29 package org.jibx.binding.def; 30 31 import org.apache.bcel.Constants; 32 import org.jibx.binding.classes.BoundClass; 33 import org.jibx.binding.classes.ClassFile; 34 import org.jibx.binding.classes.ContextMethodBuilder; 35 import org.jibx.binding.classes.ExceptionMethodBuilder; 36 import org.jibx.runtime.JiBXException; 37 38 43 44 public abstract class MappingBase extends LinkableBase implements IMapping 45 { 46 49 protected static final String IMARSHALLABLE_INTERFACE = 51 "org.jibx.runtime.IMarshallable"; 52 protected static final String MARSHALLABLE_METHODNAME = "marshal"; 53 protected static final String MARSHALLABLE_SIGNATURE = 54 "(Lorg/jibx/runtime/IMarshallingContext;)V"; 55 protected static final String GETINDEX_METHODNAME = "JiBX_getIndex"; 56 protected static final String GETINDEX_SIGNATURE = "()I"; 57 protected static final String CHECKEXTENDS_METHODNAME = "isExtension"; 58 protected static final String CHECKEXTENDS_SIGNATURE = "(I)Z"; 59 60 protected static final String IUNMARSHALLABLE_INTERFACE = 62 "org.jibx.runtime.IUnmarshallable"; 63 protected static final String UNMARSHALLABLE_METHODNAME = "unmarshal"; 64 protected static final String UNMARSHALLABLE_SIGNATURE = 65 "(Lorg/jibx/runtime/IUnmarshallingContext;)V"; 66 67 protected static final String UNMARSHALLER_INTERFACE = 69 "org.jibx.runtime.IUnmarshaller"; 70 protected static final String UNMARSHALLERUNMARSHAL_METHOD = 71 "org.jibx.runtime.IUnmarshaller.unmarshal"; 72 protected static final String UNMARSHALLERUNMARSHAL_SIGNATURE = 73 "(Ljava/lang/Object;Lorg/jibx/runtime/IUnmarshallingContext;)" + 74 "Ljava/lang/Object;"; 75 76 protected static final String MARSHALLER_INTERFACE = 78 "org.jibx.runtime.IMarshaller"; 79 protected static final String ABSTRACTMARSHALLER_INTERFACE = 80 "org.jibx.runtime.IAbstractMarshaller"; 81 protected static final String MARSHALLERMARSHAL_METHOD = 82 "org.jibx.runtime.IMarshaller.marshal"; 83 protected static final String MARSHALLERMARSHAL_SIGNATURE = 84 "(Ljava/lang/Object;Lorg/jibx/runtime/IMarshallingContext;)V"; 85 86 protected static final String GETMARSHALLER_METHOD = 88 "org.jibx.runtime.IMarshallingContext.getMarshaller"; 89 protected static final String GETMARSHALLER_SIGNATURE = 90 "(ILjava/lang/String;)Lorg/jibx/runtime/IMarshaller;"; 91 protected static final String GETUNMARSHALLER_METHOD = 92 "org.jibx.runtime.IUnmarshallingContext.getUnmarshaller"; 93 protected static final String GETUNMARSHALLER_SIGNATURE = 94 "(I)Lorg/jibx/runtime/IUnmarshaller;"; 95 96 99 100 private final int m_indexNumber; 101 102 110 111 public MappingBase(IContainer contain, String type) { 112 m_indexNumber = contain.getBindingRoot().getMappedClassIndex(type); 113 } 114 115 122 123 public MappingBase(IContainer contain, String type, IComponent wrap) { 124 this(contain, type); 125 setWrappedComponent(wrap); 126 } 127 128 134 135 public abstract BoundClass getBoundClass(); 136 137 144 145 protected void addIMarshallableMethod() throws JiBXException { 146 147 BoundClass clas = getBoundClass(); 149 ClassFile cf = clas.getMungedFile(); 150 ContextMethodBuilder mb = new ContextMethodBuilder 151 (MARSHALLABLE_METHODNAME, MARSHALLABLE_SIGNATURE, cf, 152 Constants.ACC_PUBLIC, 0, clas.getClassFile().getName(), 153 1, MARSHALLER_INTERFACE); 154 155 mb.loadContext(); 158 mb.appendLoadConstant(getIndex()); 159 mb.appendLoadConstant(cf.getName()); 160 mb.appendCallInterface(GETMARSHALLER_METHOD, GETMARSHALLER_SIGNATURE); 161 162 mb.loadObject();; 165 mb.loadContext(); 166 mb.appendCallInterface(MARSHALLERMARSHAL_METHOD, 167 MARSHALLERMARSHAL_SIGNATURE); 168 mb.appendReturn(); 169 170 clas.getUniqueNamed(mb); 172 173 ExceptionMethodBuilder xb = new ExceptionMethodBuilder 175 (GETINDEX_METHODNAME, GETINDEX_SIGNATURE, cf, Constants.ACC_PUBLIC); 176 177 xb.appendLoadConstant(getIndex()); 179 xb.appendReturn("int"); 180 181 clas.getUniqueNamed(xb); 183 clas.getClassFile().addInterface(IMARSHALLABLE_INTERFACE); 184 } 185 186 193 194 protected void addIUnmarshallableMethod() throws JiBXException { 195 196 BoundClass clas = getBoundClass(); 198 ClassFile cf = clas.getMungedFile(); 199 ContextMethodBuilder mb = new ContextMethodBuilder 200 (UNMARSHALLABLE_METHODNAME, UNMARSHALLABLE_SIGNATURE, cf, 201 Constants.ACC_PUBLIC, 0, clas.getClassFile().getName(), 202 1, UNMARSHALLER_INTERFACE); 203 204 mb.loadContext(); 206 mb.appendLoadConstant(getIndex()); 207 mb.appendCallInterface(GETUNMARSHALLER_METHOD, 208 GETUNMARSHALLER_SIGNATURE); 209 210 mb.loadObject();; 213 mb.loadContext(); 214 mb.appendCallInterface(UNMARSHALLERUNMARSHAL_METHOD, 215 UNMARSHALLERUNMARSHAL_SIGNATURE); 216 mb.appendReturn(); 217 218 clas.getUniqueNamed(mb); 220 clas.getClassFile().addInterface(IUNMARSHALLABLE_INTERFACE); 221 } 222 223 226 public int getIndex() { 227 return m_indexNumber; 228 } 229 } | Popular Tags |