📄 classfile.java
字号:
* @return <code>true</code> if an array class, <code>false</code> if not */ public boolean isArray() { return m_name.endsWith("[]"); } /** * Check if class is modifiable. * * @return <code>true</code> if class is modifiable, <code>false</code> if * not */ public boolean isModifiable() { return m_isWritable && !isInterface(); } /** * Get fully qualified class name. * * @return fully qualified name for class */ public String getName() { return m_name; } /** * Get signature for class as type. * * @return signature for class used as type */ public String getSignature() { return m_signature; } /** * Get class as type. * * @return class as type */ public Type getType() { return m_type; } /** * Get package name. * * @return package name for class */ public String getPackage() { int split = m_name.lastIndexOf('.'); if (split >= 0) { return m_name.substring(0, split); } else { return ""; } } /** * Get root directory for load path. * * @return root directory in path used for loading file */ public File getRoot() { return m_root; } /** * Get actual file for class. * * @return file used for class */ public File getFile() { return m_file; } /** * Get raw current class information. * * @return raw current class information */ public JavaClass getRawClass() { if (m_curClass == null) { throw new IllegalStateException ("No loadable class information for " + m_name); } else { return m_curClass; } } /** * Get superclass name. * * @return fully qualified name of superclass */ public String getSuperName() { if (m_curClass == null) { return null; } else { return m_curClass.getSuperclassName(); } } /** * Set superclass information. * * @param sclas superclass information */ public void setSuperFile(ClassFile sclas) { m_superClass = sclas; m_isSamePackage = getPackage().equals(sclas.getPackage()); } /** * Get superclass information. * * @return super class information as loaded (<code>null</code> if no * superclass - java.lang.Object, interface, or primitive) */ public ClassFile getSuperFile() { return m_superClass; } /** * Get names of all interfaces implemented by class. * * @return names of all interfaces implemented directly by class */ public String[] getInterfaces() { return m_interfaceNames; } /** * Add interface to class. The interface is added to the class if not * already defined. * * @param intf fully qualified interface name * @return <code>true</code> if added, <code>false</code> if already present * @throws JiBXException on configuration error */ public boolean addInterface(String intf) throws JiBXException { ClassGen gen = getClassGen(); String[] intfs = gen.getInterfaceNames(); for (int i = 0; i < intfs.length; i++) { if (intf.equals(intfs[i])) { return false; } } gen.addInterface(intf); m_isModified = true; m_instanceOfs = null; return true; } /** * Accumulate interface signatures recursively. * * @param intfs names of interfaces implemented * @param map map for interfaces already accumulated * @param accs accumulated interface names * @throws JiBXException if configuration error */ protected void accumulateInterfaces(String[] intfs, HashMap map, ArrayList accs) throws JiBXException { for (int i = 0; i < intfs.length; i++) { String name = intfs[i]; if (map.get(name) == null) { ClassFile cf = ClassCache.getClassFile(name); String sig = cf.getSignature(); map.put(name, sig); accs.add(sig); String[] inherits = cf.m_curClass.getInterfaceNames(); accumulateInterfaces(inherits, map, accs); } } } /** * Get signatures for all types of which instances of this type are * instances. * * @return all signatures suppored by instances * @throws JiBXException if configuration error */ public String[] getInstanceSigs() throws JiBXException { if (m_instanceOfs == null) { // check for an array class String name = getName(); if (name.endsWith("[]")) { // accumulate prefix by stripping suffixes String prefix = ""; do { name = name.substring(0, name.length()-2); prefix = prefix + '['; } while (name.endsWith("[]")); // check for a primitive base type on array String[] bsigs; if (ClassItem.isPrimitive(name)) { bsigs = new String[1]; bsigs[0] = ClassItem.getPrimitiveSignature(name); } else { ClassFile bcf = ClassCache.getClassFile(name); bsigs = bcf.getInstanceSigs(); } // derive array signatures from signatures for base type String[] asigs = new String[bsigs.length+1]; for (int i = 0; i < bsigs.length; i++) { asigs[i] = prefix + bsigs[i]; } asigs[bsigs.length] = "Ljava/lang/Object;"; m_instanceOfs = asigs; } else { // walk all classes and interfaces to find signatures HashMap map = new HashMap(); ArrayList iofs = new ArrayList(); ClassFile cur = this; while (cur != null) { String sig = cur.getSignature(); map.put(name, sig); iofs.add(sig); accumulateInterfaces(cur.getInterfaces(), map, iofs); cur = cur.getSuperFile(); } String[] sigs = new String[iofs.size()]; m_instanceOfs = (String[])iofs.toArray(sigs); } } return m_instanceOfs; } /** * Check if class implements an interface. * * @param sig signature of interface to be checked * @return <code>true</code> if interface is implemented by class, * <code>false</code> if not * @throws JiBXException if configuration error */ public boolean isImplements(String sig) throws JiBXException { String[] sigs = getInstanceSigs(); for (int i = 0; i < sigs.length; i++) { if (sig.equals(sigs[i])) { return true; } } return false; } /** * Check if another class is a superclass of this one. * * @param name of superclass to be checked * @return <code>true</code> if named class is a superclass of this one, * <code>false</code> if not */ public boolean isSuperclass(String name) { ClassFile cur = this; while (cur != null) { if (cur.getName().equals(name)) { return true; } else { cur = cur.getSuperFile(); } } return false; } /** * Get array of fields defined by class. * * @return array of fields defined by class */ public ClassItem[] getFieldItems() { if (m_curClass == null) { return EMPTY_CLASS_ITEMS; } else { Field[] fields = m_curClass.getFields(); ClassItem[] items = new ClassItem[fields.length]; for (int i = 0; i < fields.length; i++) { Field field = fields[i]; items[i] = new ClassItem(field.getName(), this, field); } return items; } } /** * Get internal information for field. This can only be used with * existing classes, and only checks for fields that are actually members * of the class (not superclasses). * * @param name field name * @return field information, or <code>null</code> if field not found */ protected Field getDefinedField(String name) { // check for match to field name defined in class Field[] fields = m_curClass.getFields(); for (int i = 0; i < fields.length; i++) { if (fields[i].getName().equals(name)) { return fields[i]; } } return null; } /** * Get internal information for field. This can only be used with existing * classes. If the field is not found directly, superclasses are checked for * inherited fields matching the supplied name. * * @param name field name * @return field information, or <code>null</code> if field not found */ protected Field getAccessibleField(String name) { // always return not found for unloadable class if (m_curClass == null) { return null; } else { // check for match to field name defined in class Field field = getDefinedField(name); if (field == null) { // try match to field inherited from superclass if (m_superClass != null) { field = m_superClass.getAccessibleField(name); if (field != null && (!m_isSamePackage || field.isPrivate()) && !field.isPublic() && !field.isProtected()) { field = null; } } } return field; } } /** * Get information for field. This can only be used with existing classes, * and only checks for fields that are actually members of the class (not * superclasses). * * @param name field name * @return field information, or <code>null</code> if field not found */ public ClassItem getDirectField(String name) { Field field = getAccessibleField(name); if (field == null) { return null; } else { ClassItem item = (ClassItem)m_itemMap.get(field); if (item == null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -