javaclass.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 818 行 · 第 1/2 页
JAVA
818 行
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */package com.caucho.bytecode;import com.caucho.vfs.ReadStream;import com.caucho.vfs.Vfs;import com.caucho.vfs.WriteStream;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.lang.reflect.Modifier;import java.net.URL;import java.util.ArrayList;import java.util.logging.Level;import java.util.logging.Logger;/** * Represents a java class. */public class JavaClass extends JClass { static private final Logger log = Logger.getLogger(JavaClass.class.getName()); public static final int MAGIC = 0xcafebabe; public static final int ACC_PUBLIC = 0x0001; public static final int ACC_PRIVATE = 0x0002; public static final int ACC_PROTECTED = 0x0004; public static final int ACC_STATIC = 0x0008; public static final int ACC_FINAL = 0x0010; public static final int ACC_SUPER = 0x0020; private JavaClassLoader _loader; private URL _url; private int _major; private int _minor; private ConstantPool _constantPool = new ConstantPool(); private int _accessFlags; private String _thisClass; private String _superClass; private ArrayList<String> _interfaces = new ArrayList<String>(); private ArrayList<JavaField> _fields = new ArrayList<JavaField>(); private ArrayList<JavaMethod> _methods = new ArrayList<JavaMethod>(); private ArrayList<Attribute> _attributes = new ArrayList<Attribute>(); private JavaAnnotation []_annotations; private boolean _isWrite; public JavaClass(JavaClassLoader loader) { if (loader == null) throw new NullPointerException(); _loader = loader; } /** * Returns the loader. */ public JavaClassLoader getClassLoader() { return _loader; } public void setWrite(boolean isWrite) { _isWrite = isWrite; } /** * Sets the URL. */ public void setURL(URL url) { _url = url; } /** * Sets the major identifier of the class file. */ public void setMajor(int major) { _major = major; } /** * Gets the major identifier of the class file. */ public int getMajor() { return _major; } /** * Sets the minor identifier of the class file. */ public void setMinor(int minor) { _minor = minor; } /** * Gets the minor identifier of the class file. */ public int getMinor() { return _minor; } /** * Returns the class's constant pool. */ public ConstantPool getConstantPool() { return _constantPool; } /** * Sets the access flags. */ public void setAccessFlags(int flags) { _accessFlags = flags; } /** * Gets the access flags. */ public int getAccessFlags() { lazyLoad(); return _accessFlags; } /** * Sets this class. */ public void setThisClass(String className) { _thisClass = className; if (_isWrite) getConstantPool().addClass(className); } /** * Gets this class name. */ public String getThisClass() { return _thisClass; } /** * Sets the super class. */ public void setSuperClass(String className) { _superClass = className; getConstantPool().addClass(className); } /** * Gets the super class name. */ public String getSuperClassName() { lazyLoad(); return _superClass; } /** * Gets the super class name. */ public JClass getSuperClass() { lazyLoad(); if (_superClass == null) return null; else return getClassLoader().forName(_superClass.replace('/', '.')); } /** * Returns true for a final class. */ public boolean isFinal() { return Modifier.isFinal(getAccessFlags()); } /** * Returns true for an abstract class. */ public boolean isAbstract() { return Modifier.isAbstract(getAccessFlags()); } /** * Returns true for a public class. */ public boolean isPublic() { return Modifier.isPublic(getAccessFlags()); } /** * Returns true for a primitive class. */ public boolean isPrimitive() { return false; } /** * Adds an interface. */ public void addInterface(String className) { _interfaces.add(className); if (_isWrite) getConstantPool().addClass(className); } /** * Adds an interface. */ public ArrayList<String> getInterfaceNames() { return _interfaces; } /** * Gets the interfaces. */ public JClass []getInterfaces() { lazyLoad(); JClass []interfaces = new JClass[_interfaces.size()]; for (int i = 0; i < _interfaces.size(); i++) { String name = _interfaces.get(i); name = name.replace('/', '.'); interfaces[i] = getClassLoader().forName(name); } return interfaces; } /** * Adds a field */ public void addField(JavaField field) { _fields.add(field); } public JavaField createField(String name, String descriptor) { if (! _isWrite) throw new IllegalStateException("create field requires write"); JavaField jField = new JavaField(); jField.setWrite(true); jField.setJavaClass(this); jField.setName(name); jField.setDescriptor(descriptor); _fields.add(jField); return jField; } /** * Returns the fields. */ public ArrayList<JavaField> getFieldList() { lazyLoad(); return _fields; } /** * Returns a fields. */ public JavaField getField(String name) { ArrayList<JavaField> fieldList = getFieldList(); for (int i = 0; i < fieldList.size(); i++) { JavaField field = fieldList.get(i); if (field.getName().equals(name)) return field; } return null; } /** * Adds a method */ public void addMethod(JavaMethod method) { _methods.add(method); } public JavaMethod createMethod(String name, String descriptor) { if (! _isWrite) throw new IllegalStateException("create method requires write"); JavaMethod jMethod = new JavaMethod(); jMethod.setWrite(true); jMethod.setJavaClass(this); jMethod.setName(name); jMethod.setDescriptor(descriptor); _methods.add(jMethod); return jMethod; } /** * Returns the methods. */ public ArrayList<JavaMethod> getMethodList() { lazyLoad(); return _methods; } /** * Returns true for an array. */ public boolean isArray() { return false; } /** * Returns true for an interface. */ public boolean isInterface() { lazyLoad(); return Modifier.isInterface(_accessFlags); } /** * Returns a method. */ public JavaMethod getMethod(String name) { ArrayList<JavaMethod> methodList = getMethodList(); for (int i = 0; i < methodList.size(); i++) { JavaMethod method = methodList.get(i);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?