📄 boundclass.java
字号:
} mb.appendReturn(); // track unique instance of this method method = m_mungedClass.getUniqueMethod(mb, true); m_storeMap.put(item, method); } // make sure method is accessible method.makeAccessible(from); return method.getItem(); } /** * Get unique method. Just delegates to the modified class handling, with * unique suffix appended to method name. * * @param builder method to be defined * @return defined method item * @throws JiBXException on configuration error */ public BindingMethod getUniqueMethod(MethodBuilder builder) throws JiBXException { return m_mungedClass.getUniqueMethod(builder, true); } /** * Get unique method. Just delegates to the modified class handling. The * supplied name is used without change. * * @param builder method to be defined * @return defined method item * @throws JiBXException on configuration error */ public BindingMethod getUniqueNamed(MethodBuilder builder) throws JiBXException { return m_mungedClass.getUniqueMethod(builder, false); } /** * Add binding factory to class. Makes sure that there's no surrogate class * for code generation, then delegates to the modified class handling. * * @param fact binding factory name */ public void addFactory(String fact) { if (isDirectAccess()) { m_mungedClass.addFactory(fact); } else { throw new IllegalStateException( "Internal error: not directly modifiable class"); } } /** * Generate factory list. Makes sure that there's no surrogate class for * code generation, then delegates to the modified class handling. * * @throws JiBXException on configuration error */ public void setFactoryList() throws JiBXException { if (isDirectAccess()) { m_mungedClass.setFactoryList(); } else { throw new IllegalStateException( "Internal error: not directly modifiable class"); } } /** * Create binding information for class. This creates the combination of * bound class and (if different) munged class and adds it to the internal * tables. * * @param key text identifier for this bound class and munged class * combination * @param bound class information for bound class * @param munge information for surrogate class receiving generated code, or * <code>null</code> if no separate class * @return binding information for class */ private static BoundClass createInstance(String key, ClassFile bound, MungedClass munge) { BoundClass inst = new BoundClass(bound, munge); s_nameMap.put(key, inst); return inst; } /** * Find or create binding information for class. If the combination of bound * class and munged class already exists it's returned directly, otherwise * it's created and returned. * * @param bound class information for bound class * @param munge information for surrogate class receiving generated code * @return binding information for class */ private static BoundClass findOrCreateInstance(ClassFile bound, MungedClass munge) { String key = bound.getName() + ':' + munge.getClassFile().getName(); BoundClass inst = (BoundClass)s_nameMap.get(key); if (inst == null) { inst = createInstance(key, bound, munge); } return inst; } /** * Get binding information for class. This finds the class in which code * generation for the target class takes place. Normally this class will be * the target class itself, but in cases where the target class is not * modifiable an alternate class will be used. This can take two forms. If * the context class is provided and it is a subclass of the target class, * code for the target class is instead added to the context class. If there * is no context class, or if the context class is not a subclass of the * target class, a unique catch-all class is used. * * @param cf bound class information * @param context context class for code generation, or <code>null</code> * if no context * @return binding information for class * @throws JiBXException on configuration error */ public static BoundClass getInstance(ClassFile cf, BoundClass context) throws JiBXException { // check if new instance needed for this class BoundClass inst = (BoundClass)s_nameMap.get(cf.getName()); if (inst == null) { // load the basic class information and check for modifiable if (!cf.isInterface() && cf.isModifiable()) { // return instance directly inst = createInstance(cf.getName(), cf, MungedClass .getInstance(cf)); } else { // see if the context class is a subclass if (context != null && context.getClassFile().isSuperclass(cf.getName())) { // find or create munge with subclass as surrogate inst = findOrCreateInstance(cf, context.m_mungedClass); } else { // use catch-all munge class as surrogate for all else if (s_genericMunge == null) { String mname; if (s_modifyPackage == null) { mname = GENERIC_MUNGE_CLASS; MungedClass.checkDirectory(s_modifyRoot, ""); } else { mname = s_modifyPackage + '.' + GENERIC_MUNGE_CLASS; MungedClass.checkDirectory(s_modifyRoot, s_modifyPackage); } ClassFile base = ClassCache .getClassFile("java.lang.Object"); int acc = Constants.ACC_PUBLIC | Constants.ACC_ABSTRACT; ClassFile gen = new ClassFile(mname, s_modifyRoot, base, acc, new String[0]); gen.addDefaultConstructor(); s_genericMunge = MungedClass.getInstance(gen); MungedClass.delayedAddUnique(gen); } inst = findOrCreateInstance(cf, s_genericMunge); } } } return inst; } /** * Get binding information for class. This version takes a fully-qualified * class name, calling the paired method if necessary to create a new * instance. * * @param name fully qualified name of bound class * @param context context class for code generation, or <code>null</code> * if no context * @return binding information for class * @throws JiBXException on configuration error */ public static BoundClass getInstance(String name, BoundClass context) throws JiBXException { // check if new instance needed for this class BoundClass inst = (BoundClass)s_nameMap.get(name); if (inst == null) { ClassFile cf = ClassCache.getClassFile(name); return getInstance(cf, context); } return inst; } /** * Discard cached information and reset in preparation for a new binding * run. */ public static void reset() { s_nameMap = new HashMap(); s_modifyPackage = null; s_modifyRoot = null; s_genericMunge = null; } /** * Discard cached information and reset in preparation for a new binding * run. */ public static void setModify(File root, String pkg) { s_modifyRoot = root; s_modifyPackage = pkg; if (s_modifyPackage.length() == 0) { s_modifyPackage = null; } } /** * Derive generated class name for bound class. This generates a JiBX class * name from the name of this class, using the supplied prefix and suffix * information. The derived class name is always in the same package as the * munged class for this class. * * @param prefix generated class name prefix * @param suffix generated class name suffix * @return derived class name */ public String deriveClassName(String prefix, String suffix) { String pack = m_mungedClass.getClassFile().getPackage(); if (pack.length() > 0) { pack += '.'; } String tname = m_boundClass.getName(); int split = tname.lastIndexOf('.'); if (split >= 0) { tname = tname.substring(split + 1); } return pack + prefix + tname + suffix; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -