📄 classtype.java
字号:
* @param result array to place selected methods in * @param offset start of where in result to place result * @return number of methods placed in result array * @deprecated */ public int getMethods (Filter filter, int searchSupers, Method[] result, int offset) { int count = 0; for (ClassType ctype = this; ctype != null; ctype = ctype.getSuperclass()) { for (Method meth = ctype.getDeclaredMethods(); meth != null; meth = meth.getNext()) if (filter.select(meth)) { if (result != null) result[offset + count] = meth; count++; } if (searchSupers == 0) break; if (searchSupers > 1) { ClassType[] interfaces = ctype.getInterfaces(); if (interfaces != null) { for (int i = 0; i < interfaces.length; i++) count += interfaces[i].getMethods(filter, searchSupers, result, offset+count); } } } return count; } /** Helper to get methods satisfying a filtering predicate. * @param filter to select methods to return * @param searchSupers 0 if only current class should be searched, * 1 if superclasses should also be searched, * 2 if super-interfaces should also be search * @param result Vector to add selected methods in * @param context If non-null, skip if class not visible in named package. * @return number of methods placed in result array */ public int getMethods (Filter filter, int searchSupers, Vector result, String context) { int count = 0; for (ClassType ctype = this; ctype != null; ctype = ctype.getSuperclass()) { if (context == null || (ctype.getModifiers() & Access.PUBLIC) != 0 || context.equals(ctype.getPackageName())) { for (Method meth = ctype.getDeclaredMethods(); meth != null; meth = meth.getNext()) if (filter.select(meth)) { if (result != null) result.addElement(meth); count++; } } if (searchSupers == 0) break; if (searchSupers > 1) { ClassType[] interfaces = ctype.getInterfaces(); if (interfaces != null) { for (int i = 0; i < interfaces.length; i++) count += interfaces[i].getMethods(filter, searchSupers, result, context); } } } return count; } public Method getDeclaredMethod(String name, Type[] arg_types) { for (Method method = getDeclaredMethods(); method != null; method = method.next) { if (! name.equals(method.getName())) continue; Type[] method_args = method.getParameterTypes(); if (arg_types == null || arg_types == method_args) return method; int i = arg_types.length; if (i != method_args.length) continue; while (-- i >= 0) { Type meth_type = method_args[i]; Type need_type = arg_types[i]; if (meth_type == need_type) continue; String meth_sig = meth_type.getSignature(); String need_sig = need_type.getSignature(); if (! meth_sig.equals(need_sig)) break; } if (i < 0) return method; } return null; } /** Get a method with matching name and number of arguments. */ public Method getDeclaredMethod(String name, int argCount) { Method result = null; for (Method method = getDeclaredMethods(); method != null; method = method.next) { if (name.equals(method.getName()) && argCount == method.getParameterTypes().length) { if (result != null) throw new Error("ambiguous call to getDeclaredMethod(\"" + name + "\", " + argCount+ ")\n - " + result + "\n - " + method); result = method; } } return result; } public Method getMethod(String name, Type[] arg_types) { ClassType cl = this; for (;;) { Method method = cl.getDeclaredMethod(name, arg_types); if (method != null) return method; cl = cl.getSuperclass(); if (cl == null) return null; } } /** Use reflection to add all the declared methods of this class. * Does not add constructors nor private or package-private methods. * Does not check for duplicate (already-known) methods. * @param clas should be the same as getReflectClass(). */ public void addMethods(Class clas) { // Set this flag BEFORE the actual addition. // This prevents this method to be called indirectly for the same class // while it is executed, which would result in methods being listed // twice in this class. flags |= ADD_METHODS_DONE; java.lang.reflect.Method[] methods; try { methods = clas.getDeclaredMethods(); } catch (SecurityException ex) { methods = clas.getMethods(); } int count = methods.length; for (int i = 0; i < count; i++) { java.lang.reflect.Method method = methods[i]; if (! method.getDeclaringClass().equals(clas)) continue; int modifiers = method.getModifiers(); if ((modifiers & (Access.PUBLIC|Access.PROTECTED)) == 0) continue; Class[] paramTypes = method.getParameterTypes(); int j = paramTypes.length; Type[] args = new Type[j]; while (--j >= 0) args[j] = Type.make(paramTypes[j]); Method meth = new Method (this, modifiers); meth.setName(method.getName()); meth.arg_types = args; meth.return_type = Type.make(method.getReturnType()); } java.lang.reflect.Constructor[] cmethods; try { cmethods = clas.getDeclaredConstructors(); } catch (SecurityException ex) { cmethods = clas.getConstructors(); } count = cmethods.length; for (int i = 0; i < count; i++) { java.lang.reflect.Constructor method = cmethods[i]; if (! method.getDeclaringClass().equals(clas)) continue; int modifiers = method.getModifiers(); if ((modifiers & (Access.PUBLIC|Access.PROTECTED)) == 0) continue; Class[] paramTypes = method.getParameterTypes(); int j = paramTypes.length; Type[] args = new Type[j]; while (--j >= 0) args[j] = Type.make(paramTypes[j]); Method meth = new Method (this, modifiers); meth.setName("<init>"); meth.arg_types = args; meth.return_type = Type.void_type; } } public Method[] getMatchingMethods(String name, Type[] paramTypes, int flags) { int i = getMethodCount(); int nMatches = 0; java.util.Vector matches = new java.util.Vector(10); for (Method method = methods; method != null; method = method.getNext()) { if (! name.equals(method.getName())) continue; if ((flags & Access.STATIC) != (method.access_flags & Access.STATIC)) continue; if ((flags & Access.PUBLIC) > (method.access_flags & Access.PUBLIC)) continue; Type[] mtypes = method.arg_types; if (mtypes.length != paramTypes.length) continue; nMatches++; matches.addElement(method); } Method[] result = new Method[nMatches]; matches.copyInto(result); return result; } /** Do various fixups after generating code but before we can write it out. * This includes assigning constant pool indexes where needed, * finalizing labels, etc. */ public void doFixups () { if (constants == null) constants = new ConstantPool(); if (thisClassIndex == 0) thisClassIndex = constants.addClass(this).index; if (superClass == this) setSuper((ClassType) null); if (superClassIndex < 0) superClassIndex = superClass == null ? 0 : constants.addClass(superClass).index; if (interfaces != null && interfaceIndexes == null) { int n = interfaces.length; interfaceIndexes = new int [n]; for (int i = 0; i < n; i++) interfaceIndexes[i] = constants.addClass(interfaces[i]).index; } for (Field field = fields; field != null; field = field.next) { field.assign_constants (this); } for (Method method = methods; method != null; method = method.next) method.assignConstants(); Attribute.assignConstants(this, this); } public void writeToStream (OutputStream stream) throws java.io.IOException { java.io.DataOutputStream dstr = new java.io.DataOutputStream (stream); int i; doFixups (); dstr.writeInt (0xcafebabe); // magic dstr.writeShort (minor_version); dstr.writeShort (major_version); // Write out the constant pool. if (constants == null) dstr.writeShort (1); else constants.write(dstr); dstr.writeShort (access_flags); dstr.writeShort (thisClassIndex); dstr.writeShort (superClassIndex); if (interfaceIndexes == null) dstr.writeShort (0); // interfaces_count else { int interfaces_count = interfaceIndexes.length; dstr.writeShort (interfaces_count); for (i = 0; i < interfaces_count; i++) dstr.writeShort (interfaceIndexes[i]); } dstr.writeShort (fields_count); for (Field field = fields; field != null; field = field.next) field.write (dstr, this); dstr.writeShort (methods_count); for (Method method = methods; method != null; method = method.next) method.write (dstr, this); Attribute.writeAll (this, dstr); flags |= ADD_FIELDS_DONE | ADD_METHODS_DONE; } public void writeToFile (String filename) throws java.io.IOException { OutputStream stream = new BufferedOutputStream(new FileOutputStream (filename)); writeToStream (stream); stream.close (); } public void writeToFile () throws java.io.IOException { writeToFile (this_name.replace ('.', File.separatorChar) + ".class"); } public byte[] writeToArray () { ByteArrayOutputStream stream = new ByteArrayOutputStream (500); try { writeToStream(stream); } catch (java.io.IOException ex) { throw new InternalError(ex.toString()); } return stream.toByteArray (); } /** * Convert a String to a Utf8 byte array. * @param str the input String. * @return the input encoded as a utf8 byte array. */ public static byte[] to_utf8 (String str) { if (str == null) return null; int str_len = str.length (); int utf_len = 0; for (int i = 0; i < str_len; i++) { int c = str.charAt(i); if ((c > 0) && (c <= 0x7F)) utf_len++; else if (c <= 0x7FF) utf_len += 2; else utf_len += 3; } byte[] buffer = new byte[utf_len]; int j = 0; for (int i = 0; i < str_len; i++) { int c = str.charAt(i); if ((c > 0) && (c <= 0x7F)) buffer[j++] = (byte) c; else if (c <= 0x7FF) { buffer[j++] = (byte) (0xC0 | ((c >> 6) & 0x1F)); buffer[j++] = (byte) (0x80 | ((c >> 0) & 0x3F)); } else { buffer[j++] = (byte) (0xE0 | ((c >> 12) & 0x0F)); buffer[j++] = (byte) (0x80 | ((c >> 6) & 0x3F)); buffer[j++] = (byte) (0x80 | ((c >> 0) & 0x3F)); } } return buffer; } /** True if this class/interface implements the interface iface. */ public final boolean implementsInterface(ClassType iface) { if (this == iface) return true; ClassType baseClass = this.getSuperclass(); if (baseClass != null && baseClass.implementsInterface(iface)) return true; ClassType[] interfaces = getInterfaces(); if (interfaces != null) { for (int i = interfaces.length; --i >= 0; ) { if (interfaces[i].implementsInterface(iface)) return true; } } return false; } public final boolean isSubclass(ClassType other) { if (other.isInterface()) return implementsInterface(other); if ((this == tostring_type && other == string_type) || (this == string_type && other == tostring_type)) return true; ClassType baseClass = this; while (baseClass != null) { if (baseClass == other) return true; baseClass = baseClass.getSuperclass(); } return false; } public int compare(Type other) { if (other == nullType) return 1; if (other instanceof PrimType) return swappedCompareResult(((PrimType) other).compare(this)); if (other instanceof ArrayType) return swappedCompareResult(((ArrayType) other).compare(this)); if (! (other instanceof ClassType)) return -3; String name = getName(); if (name != null && name.equals(other.getName())) return 0; ClassType cother = (ClassType) other; if (isSubclass(cother)) return -1; if (cother.isSubclass(this)) return 1; if (this == tostring_type) return 1; if (cother == tostring_type) return -1; if (this.isInterface() || cother.isInterface()) return -2; return -3; } public String toString() { return "ClassType " + getName(); } /** * @serialData Write the class name (as given by getName()) using writeUTF. */ public void writeExternal(ObjectOutput out) throws IOException { out.writeUTF(getName()); } public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { setName(in.readUTF()); flags |= ClassType.EXISTING_CLASS; } public Object readResolve() throws ObjectStreamException { String name = getName(); Type found = lookupType(name); if (found != null) return found; mapNameToType.put(name, this); return this; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -