javaclass.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 818 行 · 第 1/2 页

JAVA
818
字号
      if (method.getName().equals(name))	return method;    }    return null;  }  /**   * Finds a method.   */  public JavaMethod findMethod(String name, String descriptor)  {    ArrayList<JavaMethod> methodList = getMethodList();        for (int i = 0; i < methodList.size(); i++) {      JavaMethod method = methodList.get(i);      if (method.getName().equals(name) &&	  method.getDescriptor().equals(descriptor))	return method;    }    return null;  }  /**   * Adds an attribute   */  public void addAttribute(Attribute attr)  {    _attributes.add(attr);    attr.addConstants(this);  }  /**   * Returns the methods.   */  public ArrayList<Attribute> getAttributeList()  {    lazyLoad();        return _attributes;  }  /**   * Returns the attribute.   */  public Attribute getAttribute(String name)  {    ArrayList<Attribute> attributeList = getAttributeList();        for (int i = attributeList.size() - 1; i >= 0; i--) {      Attribute attr = attributeList.get(i);      if (attr.getName().equals(name))	return attr;    }    return null;  }  //  // JClass methods.  //  /**   * Returns the class-equivalent name.   */  public String getName()  {    return getThisClass().replace('/', '.');  }    /**   * Returns true if the class is assignable from the argument.   */  public boolean isAssignableFrom(JClass cl)  {    if (getName().equals(cl.getName()))      return true;    JClass []ifc = cl.getInterfaces();    for (int i = 0; i < ifc.length; i++) {      if (isAssignableFrom(ifc[i]))	return true;    }    if (cl.getSuperClass() != null)      return isAssignableFrom(cl.getSuperClass());    else      return false;  }    /**   * Returns true if the class is assignable from the argument.   */  public boolean isAssignableFrom(Class cl)  {    if (getName().equals(cl.getName()))      return true;    Class []ifc = cl.getInterfaces();    for (int i = 0; i < ifc.length; i++) {      if (isAssignableFrom(ifc[i]))	return true;    }    if (cl.getSuperclass() != null)      return isAssignableFrom(cl.getSuperclass());    else      return false;  }    /**   * Returns true if the class is assignable from the argument.   */  public boolean isAssignableTo(Class cl)  {    if (getName().equals(cl.getName()))      return true;    JClass []ifc = getInterfaces();    for (int i = 0; i < ifc.length; i++) {      if (ifc[i].isAssignableTo(cl))	return true;    }    if (getSuperClass() != null)      return getSuperClass().isAssignableTo(cl);    else      return false;  }    /**   * Returns the array of declared methods.   */  public JMethod []getDeclaredMethods()  {    ArrayList<JavaMethod> methodList = getMethodList();        JMethod[] methods = new JMethod[methodList.size()];    methodList.toArray(methods);    return methods;  }    /**   * Returns the array of declared methods.   */  public JMethod []getConstructors()  {    ArrayList<JavaMethod> ctorList = new ArrayList<JavaMethod>();        for (JavaMethod method : getMethodList()) {      if (method.getName().equals("<init>"))	ctorList.add(method);    }        JMethod[] methods = new JMethod[ctorList.size()];    ctorList.toArray(methods);    return methods;  }    /**   * Returns the matching method   */  public JMethod getMethod(String name, JClass []paramTypes)  {    loop:    for (JMethod method : getMethods()) {      if (! method.getName().equals(name))	continue;      JClass []mParamTypes = method.getParameterTypes();      if (mParamTypes.length != paramTypes.length)	continue;      for (int i = 0; i < paramTypes.length; i++) {	if (! paramTypes[i].getName().equals(mParamTypes[i].getName()))	  continue loop;      }      return method;    }    return null;  }    /**   * Returns the matching method   */  public JMethod []getMethods()  {    ArrayList<JMethod> methodList = new ArrayList<JMethod>();    getMethods(methodList);    JMethod []methods = new JMethod[methodList.size()];    methodList.toArray(methods);    return methods;  }    /**   * Returns the matching method   */  private void getMethods(ArrayList<JMethod> methodList)  {    for (JMethod method : getDeclaredMethods()) {      if (! methodList.contains(method))	methodList.add(method);    }    if (getSuperClass() != null) {      for (JMethod method : getSuperClass().getMethods()) {	if (! methodList.contains(method))	  methodList.add(method);      }    }  }    /**   * Returns the array of declared fields.   */  public JField []getDeclaredFields()  {    ArrayList<JavaField> fieldList = getFieldList();        JField[] fields = new JField[fieldList.size()];    fieldList.toArray(fields);    return fields;  }    /**   * Returns the array of fields.   */  public JField []getFields()  {    ArrayList<JField> fieldList = new ArrayList<JField>();    getFields(fieldList);    JField []fields = new JField[fieldList.size()];    fieldList.toArray(fields);    return fields;  }    /**   * Returns all the fields   */  private void getFields(ArrayList<JField> fieldList)  {    for (JField field : getDeclaredFields()) {      if (! fieldList.contains(field))	fieldList.add(field);    }    if (getSuperClass() != null) {      for (JField field : getSuperClass().getFields()) {	if (! fieldList.contains(field))	  fieldList.add(field);      }    }  }  /**   * Returns the declared annotations.   */  public JAnnotation []getDeclaredAnnotations()  {    if (_annotations == null) {      Attribute attr = getAttribute("RuntimeVisibleAnnotations");      if (attr instanceof OpaqueAttribute) {	byte []buffer = ((OpaqueAttribute) attr).getValue();		try {	  ByteArrayInputStream is = new ByteArrayInputStream(buffer);	  ConstantPool cp = getConstantPool();	  _annotations = JavaAnnotation.parseAnnotations(is, cp,							 getClassLoader());	} catch (IOException e) {	  log.log(Level.FINER, e.toString(), e);	}      }      if (_annotations == null) {	_annotations = new JavaAnnotation[0];      }    }        return _annotations;  }    /**   * Returns the annotation.   */  public JAnnotation getAnnotation(String className)  {    JAnnotation []annList = getDeclaredAnnotations();    for (int i = 0; i < annList.length; i++) {      if (annList[i].getType().equals(className))	return annList[i];    }        return null;  }  /**   * Lazily load the class.   */  private void lazyLoad()  {    if (_major > 0)      return;    try {      if (_url == null)	throw new IllegalStateException();            InputStream is = _url.openStream();      ReadStream rs = Vfs.openRead(is);      try {	_major = 1;		ByteCodeParser parser = new ByteCodeParser();	parser.setClassLoader(_loader);	parser.setJavaClass(this);	parser.parse(rs);      } finally {	rs.close();	is.close();      }    } catch (RuntimeException e) {      throw e;    } catch (Exception e) {      throw new RuntimeException(e);    }  }  /**   * Writes the class to the output.   */  public void write(WriteStream os)    throws IOException  {    ByteCodeWriter out = new ByteCodeWriter(os, this);    out.writeInt(MAGIC);    out.writeShort(_minor);    out.writeShort(_major);    _constantPool.write(out);    out.writeShort(_accessFlags);    out.writeClass(_thisClass);    out.writeClass(_superClass);    out.writeShort(_interfaces.size());    for (int i = 0; i < _interfaces.size(); i++) {      String className = _interfaces.get(i);      out.writeClass(className);    }    out.writeShort(_fields.size());    for (int i = 0; i < _fields.size(); i++) {      JavaField field = _fields.get(i);      field.write(out);    }    out.writeShort(_methods.size());    for (int i = 0; i < _methods.size(); i++) {      JavaMethod method = _methods.get(i);      method.write(out);    }    out.writeShort(_attributes.size());    for (int i = 0; i < _attributes.size(); i++) {      Attribute attr = _attributes.get(i);      attr.write(out);    }  }  public String toString()  {    return "JavaClass[" + _thisClass + "]";  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?