You are viewing a plain text version of this content. The canonical link for it is here.
Posted to kato-commits@incubator.apache.org by mo...@apache.org on 2009/11/23 15:54:15 UTC
svn commit: r883384 [11/47] - in /incubator/kato/trunk/org.apache.kato: ./
kato.anttasks/src/main/java/org/apache/kato/anttasks/
kato.anttasks/src/main/java/org/apache/kato/anttasks/sitebuilder/
kato.anttasks/src/main/java/org/apache/kato/anttasks/tck/...
Modified: incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaClassImpl.java
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaClassImpl.java?rev=883384&r1=883383&r2=883384&view=diff
==============================================================================
--- incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaClassImpl.java (original)
+++ incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaClassImpl.java Mon Nov 23 15:53:48 2009
@@ -1,756 +1,756 @@
-/*******************************************************************************
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- ******************************************************************************/
-package org.apache.kato.hprof.java;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-
-import javax.tools.diagnostics.image.CorruptDataException;
-import javax.tools.diagnostics.image.DataUnavailable;
-import javax.tools.diagnostics.image.ImagePointer;
-import javax.tools.diagnostics.image.MemoryAccessException;
-import javax.tools.diagnostics.runtime.java.JavaClass;
-import javax.tools.diagnostics.runtime.java.JavaClassLoader;
-import javax.tools.diagnostics.runtime.java.JavaField;
-import javax.tools.diagnostics.runtime.java.JavaObject;
-
-import org.apache.kato.common.BasicType;
-import org.apache.kato.hprof.IJavaClass;
-import org.apache.kato.hprof.datalayer.ConstantPoolEntry;
-import org.apache.kato.hprof.datalayer.FieldEntry;
-import org.apache.kato.hprof.datalayer.InstanceFieldEntry;
-import org.apache.kato.hprof.datalayer.StaticFieldEntry;
-import org.apache.kato.hprof.image.CorruptDataImpl;
-import org.apache.kato.hprof.image.ImagePointerImpl;
-
-/**
- * HProf implementation of JavaClass.
- *
- *
- */
-public class JavaClassImpl implements JavaClass {
- IJavaHeapInternal heap;
- IJavaClass javaClass;
-
- /**
- * Superclass to static and instance fields.
- *
- */
- abstract class JavaFieldImpl implements JavaField {
- FieldEntry field;
-
- /**
- * Returns the signature of this field.
- * Unfortunately, only the fact that a field is an object reference
- * is recorded.
- * If we find all objects an object reference field refers to,
- * if we take the lowest common subclass then that is the closest to
- * the field's actual type. It would be prohibitively expensive to do
- * that though.
- */
- @Override
- public String getSignature() throws CorruptDataException {
- switch (field.getType()) {
- case BasicType.OBJECT:
- return "Ljava/lang/Object;";
- case BasicType.BOOLEAN:
- return "Z";
- case BasicType.CHAR:
- return "C";
- case BasicType.FLOAT:
- return "F";
- case BasicType.DOUBLE:
- return "D";
- case BasicType.BYTE:
- return "B";
- case BasicType.SHORT:
- return "S";
- case BasicType.INT:
- return "I";
- case BasicType.LONG:
- return "J";
- default:
- throw new CorruptDataException(new CorruptDataImpl("Unexpected basic type of " + field.getType()+
- "encountered."));
- }
- }
-
- @Override
- public String getName() throws CorruptDataException {
- return heap.getUTF8StringByID(field.getFieldNameID());
- }
-
- @Override
- public JavaClass getDeclaringClass() throws CorruptDataException,
- DataUnavailable {
- return JavaClassImpl.this;
- }
-
- String getStringFromJavaObject(JavaObject obj) throws CorruptDataException, MemoryAccessException {
- JavaClass clazz = obj.getJavaClass();
- JavaObject value=null;
- int offset=0;
- int count=0;
- boolean valueSet = false, offsetSet = false, countSet=false;
-
- Iterator iter = clazz.getDeclaredFields().iterator();
-
- while (iter.hasNext()) {
- JavaField field = (JavaField) iter.next();
- String name = field.getName();
- if("value".equals(name)) {
- value = (JavaObject) field.get(obj);
- valueSet = true;
- } else if ("offset".equals(name)) {
- offset = field.getInt(obj);
- offsetSet = true;
- } else if ("count".equals(name)) {
- count = field.getInt(obj);
- countSet = true;
- }
-
- }
-
- if (valueSet == false || offsetSet == false || countSet == false ) {
- throw new CorruptDataException(new CorruptDataImpl("Unable to find all String classes fields values"));
- }
- char characters[] = new char[count];
-
- value.arraycopy(offset, characters, 0, count);
-
- return new String(characters);
- }
- }
-
- class InstanceFieldImpl extends JavaFieldImpl {
-
-
- public InstanceFieldImpl(InstanceFieldEntry field) {
- this.field = field;
- }
-
- /**
- * Returns true if this field can retrieve a value from the passed class.
- *
- * @param clazz JavaClassImpl of object to retrieve field from
- * @return true if the class is compatible
- *
- * @throws CorruptDataException
- */
- private boolean isCompatible(JavaClassImpl clazz) throws CorruptDataException {
- // checks that this field can be extracted from the passed clazz
-
- while(clazz != null) {
- if (clazz.equals(JavaClassImpl.this)) {
- return true;
- }
-
- clazz = clazz.getSuperclass();
- }
-
- return false;
- }
-
- /**
- * Returns the offset of this field into a given object.
- *
- * @param clazz JavaClassImpl
- * @return int offset
- * @throws CorruptDataException
- */
- private int getOffset(JavaClassImpl clazz) throws CorruptDataException {
- // The potential subclasses of this field have to be skipped before
- // using the stored field offset.
- int offset = ((InstanceFieldEntry)field).getOffset();
- while ((clazz != null) && !clazz.equals(JavaClassImpl.this)) {
- offset += clazz.getOffsetSize(); // Or some variant.
- clazz = clazz.getSuperclass();
- }
- return offset;
- }
-
- @Override
- public Object get(JavaObject obj) throws CorruptDataException,
- MemoryAccessException {
-
- if (! (obj instanceof JavaObjectInstanceImpl)) {
- throw new IllegalArgumentException("Passed object must be an ordinary object");
- }
-
- JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
-
- if (!isCompatible(clazz)) {
- throw new IllegalArgumentException("Field " +
- JavaClassImpl.this.getName() + "." +
- getName() +" not in class hierachy of object 0x"+
- Long.toHexString(obj.getID().getAddress()));
- }
-
- // The potential subclasses of this field have to be skipped before
- // using the stored field offset.
- int offset = getOffset(clazz);
-
- JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
-
- switch(field.getType()) {
- case BasicType.BOOLEAN:
- return new Boolean(instance.getBooleanField(offset));
- case BasicType.BYTE:
- return new Byte(instance.getByteField(offset));
- case BasicType.SHORT:
- return new Short(instance.getShortField(offset));
- case BasicType.CHAR:
- return new Character(instance.getCharField(offset));
- case BasicType.INT:
- return new Integer(instance.getIntegerField(offset));
- case BasicType.LONG:
- return new Long(instance.getLongField(offset));
- case BasicType.FLOAT:
- return new Float(instance.getFloatField(offset));
- case BasicType.DOUBLE:
- return new Double(instance.getDoubleField(offset));
- case BasicType.OBJECT:
- return JavaClassImpl.this.heap.getObjectByID(instance.getIDField(offset));
- default:
- throw new CorruptDataException(new CorruptDataImpl("Invalid field type "
- +field.getType()));
- }
- }
-
- @Override
- public boolean getBoolean(JavaObject obj) throws CorruptDataException,
- MemoryAccessException {
- JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
- JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
- int offset = getOffset(clazz);
-
- return instance.getBooleanField(offset);
- }
-
- @Override
- public byte getByte(JavaObject obj) throws CorruptDataException,
- MemoryAccessException {
- JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
- JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
- int offset = getOffset(clazz);
-
- return instance.getByteField(offset);
- }
-
- @Override
- public char getChar(JavaObject obj) throws CorruptDataException,
- MemoryAccessException {
- JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
- JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
- int offset = getOffset(clazz);
-
- return instance.getCharField(offset);
- }
-
- @Override
- public double getDouble(JavaObject obj) throws CorruptDataException,
- MemoryAccessException {
- JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
- JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
- int offset = getOffset(clazz);
-
- return instance.getDoubleField(offset);
- }
-
- @Override
- public float getFloat(JavaObject obj) throws CorruptDataException,
- MemoryAccessException {
- JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
- JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
- int offset = getOffset(clazz);
-
- return instance.getFloatField(offset);
-
- }
-
- @Override
- public int getInt(JavaObject obj) throws CorruptDataException,
- MemoryAccessException {
- JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
- JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
- int offset = getOffset(clazz);
-
- return instance.getIntegerField(offset);
-
- }
-
- @Override
- public long getLong(JavaObject obj) throws CorruptDataException,
- MemoryAccessException {
- JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
- JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
- int offset = getOffset(clazz);
-
- return instance.getLongField(offset);
-
- }
-
- @Override
- public short getShort(JavaObject obj) throws CorruptDataException,
- MemoryAccessException {
- JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
- JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
- int offset = getOffset(clazz);
-
- return instance.getShortField(offset);
- }
-
- @Override
- public String getString(JavaObject obj) throws CorruptDataException,
- MemoryAccessException {
- Object fieldObj = get(obj);
- if (fieldObj == null) {
- return null;
- }
- if (!(fieldObj instanceof JavaObject)) {
- throw new IllegalArgumentException("Field "+getName()+" not an object");
- }
- JavaObject jObject = (JavaObject) fieldObj;
-
- if (! "java/lang/String".equals( jObject.getJavaClass().getName() )) {
- throw new IllegalArgumentException("Field "+getName()+" refers to 0x"+
- Long.toHexString(jObject.getID().getAddress()) +
- ", which not a string - it's a " + jObject.getJavaClass());
- }
-
- return getStringFromJavaObject(jObject);
- }
-
- @Override
- public int getModifiers() throws CorruptDataException {
- return 0;
- }
-
- }
-
- /**
- * Represents a static field.
- * Passed JavaObject is always ignored. The actual value is stored
- * within the StaticFieldEntry.
- *
- */
- class StaticFieldImpl extends JavaFieldImpl {
-
- StaticFieldImpl(StaticFieldEntry field) {
- this.field = field;
- }
-
- /**
- * Retrieves the field and returns an Object - primitive types are
- * boxed. Other types are implementations of JavaObject, or null.
- */
- @Override
- public Object get(JavaObject arg0) throws CorruptDataException,
- MemoryAccessException {
- StaticFieldEntry field = (StaticFieldEntry) this.field;
-
- switch(field.getType()){
- case BasicType.OBJECT:
- return heap.getObjectByID(field.getIDField());
- case BasicType.BOOLEAN:
- return new Boolean(field.getBooleanField());
- case BasicType.CHAR:
- return new Character(field.getCharField());
- case BasicType.FLOAT:
- return new Float(field.getFloatField());
- case BasicType.DOUBLE:
- return new Double(field.getDoubleField());
- case BasicType.BYTE:
- return new Byte(field.getByteField());
- case BasicType.SHORT:
- return new Short(field.getShortField());
- case BasicType.INT:
- return new Integer(field.getIntegerField());
- case BasicType.LONG:
- return new Long(field.getLongField());
- default:
- throw new CorruptDataException(new CorruptDataImpl("Unexpected basic type of " + field.getType()+
- "encountered."));
- }
- }
-
- @Override
- public boolean getBoolean(JavaObject arg0) throws CorruptDataException,
- MemoryAccessException {
- return ((StaticFieldEntry)field).getBooleanField();
- }
-
- @Override
- public byte getByte(JavaObject arg0) throws CorruptDataException,
- MemoryAccessException {
- return ((StaticFieldEntry)field).getByteField();
- }
-
- @Override
- public char getChar(JavaObject arg0) throws CorruptDataException,
- MemoryAccessException {
- return ((StaticFieldEntry)field).getCharField();
- }
-
- @Override
- public double getDouble(JavaObject arg0) throws CorruptDataException,
- MemoryAccessException {
- return ((StaticFieldEntry)field).getDoubleField();
- }
-
- @Override
- public float getFloat(JavaObject arg0) throws CorruptDataException,
- MemoryAccessException {
- return ((StaticFieldEntry)field).getFloatField();
- }
-
- @Override
- public int getInt(JavaObject arg0) throws CorruptDataException,
- MemoryAccessException {
- return ((StaticFieldEntry)field).getIntegerField();
- }
-
- @Override
- public long getLong(JavaObject arg0) throws CorruptDataException,
- MemoryAccessException {
- return ((StaticFieldEntry)field).getLongField();
- }
-
- @Override
- public short getShort(JavaObject arg0) throws CorruptDataException,
- MemoryAccessException {
- return ((StaticFieldEntry)field).getShortField();
- }
-
- @Override
- public String getString(JavaObject arg0) throws CorruptDataException,
- MemoryAccessException {
-
- return getStringFromJavaObject(heap.getObjectByID(((StaticFieldEntry)field).getIDField()));
- }
-
- /**
- * This field is static - nothing else is known.
- */
- @Override
- public int getModifiers() throws CorruptDataException {
- return java.lang.reflect.Modifier.STATIC;
- }
- }
-
- public JavaClassImpl(IJavaHeapInternal heap, IJavaClass javaClass) {
- this.heap = heap;
- this.javaClass = javaClass;
- }
-
- @Override
- public JavaClassLoader getClassLoader() throws CorruptDataException {
- return heap.getJavaClassLoaderByID(javaClass.getClassLoaderObjectID());
- }
-
- /**
- * Get the component type of this class if it is an array class.
- *
- * Primitive arrays have primitives as elements, so their component
- * type are classes called "int", etc. These classes are faked using
- * JavaPrimitiveClassImpl as they don't exist in hprof dumps.
- *
- * An int[][] array is an array of arrays, hence not primitive.
- *
- * The component types are retrieved by name from the JavaClassLoader this
- * array class belongs to.
- */
- @Override
- public JavaClass getComponentType() throws CorruptDataException {
- if (!isArray()) {
- throw new IllegalArgumentException("Class is not an array type");
- }
-
- String name = javaClass.getClassName();
-
- if ("boolean[]".equals(name)) {
- return heap.getPrimitiveClass(BasicType.BOOLEAN);
- }if ("byte[]".equals(name)) {
- return heap.getPrimitiveClass(BasicType.BYTE);
- }if ("short[]".equals(name)) {
- return heap.getPrimitiveClass(BasicType.SHORT);
- }if ("char[]".equals(name)) {
- return heap.getPrimitiveClass(BasicType.CHAR);
- }if ("int[]".equals(name)) {
- return heap.getPrimitiveClass(BasicType.INT);
- }if ("long[]".equals(name)) {
- return heap.getPrimitiveClass(BasicType.LONG);
- }if ("float[]".equals(name)) {
- return heap.getPrimitiveClass(BasicType.FLOAT);
- }if ("double[]".equals(name)) {
- return heap.getPrimitiveClass(BasicType.DOUBLE);
- }
-
- String componentName = this.getName().substring(1);
-
- /* If the array was "[Ljava/lang/String;"
- * Strip it to "java/lang/String"
- */
- if(componentName.charAt(0) == 'L') {
- componentName = componentName.substring(1,componentName.length()-1);
- }
-
- return getClassLoader().findClass(componentName);
- }
-
- @Override
- public List getConstantPoolReferences() {
- List cpeList = new LinkedList();
-
-
- ConstantPoolEntry[] cpes = this.javaClass.getConstantPoolEntries();
-
- for ( ConstantPoolEntry cpe : cpes) {
- if (cpe.type == BasicType.OBJECT) {
- // cpeList.add(heap.getObjectByID(cpe.data));
- }
- }
- return null;
- }
-
- /**
- * The static and instance fields belonging to this class.
- *
- */
- private ArrayList<JavaField> fields;
- @Override
- public List getDeclaredFields() {
- if (fields == null) {
- StaticFieldEntry staticFields[] = javaClass.getStaticFields();
- InstanceFieldEntry instanceFields[] = javaClass.getInstanceFields();
-
- fields = new ArrayList<JavaField>(staticFields.length + instanceFields.length);
-
- for (StaticFieldEntry f : staticFields) {
- fields.add(new StaticFieldImpl(f));
- }
-
- for (InstanceFieldEntry f : instanceFields) {
- fields.add(new InstanceFieldImpl(f));
- }
- }
-
- return fields;
- }
-
- /**
- * Method information isn't stored within classes.
- *
- */
- @Override
- public List getDeclaredMethods() {
- return new LinkedList();
- }
-
-
- /**
- * Wraps up object ID of class as it's ID.
- * That should be sufficiently unique.
- *
- * @return ImagePointer
- */
- @Override
- public ImagePointer getID() {
- return heap.getImagePointer(javaClass.getClassObjectID());
- }
-
- /**
- * Implemented interfaces aren't stored in Hprof.
- * The constant pool could be used if the right entries could be targeted,
- * but there is nothing to suggest that that is possible.
- */
- @Override
- public List getInterfaces() {
- return new LinkedList();
- }
-
- /**
- * There is no modifier information to return
- * @return 0
- */
- @Override
- public int getModifiers() throws CorruptDataException {
- return 0;
- }
-
- /**
- * Returns the classes's name. This is stored in hprof with "." and "$" separators,
- * the "."s are replaced by "/"s to conform with the API.
- *
- * @return String of the class's name.
- */
- @Override
- public String getName() throws CorruptDataException {
- String name = javaClass.getClassName();
-
- /* Process the array's such that they are of the format:
- * [Ljava/lang/Object;
- *
- * or
- * [[B
- *
- * Primitive arrays are renamed correctly.
- */
- if(name.indexOf('[') != -1) {
- int depth=0;
- String prefix = "";
- for(int n=0; n < name.length(); n++) {
- if (name.charAt(n) == '[') {
- prefix+="[";
- }
- }
-
- // Chop off arrays.
- name = name.substring(0, name.indexOf('['));
-
- if (name.startsWith("boolean")) {
- name = "Z";
- } else if (name.startsWith("byte")) {
- name = "B";
- } else if (name.startsWith("char")) {
- name = "C";
- } else if (name.startsWith("short")) {
- name = "S";
- } else if (name.startsWith("int")) {
- name = "I";
- } else if (name.startsWith("long")) {
- name = "J";
- } else if (name.startsWith("float")) {
- name = "F";
- } else if (name.startsWith("double")) {
- name = "D";
- } else {
- name = "L" + name.replace('.', '/') +";";
- }
- return prefix+name;
- }
- return name.replace('.', '/');
- }
-
- /**
- * Returns null. There are no java/lang/Class objects referenced
- * by the class records.
- */
- @Override
- public JavaObject getObject() throws CorruptDataException {
- return null;
- }
-
- /**
- * Return the superclass of this class or null.
- *
- * @return JavaClassImpl or null
- */
- @Override
- public JavaClassImpl getSuperclass() throws CorruptDataException {
- long superClassObjId = javaClass.getSuperClassObjectID();
-
- // This is an assumption, but it will be necessary.
- if (superClassObjId == 0L) {
- return null;
- }
-
- return heap.getJavaClassByID(superClassObjId);
- }
-
- @Override
- public List getReferences() {
- // TODO Auto-generated method stub
- // All object references, constantpool entries, superclass, etc.
- return null;
- }
-
- /**
- * Returns true if this is an array.
- *
- * @return true if this class is an array type.
- */
- @Override
- public boolean isArray() throws CorruptDataException {
- return javaClass.getClassName().indexOf('[') != -1;
- }
-
- /**
- * @return true if passed object is this JavaClass
- */
- public boolean equals(Object obj) {
- if (obj == null) {
- return false;
- }
-
- if (!(obj instanceof JavaClassImpl)) {
- return false;
- }
-
- if (obj == this) {
- return true;
- }
-
- JavaClassImpl clazz = (JavaClassImpl) obj;
-
- return clazz.getObjectID() == getObjectID();
- }
-
- /**
- * Gets the object ID that uniquely identifies this class on the heap.
- *
- * @return long object ID
- */
- long getObjectID() {
- return javaClass.getClassObjectID();
- }
-
- public String toString() {
- String name = "<UNSET CLASS NAME>";
- try {
- name = this.getName();
- } catch (CorruptDataException e) {
- // oh well.
- }
- return name+" @ "+ Long.toHexString(javaClass.getClassObjectID());
- }
-
- public int hashCode() {
- return (int) (javaClass.getClassObjectID() % Integer.MAX_VALUE);
- }
-
- /**
- * Return the size of object instances.
- *
- * @return int of the object size.
- */
- int getInstanceSize() {
- return javaClass.getInstanceSize();
- }
-
- /**
- * Returns the number of bytes the part of this class takes up in an instance object.
- * This is the number of bytes taken up by the fields in this class and none of its
- * superclasses.
- * This is different from getInstanceSize(), which is the size of an instance of this
- * class, including all superclasses.
- *
- * @return int fields size
- */
- int getOffsetSize() {
- return javaClass.getOffsetSize();
- }
-}
+/*******************************************************************************
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.kato.hprof.java;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import javax.tools.diagnostics.image.CorruptDataException;
+import javax.tools.diagnostics.image.DataUnavailable;
+import javax.tools.diagnostics.image.ImagePointer;
+import javax.tools.diagnostics.image.MemoryAccessException;
+import javax.tools.diagnostics.runtime.java.JavaClass;
+import javax.tools.diagnostics.runtime.java.JavaClassLoader;
+import javax.tools.diagnostics.runtime.java.JavaField;
+import javax.tools.diagnostics.runtime.java.JavaObject;
+
+import org.apache.kato.common.BasicType;
+import org.apache.kato.hprof.IJavaClass;
+import org.apache.kato.hprof.datalayer.ConstantPoolEntry;
+import org.apache.kato.hprof.datalayer.FieldEntry;
+import org.apache.kato.hprof.datalayer.InstanceFieldEntry;
+import org.apache.kato.hprof.datalayer.StaticFieldEntry;
+import org.apache.kato.hprof.image.CorruptDataImpl;
+import org.apache.kato.hprof.image.ImagePointerImpl;
+
+/**
+ * HProf implementation of JavaClass.
+ *
+ *
+ */
+public class JavaClassImpl implements JavaClass {
+ IJavaHeapInternal heap;
+ IJavaClass javaClass;
+
+ /**
+ * Superclass to static and instance fields.
+ *
+ */
+ abstract class JavaFieldImpl implements JavaField {
+ FieldEntry field;
+
+ /**
+ * Returns the signature of this field.
+ * Unfortunately, only the fact that a field is an object reference
+ * is recorded.
+ * If we find all objects an object reference field refers to,
+ * if we take the lowest common subclass then that is the closest to
+ * the field's actual type. It would be prohibitively expensive to do
+ * that though.
+ */
+ @Override
+ public String getSignature() throws CorruptDataException {
+ switch (field.getType()) {
+ case BasicType.OBJECT:
+ return "Ljava/lang/Object;";
+ case BasicType.BOOLEAN:
+ return "Z";
+ case BasicType.CHAR:
+ return "C";
+ case BasicType.FLOAT:
+ return "F";
+ case BasicType.DOUBLE:
+ return "D";
+ case BasicType.BYTE:
+ return "B";
+ case BasicType.SHORT:
+ return "S";
+ case BasicType.INT:
+ return "I";
+ case BasicType.LONG:
+ return "J";
+ default:
+ throw new CorruptDataException(new CorruptDataImpl("Unexpected basic type of " + field.getType()+
+ "encountered."));
+ }
+ }
+
+ @Override
+ public String getName() throws CorruptDataException {
+ return heap.getUTF8StringByID(field.getFieldNameID());
+ }
+
+ @Override
+ public JavaClass getDeclaringClass() throws CorruptDataException,
+ DataUnavailable {
+ return JavaClassImpl.this;
+ }
+
+ String getStringFromJavaObject(JavaObject obj) throws CorruptDataException, MemoryAccessException {
+ JavaClass clazz = obj.getJavaClass();
+ JavaObject value=null;
+ int offset=0;
+ int count=0;
+ boolean valueSet = false, offsetSet = false, countSet=false;
+
+ Iterator iter = clazz.getDeclaredFields().iterator();
+
+ while (iter.hasNext()) {
+ JavaField field = (JavaField) iter.next();
+ String name = field.getName();
+ if("value".equals(name)) {
+ value = (JavaObject) field.get(obj);
+ valueSet = true;
+ } else if ("offset".equals(name)) {
+ offset = field.getInt(obj);
+ offsetSet = true;
+ } else if ("count".equals(name)) {
+ count = field.getInt(obj);
+ countSet = true;
+ }
+
+ }
+
+ if (valueSet == false || offsetSet == false || countSet == false ) {
+ throw new CorruptDataException(new CorruptDataImpl("Unable to find all String classes fields values"));
+ }
+ char characters[] = new char[count];
+
+ value.arraycopy(offset, characters, 0, count);
+
+ return new String(characters);
+ }
+ }
+
+ class InstanceFieldImpl extends JavaFieldImpl {
+
+
+ public InstanceFieldImpl(InstanceFieldEntry field) {
+ this.field = field;
+ }
+
+ /**
+ * Returns true if this field can retrieve a value from the passed class.
+ *
+ * @param clazz JavaClassImpl of object to retrieve field from
+ * @return true if the class is compatible
+ *
+ * @throws CorruptDataException
+ */
+ private boolean isCompatible(JavaClassImpl clazz) throws CorruptDataException {
+ // checks that this field can be extracted from the passed clazz
+
+ while(clazz != null) {
+ if (clazz.equals(JavaClassImpl.this)) {
+ return true;
+ }
+
+ clazz = clazz.getSuperclass();
+ }
+
+ return false;
+ }
+
+ /**
+ * Returns the offset of this field into a given object.
+ *
+ * @param clazz JavaClassImpl
+ * @return int offset
+ * @throws CorruptDataException
+ */
+ private int getOffset(JavaClassImpl clazz) throws CorruptDataException {
+ // The potential subclasses of this field have to be skipped before
+ // using the stored field offset.
+ int offset = ((InstanceFieldEntry)field).getOffset();
+ while ((clazz != null) && !clazz.equals(JavaClassImpl.this)) {
+ offset += clazz.getOffsetSize(); // Or some variant.
+ clazz = clazz.getSuperclass();
+ }
+ return offset;
+ }
+
+ @Override
+ public Object get(JavaObject obj) throws CorruptDataException,
+ MemoryAccessException {
+
+ if (! (obj instanceof JavaObjectInstanceImpl)) {
+ throw new IllegalArgumentException("Passed object must be an ordinary object");
+ }
+
+ JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
+
+ if (!isCompatible(clazz)) {
+ throw new IllegalArgumentException("Field " +
+ JavaClassImpl.this.getName() + "." +
+ getName() +" not in class hierachy of object 0x"+
+ Long.toHexString(obj.getID().getAddress()));
+ }
+
+ // The potential subclasses of this field have to be skipped before
+ // using the stored field offset.
+ int offset = getOffset(clazz);
+
+ JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
+
+ switch(field.getType()) {
+ case BasicType.BOOLEAN:
+ return new Boolean(instance.getBooleanField(offset));
+ case BasicType.BYTE:
+ return new Byte(instance.getByteField(offset));
+ case BasicType.SHORT:
+ return new Short(instance.getShortField(offset));
+ case BasicType.CHAR:
+ return new Character(instance.getCharField(offset));
+ case BasicType.INT:
+ return new Integer(instance.getIntegerField(offset));
+ case BasicType.LONG:
+ return new Long(instance.getLongField(offset));
+ case BasicType.FLOAT:
+ return new Float(instance.getFloatField(offset));
+ case BasicType.DOUBLE:
+ return new Double(instance.getDoubleField(offset));
+ case BasicType.OBJECT:
+ return JavaClassImpl.this.heap.getObjectByID(instance.getIDField(offset));
+ default:
+ throw new CorruptDataException(new CorruptDataImpl("Invalid field type "
+ +field.getType()));
+ }
+ }
+
+ @Override
+ public boolean getBoolean(JavaObject obj) throws CorruptDataException,
+ MemoryAccessException {
+ JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
+ JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
+ int offset = getOffset(clazz);
+
+ return instance.getBooleanField(offset);
+ }
+
+ @Override
+ public byte getByte(JavaObject obj) throws CorruptDataException,
+ MemoryAccessException {
+ JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
+ JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
+ int offset = getOffset(clazz);
+
+ return instance.getByteField(offset);
+ }
+
+ @Override
+ public char getChar(JavaObject obj) throws CorruptDataException,
+ MemoryAccessException {
+ JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
+ JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
+ int offset = getOffset(clazz);
+
+ return instance.getCharField(offset);
+ }
+
+ @Override
+ public double getDouble(JavaObject obj) throws CorruptDataException,
+ MemoryAccessException {
+ JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
+ JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
+ int offset = getOffset(clazz);
+
+ return instance.getDoubleField(offset);
+ }
+
+ @Override
+ public float getFloat(JavaObject obj) throws CorruptDataException,
+ MemoryAccessException {
+ JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
+ JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
+ int offset = getOffset(clazz);
+
+ return instance.getFloatField(offset);
+
+ }
+
+ @Override
+ public int getInt(JavaObject obj) throws CorruptDataException,
+ MemoryAccessException {
+ JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
+ JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
+ int offset = getOffset(clazz);
+
+ return instance.getIntegerField(offset);
+
+ }
+
+ @Override
+ public long getLong(JavaObject obj) throws CorruptDataException,
+ MemoryAccessException {
+ JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
+ JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
+ int offset = getOffset(clazz);
+
+ return instance.getLongField(offset);
+
+ }
+
+ @Override
+ public short getShort(JavaObject obj) throws CorruptDataException,
+ MemoryAccessException {
+ JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
+ JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
+ int offset = getOffset(clazz);
+
+ return instance.getShortField(offset);
+ }
+
+ @Override
+ public String getString(JavaObject obj) throws CorruptDataException,
+ MemoryAccessException {
+ Object fieldObj = get(obj);
+ if (fieldObj == null) {
+ return null;
+ }
+ if (!(fieldObj instanceof JavaObject)) {
+ throw new IllegalArgumentException("Field "+getName()+" not an object");
+ }
+ JavaObject jObject = (JavaObject) fieldObj;
+
+ if (! "java/lang/String".equals( jObject.getJavaClass().getName() )) {
+ throw new IllegalArgumentException("Field "+getName()+" refers to 0x"+
+ Long.toHexString(jObject.getID().getAddress()) +
+ ", which not a string - it's a " + jObject.getJavaClass());
+ }
+
+ return getStringFromJavaObject(jObject);
+ }
+
+ @Override
+ public int getModifiers() throws CorruptDataException {
+ return 0;
+ }
+
+ }
+
+ /**
+ * Represents a static field.
+ * Passed JavaObject is always ignored. The actual value is stored
+ * within the StaticFieldEntry.
+ *
+ */
+ class StaticFieldImpl extends JavaFieldImpl {
+
+ StaticFieldImpl(StaticFieldEntry field) {
+ this.field = field;
+ }
+
+ /**
+ * Retrieves the field and returns an Object - primitive types are
+ * boxed. Other types are implementations of JavaObject, or null.
+ */
+ @Override
+ public Object get(JavaObject arg0) throws CorruptDataException,
+ MemoryAccessException {
+ StaticFieldEntry field = (StaticFieldEntry) this.field;
+
+ switch(field.getType()){
+ case BasicType.OBJECT:
+ return heap.getObjectByID(field.getIDField());
+ case BasicType.BOOLEAN:
+ return new Boolean(field.getBooleanField());
+ case BasicType.CHAR:
+ return new Character(field.getCharField());
+ case BasicType.FLOAT:
+ return new Float(field.getFloatField());
+ case BasicType.DOUBLE:
+ return new Double(field.getDoubleField());
+ case BasicType.BYTE:
+ return new Byte(field.getByteField());
+ case BasicType.SHORT:
+ return new Short(field.getShortField());
+ case BasicType.INT:
+ return new Integer(field.getIntegerField());
+ case BasicType.LONG:
+ return new Long(field.getLongField());
+ default:
+ throw new CorruptDataException(new CorruptDataImpl("Unexpected basic type of " + field.getType()+
+ "encountered."));
+ }
+ }
+
+ @Override
+ public boolean getBoolean(JavaObject arg0) throws CorruptDataException,
+ MemoryAccessException {
+ return ((StaticFieldEntry)field).getBooleanField();
+ }
+
+ @Override
+ public byte getByte(JavaObject arg0) throws CorruptDataException,
+ MemoryAccessException {
+ return ((StaticFieldEntry)field).getByteField();
+ }
+
+ @Override
+ public char getChar(JavaObject arg0) throws CorruptDataException,
+ MemoryAccessException {
+ return ((StaticFieldEntry)field).getCharField();
+ }
+
+ @Override
+ public double getDouble(JavaObject arg0) throws CorruptDataException,
+ MemoryAccessException {
+ return ((StaticFieldEntry)field).getDoubleField();
+ }
+
+ @Override
+ public float getFloat(JavaObject arg0) throws CorruptDataException,
+ MemoryAccessException {
+ return ((StaticFieldEntry)field).getFloatField();
+ }
+
+ @Override
+ public int getInt(JavaObject arg0) throws CorruptDataException,
+ MemoryAccessException {
+ return ((StaticFieldEntry)field).getIntegerField();
+ }
+
+ @Override
+ public long getLong(JavaObject arg0) throws CorruptDataException,
+ MemoryAccessException {
+ return ((StaticFieldEntry)field).getLongField();
+ }
+
+ @Override
+ public short getShort(JavaObject arg0) throws CorruptDataException,
+ MemoryAccessException {
+ return ((StaticFieldEntry)field).getShortField();
+ }
+
+ @Override
+ public String getString(JavaObject arg0) throws CorruptDataException,
+ MemoryAccessException {
+
+ return getStringFromJavaObject(heap.getObjectByID(((StaticFieldEntry)field).getIDField()));
+ }
+
+ /**
+ * This field is static - nothing else is known.
+ */
+ @Override
+ public int getModifiers() throws CorruptDataException {
+ return java.lang.reflect.Modifier.STATIC;
+ }
+ }
+
+ public JavaClassImpl(IJavaHeapInternal heap, IJavaClass javaClass) {
+ this.heap = heap;
+ this.javaClass = javaClass;
+ }
+
+ @Override
+ public JavaClassLoader getClassLoader() throws CorruptDataException {
+ return heap.getJavaClassLoaderByID(javaClass.getClassLoaderObjectID());
+ }
+
+ /**
+ * Get the component type of this class if it is an array class.
+ *
+ * Primitive arrays have primitives as elements, so their component
+ * type are classes called "int", etc. These classes are faked using
+ * JavaPrimitiveClassImpl as they don't exist in hprof dumps.
+ *
+ * An int[][] array is an array of arrays, hence not primitive.
+ *
+ * The component types are retrieved by name from the JavaClassLoader this
+ * array class belongs to.
+ */
+ @Override
+ public JavaClass getComponentType() throws CorruptDataException {
+ if (!isArray()) {
+ throw new IllegalArgumentException("Class is not an array type");
+ }
+
+ String name = javaClass.getClassName();
+
+ if ("boolean[]".equals(name)) {
+ return heap.getPrimitiveClass(BasicType.BOOLEAN);
+ }if ("byte[]".equals(name)) {
+ return heap.getPrimitiveClass(BasicType.BYTE);
+ }if ("short[]".equals(name)) {
+ return heap.getPrimitiveClass(BasicType.SHORT);
+ }if ("char[]".equals(name)) {
+ return heap.getPrimitiveClass(BasicType.CHAR);
+ }if ("int[]".equals(name)) {
+ return heap.getPrimitiveClass(BasicType.INT);
+ }if ("long[]".equals(name)) {
+ return heap.getPrimitiveClass(BasicType.LONG);
+ }if ("float[]".equals(name)) {
+ return heap.getPrimitiveClass(BasicType.FLOAT);
+ }if ("double[]".equals(name)) {
+ return heap.getPrimitiveClass(BasicType.DOUBLE);
+ }
+
+ String componentName = this.getName().substring(1);
+
+ /* If the array was "[Ljava/lang/String;"
+ * Strip it to "java/lang/String"
+ */
+ if(componentName.charAt(0) == 'L') {
+ componentName = componentName.substring(1,componentName.length()-1);
+ }
+
+ return getClassLoader().findClass(componentName);
+ }
+
+ @Override
+ public List getConstantPoolReferences() {
+ List cpeList = new LinkedList();
+
+
+ ConstantPoolEntry[] cpes = this.javaClass.getConstantPoolEntries();
+
+ for ( ConstantPoolEntry cpe : cpes) {
+ if (cpe.type == BasicType.OBJECT) {
+ // cpeList.add(heap.getObjectByID(cpe.data));
+ }
+ }
+ return null;
+ }
+
+ /**
+ * The static and instance fields belonging to this class.
+ *
+ */
+ private ArrayList<JavaField> fields;
+ @Override
+ public List getDeclaredFields() {
+ if (fields == null) {
+ StaticFieldEntry staticFields[] = javaClass.getStaticFields();
+ InstanceFieldEntry instanceFields[] = javaClass.getInstanceFields();
+
+ fields = new ArrayList<JavaField>(staticFields.length + instanceFields.length);
+
+ for (StaticFieldEntry f : staticFields) {
+ fields.add(new StaticFieldImpl(f));
+ }
+
+ for (InstanceFieldEntry f : instanceFields) {
+ fields.add(new InstanceFieldImpl(f));
+ }
+ }
+
+ return fields;
+ }
+
+ /**
+ * Method information isn't stored within classes.
+ *
+ */
+ @Override
+ public List getDeclaredMethods() {
+ return new LinkedList();
+ }
+
+
+ /**
+ * Wraps up object ID of class as it's ID.
+ * That should be sufficiently unique.
+ *
+ * @return ImagePointer
+ */
+ @Override
+ public ImagePointer getID() {
+ return heap.getImagePointer(javaClass.getClassObjectID());
+ }
+
+ /**
+ * Implemented interfaces aren't stored in Hprof.
+ * The constant pool could be used if the right entries could be targeted,
+ * but there is nothing to suggest that that is possible.
+ */
+ @Override
+ public List getInterfaces() {
+ return new LinkedList();
+ }
+
+ /**
+ * There is no modifier information to return
+ * @return 0
+ */
+ @Override
+ public int getModifiers() throws CorruptDataException {
+ return 0;
+ }
+
+ /**
+ * Returns the classes's name. This is stored in hprof with "." and "$" separators,
+ * the "."s are replaced by "/"s to conform with the API.
+ *
+ * @return String of the class's name.
+ */
+ @Override
+ public String getName() throws CorruptDataException {
+ String name = javaClass.getClassName();
+
+ /* Process the array's such that they are of the format:
+ * [Ljava/lang/Object;
+ *
+ * or
+ * [[B
+ *
+ * Primitive arrays are renamed correctly.
+ */
+ if(name.indexOf('[') != -1) {
+ int depth=0;
+ String prefix = "";
+ for(int n=0; n < name.length(); n++) {
+ if (name.charAt(n) == '[') {
+ prefix+="[";
+ }
+ }
+
+ // Chop off arrays.
+ name = name.substring(0, name.indexOf('['));
+
+ if (name.startsWith("boolean")) {
+ name = "Z";
+ } else if (name.startsWith("byte")) {
+ name = "B";
+ } else if (name.startsWith("char")) {
+ name = "C";
+ } else if (name.startsWith("short")) {
+ name = "S";
+ } else if (name.startsWith("int")) {
+ name = "I";
+ } else if (name.startsWith("long")) {
+ name = "J";
+ } else if (name.startsWith("float")) {
+ name = "F";
+ } else if (name.startsWith("double")) {
+ name = "D";
+ } else {
+ name = "L" + name.replace('.', '/') +";";
+ }
+ return prefix+name;
+ }
+ return name.replace('.', '/');
+ }
+
+ /**
+ * Returns null. There are no java/lang/Class objects referenced
+ * by the class records.
+ */
+ @Override
+ public JavaObject getObject() throws CorruptDataException {
+ return null;
+ }
+
+ /**
+ * Return the superclass of this class or null.
+ *
+ * @return JavaClassImpl or null
+ */
+ @Override
+ public JavaClassImpl getSuperclass() throws CorruptDataException {
+ long superClassObjId = javaClass.getSuperClassObjectID();
+
+ // This is an assumption, but it will be necessary.
+ if (superClassObjId == 0L) {
+ return null;
+ }
+
+ return heap.getJavaClassByID(superClassObjId);
+ }
+
+ @Override
+ public List getReferences() {
+ // TODO Auto-generated method stub
+ // All object references, constantpool entries, superclass, etc.
+ return null;
+ }
+
+ /**
+ * Returns true if this is an array.
+ *
+ * @return true if this class is an array type.
+ */
+ @Override
+ public boolean isArray() throws CorruptDataException {
+ return javaClass.getClassName().indexOf('[') != -1;
+ }
+
+ /**
+ * @return true if passed object is this JavaClass
+ */
+ public boolean equals(Object obj) {
+ if (obj == null) {
+ return false;
+ }
+
+ if (!(obj instanceof JavaClassImpl)) {
+ return false;
+ }
+
+ if (obj == this) {
+ return true;
+ }
+
+ JavaClassImpl clazz = (JavaClassImpl) obj;
+
+ return clazz.getObjectID() == getObjectID();
+ }
+
+ /**
+ * Gets the object ID that uniquely identifies this class on the heap.
+ *
+ * @return long object ID
+ */
+ long getObjectID() {
+ return javaClass.getClassObjectID();
+ }
+
+ public String toString() {
+ String name = "<UNSET CLASS NAME>";
+ try {
+ name = this.getName();
+ } catch (CorruptDataException e) {
+ // oh well.
+ }
+ return name+" @ "+ Long.toHexString(javaClass.getClassObjectID());
+ }
+
+ public int hashCode() {
+ return (int) (javaClass.getClassObjectID() % Integer.MAX_VALUE);
+ }
+
+ /**
+ * Return the size of object instances.
+ *
+ * @return int of the object size.
+ */
+ int getInstanceSize() {
+ return javaClass.getInstanceSize();
+ }
+
+ /**
+ * Returns the number of bytes the part of this class takes up in an instance object.
+ * This is the number of bytes taken up by the fields in this class and none of its
+ * superclasses.
+ * This is different from getInstanceSize(), which is the size of an instance of this
+ * class, including all superclasses.
+ *
+ * @return int fields size
+ */
+ int getOffsetSize() {
+ return javaClass.getOffsetSize();
+ }
+}
Propchange: incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaClassImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaClassLoaderImpl.java
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaClassLoaderImpl.java?rev=883384&r1=883383&r2=883384&view=diff
==============================================================================
--- incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaClassLoaderImpl.java (original)
+++ incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaClassLoaderImpl.java Mon Nov 23 15:53:48 2009
@@ -1,132 +1,132 @@
-/*******************************************************************************
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- ******************************************************************************/
-package org.apache.kato.hprof.java;
-
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-
-import javax.tools.diagnostics.image.CorruptDataException;
-import javax.tools.diagnostics.runtime.java.JavaClassLoader;
-import javax.tools.diagnostics.runtime.java.JavaObject;
-
-import org.apache.kato.common.IteratorBackedList;
-import org.apache.kato.hprof.IJavaClassLoader;
-import org.apache.kato.hprof.image.CorruptDataImpl;
-
-/**
- * Implements JavaClassLoader.
- *
- *
- */
-public class JavaClassLoaderImpl implements JavaClassLoader {
- IJavaClassLoader loader;
- IJavaHeapInternal heap;
-
- public JavaClassLoaderImpl(IJavaHeapInternal heap, IJavaClassLoader loader) {
- this.loader = loader;
- this.heap = heap;;
- }
-
- /**
- * Returns the JavaClassImpl that has the name passed.
- *
- * Iterates through all of the Class object IDs, and retrieves
- * the IJavaClasses held in HProfView, so as not to create JavaClasses
- * unnecessarily.
- */
- @Override
- public JavaClassImpl findClass(String className) throws CorruptDataException {
- if (className == null) {
- return null;
- } else if (className.length() == 0) {
- return null;
- }
-
- Collection<Long> classes = loader.getClasses();
-
- for (Long ID : classes) {
- JavaClassImpl clazz = heap.getJavaClassByID(ID);
- if (className.equals(clazz.getName())) {
- return clazz;
- }
- }
-
- return null;
- }
-
- @Override
- /**
- * Returns an empty Iterator, as the HProf format doesn't describe
- * how the classes are cached.
- *
- * FIXME It may be possible to pull out the classes from the java.lang.ClassLoader.classes Vector
- */
- public List getCachedClasses() {
- return new LinkedList();
- }
-
- @Override
- public List getDefinedClasses() {
- return new IteratorBackedList(new ClassesIterator());
- }
-
- /**
- * An iterator over the classes loaded by this class loader.
- * Wraps the IDs this class loader holds with JavaClassImpls.
- *
- * @author monteith
- *
- */
- private class ClassesIterator implements Iterator {
- private Collection<Long> classes = loader.getClasses();
- private Iterator<Long> iterator = classes.iterator();
- @Override
- public boolean hasNext() {
- return iterator.hasNext();
- }
-
- @Override
- public Object next() {
- Long ID = iterator.next();
- JavaClassImpl clazz = heap.getJavaClassByID(ID);
-
- if (clazz == null) {
- return new CorruptDataImpl(heap.getImagePointer(ID), "Can't match class object ID to a JavaClassImpl.");
- }
-
- return clazz;
- }
-
- /**
- * Not implemented.
- */
- @Override
- public void remove() {
- throw new UnsupportedOperationException("remove() not supported for ClassesIterator");
- }
-
- }
-
- /**
- * Gets the java/lang/ClassLoader object from the heap.
- *
- */
- @Override
- public JavaObject getObject() throws CorruptDataException {
- return heap.getObjectByID(this.loader.getID());
- }
-
-}
+/*******************************************************************************
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.kato.hprof.java;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import javax.tools.diagnostics.image.CorruptDataException;
+import javax.tools.diagnostics.runtime.java.JavaClassLoader;
+import javax.tools.diagnostics.runtime.java.JavaObject;
+
+import org.apache.kato.common.IteratorBackedList;
+import org.apache.kato.hprof.IJavaClassLoader;
+import org.apache.kato.hprof.image.CorruptDataImpl;
+
+/**
+ * Implements JavaClassLoader.
+ *
+ *
+ */
+public class JavaClassLoaderImpl implements JavaClassLoader {
+ IJavaClassLoader loader;
+ IJavaHeapInternal heap;
+
+ public JavaClassLoaderImpl(IJavaHeapInternal heap, IJavaClassLoader loader) {
+ this.loader = loader;
+ this.heap = heap;;
+ }
+
+ /**
+ * Returns the JavaClassImpl that has the name passed.
+ *
+ * Iterates through all of the Class object IDs, and retrieves
+ * the IJavaClasses held in HProfView, so as not to create JavaClasses
+ * unnecessarily.
+ */
+ @Override
+ public JavaClassImpl findClass(String className) throws CorruptDataException {
+ if (className == null) {
+ return null;
+ } else if (className.length() == 0) {
+ return null;
+ }
+
+ Collection<Long> classes = loader.getClasses();
+
+ for (Long ID : classes) {
+ JavaClassImpl clazz = heap.getJavaClassByID(ID);
+ if (className.equals(clazz.getName())) {
+ return clazz;
+ }
+ }
+
+ return null;
+ }
+
+ @Override
+ /**
+ * Returns an empty Iterator, as the HProf format doesn't describe
+ * how the classes are cached.
+ *
+ * FIXME It may be possible to pull out the classes from the java.lang.ClassLoader.classes Vector
+ */
+ public List getCachedClasses() {
+ return new LinkedList();
+ }
+
+ @Override
+ public List getDefinedClasses() {
+ return new IteratorBackedList(new ClassesIterator());
+ }
+
+ /**
+ * An iterator over the classes loaded by this class loader.
+ * Wraps the IDs this class loader holds with JavaClassImpls.
+ *
+ * @author monteith
+ *
+ */
+ private class ClassesIterator implements Iterator {
+ private Collection<Long> classes = loader.getClasses();
+ private Iterator<Long> iterator = classes.iterator();
+ @Override
+ public boolean hasNext() {
+ return iterator.hasNext();
+ }
+
+ @Override
+ public Object next() {
+ Long ID = iterator.next();
+ JavaClassImpl clazz = heap.getJavaClassByID(ID);
+
+ if (clazz == null) {
+ return new CorruptDataImpl(heap.getImagePointer(ID), "Can't match class object ID to a JavaClassImpl.");
+ }
+
+ return clazz;
+ }
+
+ /**
+ * Not implemented.
+ */
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException("remove() not supported for ClassesIterator");
+ }
+
+ }
+
+ /**
+ * Gets the java/lang/ClassLoader object from the heap.
+ *
+ */
+ @Override
+ public JavaObject getObject() throws CorruptDataException {
+ return heap.getObjectByID(this.loader.getID());
+ }
+
+}
Propchange: incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaClassLoaderImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaHeapImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaObjectArrayImpl.java
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaObjectArrayImpl.java?rev=883384&r1=883383&r2=883384&view=diff
==============================================================================
--- incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaObjectArrayImpl.java (original)
+++ incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaObjectArrayImpl.java Mon Nov 23 15:53:48 2009
@@ -1,168 +1,168 @@
-/*******************************************************************************
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- ******************************************************************************/
-package org.apache.kato.hprof.java;
-
-
-import java.util.LinkedList;
-import java.util.List;
-
-import javax.tools.diagnostics.image.CorruptDataException;
-import javax.tools.diagnostics.image.DataUnavailable;
-import javax.tools.diagnostics.image.ImagePointer;
-import javax.tools.diagnostics.image.MemoryAccessException;
-import javax.tools.diagnostics.runtime.java.JavaClass;
-import javax.tools.diagnostics.runtime.java.JavaHeap;
-import javax.tools.diagnostics.runtime.java.JavaObject;
-
-import org.apache.kato.hprof.datalayer.IGCObjectArrayHeapDumpRecord;
-
-/**
- * An array of object references.
- *
- *
- *
- */
-public class JavaObjectArrayImpl implements JavaObject {
- private IJavaHeapInternal heap;
- private IGCObjectArrayHeapDumpRecord record;
-
- public JavaObjectArrayImpl(IJavaHeapInternal heap, IGCObjectArrayHeapDumpRecord record) {
- this.heap = heap;
- this.record = record;
- }
-
- /**
- * Retrieves object IDs from the record on the disk and fills in array
- * with JavaObjects by mapping from the IDs to JavaObjects.
- *
- */
- @Override
- public void arraycopy(int srcStart, Object dst, int dstStart, int length)
- throws CorruptDataException, MemoryAccessException {
- if (srcStart < 0 || srcStart > getArraySize()) {
- throw new IndexOutOfBoundsException("srcStart must be between 0 and " + getArraySize()+", not " + srcStart);
- }
-
- if (srcStart+length > getArraySize()) {
- throw new IndexOutOfBoundsException("srcStart+length must be <="+getArraySize()+", srcStart=" + srcStart+", length="+length);
- }
- if (dstStart < 0) {
- throw new IndexOutOfBoundsException("dstStart must be >=0, not " + dstStart);
- }
-
- if (dst == null) {
- throw new IllegalArgumentException("Destination array must not be null");
- }
- if (!dst.getClass().isArray()) {
- throw new IllegalArgumentException("Destination array must be an array, not "+dst.getClass().getName());
- }
- if(dst.getClass().getComponentType() != JavaObject.class) {
- throw new IllegalArgumentException("Destination array wrong type - must be JavaObject array");
- }
-
- JavaObject[] array = (JavaObject[]) dst;
- checkArrayBounds(dstStart, array.length, length);
-
- for (int i=0; i < length; i++) {
- array[dstStart+i] = heap.getObjectByID(record.getElement(srcStart+i));
- }
- }
-
- private static void checkArrayBounds(int arrayStart, int arrayLength, int dataLength) {
- if ( arrayStart + dataLength > arrayLength) {
- throw new IndexOutOfBoundsException("Destination array of length "+ arrayLength+" can't"
- + " accomodate array copy with start of "+arrayStart+" and length of "+ dataLength);
- }
- }
- @Override
- public int getArraySize() throws CorruptDataException {
- return record.getNumberOfElements();
- }
-
- @Override
- public long getHashcode() throws DataUnavailable, CorruptDataException {
- // this is probably a bad hash
- return record.getID();
- }
-
- @Override
- public JavaHeap getHeap() throws CorruptDataException, DataUnavailable {
- return heap;
- }
-
- @Override
- public ImagePointer getID() {
- return heap.getImagePointer(record.getID());
- }
-
- @Override
- public JavaClass getJavaClass() throws CorruptDataException {
- return heap.getJavaClassByID(record.getArrayClassObjectID());
- }
-
- @Override
- public long getPersistentHashcode() throws DataUnavailable,
- CorruptDataException {
- throw new DataUnavailable("Persistent hashcode unknown."); }
-
- @Override
- public List getReferences() {
- return new LinkedList();
- }
-
- @Override
- public List getSections() {
- return new LinkedList();
- }
-
- @Override
- public long getSize() throws CorruptDataException {
- return 0;
- }
-
- @Override
- public boolean isArray() throws CorruptDataException {
- return true;
- }
-
- /**
- * Compares identity. This object may be created multiple times.
- * Uses object ID for comparing identity.
- * We don't check runtime identity.
- */
- @Override
- public boolean equals(Object obj) {
- if (obj == null) {
- return false;
- }
-
- if (!(obj instanceof JavaObjectArrayImpl)) {
- JavaObjectArrayImpl other = (JavaObjectArrayImpl) obj;
-
- if (other.getObjectID() == record.getID()) {
- return true;
- }
- }
-
- return false;
- }
-
- /**
- * Return object ID of record on disk.
- * @return
- */
- public long getObjectID() {
- return record.getID();
- }
-}
+/*******************************************************************************
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.kato.hprof.java;
+
+
+import java.util.LinkedList;
+import java.util.List;
+
+import javax.tools.diagnostics.image.CorruptDataException;
+import javax.tools.diagnostics.image.DataUnavailable;
+import javax.tools.diagnostics.image.ImagePointer;
+import javax.tools.diagnostics.image.MemoryAccessException;
+import javax.tools.diagnostics.runtime.java.JavaClass;
+import javax.tools.diagnostics.runtime.java.JavaHeap;
+import javax.tools.diagnostics.runtime.java.JavaObject;
+
+import org.apache.kato.hprof.datalayer.IGCObjectArrayHeapDumpRecord;
+
+/**
+ * An array of object references.
+ *
+ *
+ *
+ */
+public class JavaObjectArrayImpl implements JavaObject {
+ private IJavaHeapInternal heap;
+ private IGCObjectArrayHeapDumpRecord record;
+
+ public JavaObjectArrayImpl(IJavaHeapInternal heap, IGCObjectArrayHeapDumpRecord record) {
+ this.heap = heap;
+ this.record = record;
+ }
+
+ /**
+ * Retrieves object IDs from the record on the disk and fills in array
+ * with JavaObjects by mapping from the IDs to JavaObjects.
+ *
+ */
+ @Override
+ public void arraycopy(int srcStart, Object dst, int dstStart, int length)
+ throws CorruptDataException, MemoryAccessException {
+ if (srcStart < 0 || srcStart > getArraySize()) {
+ throw new IndexOutOfBoundsException("srcStart must be between 0 and " + getArraySize()+", not " + srcStart);
+ }
+
+ if (srcStart+length > getArraySize()) {
+ throw new IndexOutOfBoundsException("srcStart+length must be <="+getArraySize()+", srcStart=" + srcStart+", length="+length);
+ }
+ if (dstStart < 0) {
+ throw new IndexOutOfBoundsException("dstStart must be >=0, not " + dstStart);
+ }
+
+ if (dst == null) {
+ throw new IllegalArgumentException("Destination array must not be null");
+ }
+ if (!dst.getClass().isArray()) {
+ throw new IllegalArgumentException("Destination array must be an array, not "+dst.getClass().getName());
+ }
+ if(dst.getClass().getComponentType() != JavaObject.class) {
+ throw new IllegalArgumentException("Destination array wrong type - must be JavaObject array");
+ }
+
+ JavaObject[] array = (JavaObject[]) dst;
+ checkArrayBounds(dstStart, array.length, length);
+
+ for (int i=0; i < length; i++) {
+ array[dstStart+i] = heap.getObjectByID(record.getElement(srcStart+i));
+ }
+ }
+
+ private static void checkArrayBounds(int arrayStart, int arrayLength, int dataLength) {
+ if ( arrayStart + dataLength > arrayLength) {
+ throw new IndexOutOfBoundsException("Destination array of length "+ arrayLength+" can't"
+ + " accomodate array copy with start of "+arrayStart+" and length of "+ dataLength);
+ }
+ }
+ @Override
+ public int getArraySize() throws CorruptDataException {
+ return record.getNumberOfElements();
+ }
+
+ @Override
+ public long getHashcode() throws DataUnavailable, CorruptDataException {
+ // this is probably a bad hash
+ return record.getID();
+ }
+
+ @Override
+ public JavaHeap getHeap() throws CorruptDataException, DataUnavailable {
+ return heap;
+ }
+
+ @Override
+ public ImagePointer getID() {
+ return heap.getImagePointer(record.getID());
+ }
+
+ @Override
+ public JavaClass getJavaClass() throws CorruptDataException {
+ return heap.getJavaClassByID(record.getArrayClassObjectID());
+ }
+
+ @Override
+ public long getPersistentHashcode() throws DataUnavailable,
+ CorruptDataException {
+ throw new DataUnavailable("Persistent hashcode unknown."); }
+
+ @Override
+ public List getReferences() {
+ return new LinkedList();
+ }
+
+ @Override
+ public List getSections() {
+ return new LinkedList();
+ }
+
+ @Override
+ public long getSize() throws CorruptDataException {
+ return 0;
+ }
+
+ @Override
+ public boolean isArray() throws CorruptDataException {
+ return true;
+ }
+
+ /**
+ * Compares identity. This object may be created multiple times.
+ * Uses object ID for comparing identity.
+ * We don't check runtime identity.
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == null) {
+ return false;
+ }
+
+ if (!(obj instanceof JavaObjectArrayImpl)) {
+ JavaObjectArrayImpl other = (JavaObjectArrayImpl) obj;
+
+ if (other.getObjectID() == record.getID()) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Return object ID of record on disk.
+ * @return
+ */
+ public long getObjectID() {
+ return record.getID();
+ }
+}
Propchange: incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaObjectArrayImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaObjectInstanceImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaPrimitiveArrayImpl.java
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaPrimitiveArrayImpl.java?rev=883384&r1=883383&r2=883384&view=diff
==============================================================================
--- incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaPrimitiveArrayImpl.java (original)
+++ incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaPrimitiveArrayImpl.java Mon Nov 23 15:53:48 2009
@@ -1,258 +1,258 @@
-/*******************************************************************************
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- ******************************************************************************/
-package org.apache.kato.hprof.java;
-
-
-import java.util.LinkedList;
-import java.util.List;
-
-import javax.tools.diagnostics.image.CorruptDataException;
-import javax.tools.diagnostics.image.DataUnavailable;
-import javax.tools.diagnostics.image.ImagePointer;
-import javax.tools.diagnostics.image.MemoryAccessException;
-import javax.tools.diagnostics.runtime.java.JavaClass;
-import javax.tools.diagnostics.runtime.java.JavaHeap;
-import javax.tools.diagnostics.runtime.java.JavaObject;
-
-import org.apache.kato.common.BasicType;
-import org.apache.kato.hprof.datalayer.IGCPrimitiveArrayHeapDumpRecord;
-import org.apache.kato.hprof.image.CorruptDataImpl;
-
-public class JavaPrimitiveArrayImpl implements JavaObject {
- private IJavaHeapInternal heap;
- private IGCPrimitiveArrayHeapDumpRecord record;
-
- public JavaPrimitiveArrayImpl(IJavaHeapInternal heap, IGCPrimitiveArrayHeapDumpRecord record) {
- this.heap = heap;
- this.record = record;
- }
- @Override
- public void arraycopy(int srcStart, Object dst, int dstStart, int length)
- throws CorruptDataException, MemoryAccessException {
- if (srcStart < 0 || srcStart > getArraySize()) {
- throw new IndexOutOfBoundsException("srcStart must be between 0 and " + getArraySize()+", not " + srcStart);
- }
-
- if (srcStart+length > getArraySize()) {
- throw new IndexOutOfBoundsException("srcStart+length must be <="+getArraySize()+", srcStart=" + srcStart+", length="+length);
- }
- if (dstStart < 0) {
- throw new IndexOutOfBoundsException("dstStart must be >=0, not " + dstStart);
- }
-
- if (dst == null) {
- throw new IllegalArgumentException("Destination array must not be null");
- }
- if (!dst.getClass().isArray()) {
- throw new IllegalArgumentException("Destination array must be an array, not "+dst.getClass().getName());
- }
-
-
- switch(record.getElementType()) {
- case BasicType.BOOLEAN:
- {
- if(dst.getClass().getComponentType().getName() != "boolean") {
- throw new IllegalArgumentException("Destination array wrong type - must be boolean");
- }
- boolean[] array = (boolean[]) dst;
- checkArrayBounds(dstStart, array.length, length);
-
- for (int i=0; i < length; i++) {
- array[dstStart+i] = record.getBooleanElement(srcStart+i);
- }
- break;
- }
- case BasicType.BYTE:
- {
- if(dst.getClass().getComponentType().getName() != "byte") {
- throw new IllegalArgumentException("Destination array wrong type - must be byte");
- }
- byte[] array = (byte[]) dst;
- checkArrayBounds(dstStart, array.length, length);
-
- for (int i=0; i < length; i++) {
- array[dstStart+i] = (byte) record.getByteElement(srcStart+i);
- }
- break;
- }
- case BasicType.SHORT:
- {
- if(dst.getClass().getComponentType().getName() != "short") {
- throw new IllegalArgumentException("Destination array wrong type - must be short");
- }
- short[] array = (short[]) dst;
- checkArrayBounds(dstStart, array.length, length);
-
- for (int i=0; i < length; i++) {
- array[dstStart+i] = record.getShortElement(srcStart+i);
- }
- break;
- }
- case BasicType.CHAR:
- {
- if(dst.getClass().getComponentType().getName() != "char") {
- throw new IllegalArgumentException("Destination array wrong type - must be char");
- }
- char[] array = (char[]) dst;
- checkArrayBounds(dstStart, array.length, length);
-
- for (int i=0; i < length; i++) {
- array[dstStart+i] = record.getCharElement(srcStart+i);
- }
- break;
- }
- case BasicType.INT:
- {
- if(dst.getClass().getComponentType().getName() != "int") {
- throw new IllegalArgumentException("Destination array wrong type - must be int");
- }
- int[] array = (int[]) dst;
- checkArrayBounds(dstStart, array.length, length);
-
- for (int i=0; i < length; i++) {
- array[dstStart+i] = record.getIntElement(srcStart+i);
- }
- break;
- }
- case BasicType.LONG:
- {
- if(dst.getClass().getComponentType().getName() != "long") {
- throw new IllegalArgumentException("Destination array wrong type - must be long");
- }
- long[] array = (long[]) dst;
- checkArrayBounds(dstStart, array.length, length);
-
- for (int i=0; i < length; i++) {
- array[dstStart+i] = record.getLongElement(srcStart+i);
- }
- break;
- }
- case BasicType.FLOAT:
- {
- if(dst.getClass().getComponentType().getName() != "float") {
- throw new IllegalArgumentException("Destination array wrong type - must be float");
- }
- float[] array = (float[]) dst;
- checkArrayBounds(dstStart, array.length, length);
-
- for (int i=0; i < length; i++) {
- array[dstStart+i] = record.getFloatElement(srcStart+i);
- }
- break;
- }
- case BasicType.DOUBLE:
- {
- if(dst.getClass().getComponentType().getName() != "double") {
- throw new IllegalArgumentException("Destination array wrong type - must be double");
- }
- double[] array = (double[]) dst;
- checkArrayBounds(dstStart, array.length, length);
-
- for (int i=0; i < length; i++) {
- array[dstStart+i] = record.getDoubleElement(srcStart+i);
- }
- break;
- }
- default:
- throw new CorruptDataException(new CorruptDataImpl("Primitive array has invalid type of "+record.getElementType()));
- }
- }
-
- private static void checkArrayBounds(int arrayStart, int arrayLength, int dataLength) {
- if ( arrayStart + dataLength > arrayLength) {
- throw new IndexOutOfBoundsException("Destination array of length "+ arrayLength+" can't"
- + " accomodate array copy with start of "+arrayStart+" and length of "+ dataLength);
- }
- }
- @Override
- public int getArraySize() throws CorruptDataException {
- return record.getNumberOfElements();
- }
-
- @Override
- public long getHashcode() throws DataUnavailable, CorruptDataException {
- // TODO Auto-generated method stub
- return 0;
- }
-
- @Override
- public JavaHeap getHeap() throws CorruptDataException, DataUnavailable {
- return heap;
- }
-
- @Override
- public ImagePointer getID() {
- return heap.getImagePointer(record.getID());
- }
-
- @Override
- public JavaClass getJavaClass() throws CorruptDataException {
- return heap.getPrimitiveArrayClass(record.getElementType());
- }
-
- @Override
- public long getPersistentHashcode() throws DataUnavailable,
- CorruptDataException {
- throw new DataUnavailable("Persistent hashcode unknown."); }
-
- @Override
- public List getReferences() {
- return new LinkedList();
- }
-
- @Override
- public List getSections() {
- return new LinkedList();
- }
-
- @Override
- public long getSize() throws CorruptDataException {
- return 0;
- }
-
- @Override
- public boolean isArray() throws CorruptDataException {
- return true;
- }
-
- /**
- * Compares identity. This object may be created multiple times.
- * Uses object ID for comparing identity.
- * We don't check runtime identity.
- */
- @Override
- public boolean equals(Object obj) {
- if (obj == null) {
- return false;
- }
-
- if (!(obj instanceof JavaPrimitiveArrayImpl)) {
- JavaPrimitiveArrayImpl other = (JavaPrimitiveArrayImpl) obj;
-
- if (other.getObjectID() == record.getID()) {
- return true;
- }
- }
-
- return false;
- }
-
- /**
- * Return object ID of record on disk.
- * @return
- */
- long getObjectID() {
- return record.getID();
- }
-}
+/*******************************************************************************
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.kato.hprof.java;
+
+
+import java.util.LinkedList;
+import java.util.List;
+
+import javax.tools.diagnostics.image.CorruptDataException;
+import javax.tools.diagnostics.image.DataUnavailable;
+import javax.tools.diagnostics.image.ImagePointer;
+import javax.tools.diagnostics.image.MemoryAccessException;
+import javax.tools.diagnostics.runtime.java.JavaClass;
+import javax.tools.diagnostics.runtime.java.JavaHeap;
+import javax.tools.diagnostics.runtime.java.JavaObject;
+
+import org.apache.kato.common.BasicType;
+import org.apache.kato.hprof.datalayer.IGCPrimitiveArrayHeapDumpRecord;
+import org.apache.kato.hprof.image.CorruptDataImpl;
+
+public class JavaPrimitiveArrayImpl implements JavaObject {
+ private IJavaHeapInternal heap;
+ private IGCPrimitiveArrayHeapDumpRecord record;
+
+ public JavaPrimitiveArrayImpl(IJavaHeapInternal heap, IGCPrimitiveArrayHeapDumpRecord record) {
+ this.heap = heap;
+ this.record = record;
+ }
+ @Override
+ public void arraycopy(int srcStart, Object dst, int dstStart, int length)
+ throws CorruptDataException, MemoryAccessException {
+ if (srcStart < 0 || srcStart > getArraySize()) {
+ throw new IndexOutOfBoundsException("srcStart must be between 0 and " + getArraySize()+", not " + srcStart);
+ }
+
+ if (srcStart+length > getArraySize()) {
+ throw new IndexOutOfBoundsException("srcStart+length must be <="+getArraySize()+", srcStart=" + srcStart+", length="+length);
+ }
+ if (dstStart < 0) {
+ throw new IndexOutOfBoundsException("dstStart must be >=0, not " + dstStart);
+ }
+
+ if (dst == null) {
+ throw new IllegalArgumentException("Destination array must not be null");
+ }
+ if (!dst.getClass().isArray()) {
+ throw new IllegalArgumentException("Destination array must be an array, not "+dst.getClass().getName());
+ }
+
+
+ switch(record.getElementType()) {
+ case BasicType.BOOLEAN:
+ {
+ if(dst.getClass().getComponentType().getName() != "boolean") {
+ throw new IllegalArgumentException("Destination array wrong type - must be boolean");
+ }
+ boolean[] array = (boolean[]) dst;
+ checkArrayBounds(dstStart, array.length, length);
+
+ for (int i=0; i < length; i++) {
+ array[dstStart+i] = record.getBooleanElement(srcStart+i);
+ }
+ break;
+ }
+ case BasicType.BYTE:
+ {
+ if(dst.getClass().getComponentType().getName() != "byte") {
+ throw new IllegalArgumentException("Destination array wrong type - must be byte");
+ }
+ byte[] array = (byte[]) dst;
+ checkArrayBounds(dstStart, array.length, length);
+
+ for (int i=0; i < length; i++) {
+ array[dstStart+i] = (byte) record.getByteElement(srcStart+i);
+ }
+ break;
+ }
+ case BasicType.SHORT:
+ {
+ if(dst.getClass().getComponentType().getName() != "short") {
+ throw new IllegalArgumentException("Destination array wrong type - must be short");
+ }
+ short[] array = (short[]) dst;
+ checkArrayBounds(dstStart, array.length, length);
+
+ for (int i=0; i < length; i++) {
+ array[dstStart+i] = record.getShortElement(srcStart+i);
+ }
+ break;
+ }
+ case BasicType.CHAR:
+ {
+ if(dst.getClass().getComponentType().getName() != "char") {
+ throw new IllegalArgumentException("Destination array wrong type - must be char");
+ }
+ char[] array = (char[]) dst;
+ checkArrayBounds(dstStart, array.length, length);
+
+ for (int i=0; i < length; i++) {
+ array[dstStart+i] = record.getCharElement(srcStart+i);
+ }
+ break;
+ }
+ case BasicType.INT:
+ {
+ if(dst.getClass().getComponentType().getName() != "int") {
+ throw new IllegalArgumentException("Destination array wrong type - must be int");
+ }
+ int[] array = (int[]) dst;
+ checkArrayBounds(dstStart, array.length, length);
+
+ for (int i=0; i < length; i++) {
+ array[dstStart+i] = record.getIntElement(srcStart+i);
+ }
+ break;
+ }
+ case BasicType.LONG:
+ {
+ if(dst.getClass().getComponentType().getName() != "long") {
+ throw new IllegalArgumentException("Destination array wrong type - must be long");
+ }
+ long[] array = (long[]) dst;
+ checkArrayBounds(dstStart, array.length, length);
+
+ for (int i=0; i < length; i++) {
+ array[dstStart+i] = record.getLongElement(srcStart+i);
+ }
+ break;
+ }
+ case BasicType.FLOAT:
+ {
+ if(dst.getClass().getComponentType().getName() != "float") {
+ throw new IllegalArgumentException("Destination array wrong type - must be float");
+ }
+ float[] array = (float[]) dst;
+ checkArrayBounds(dstStart, array.length, length);
+
+ for (int i=0; i < length; i++) {
+ array[dstStart+i] = record.getFloatElement(srcStart+i);
+ }
+ break;
+ }
+ case BasicType.DOUBLE:
+ {
+ if(dst.getClass().getComponentType().getName() != "double") {
+ throw new IllegalArgumentException("Destination array wrong type - must be double");
+ }
+ double[] array = (double[]) dst;
+ checkArrayBounds(dstStart, array.length, length);
+
+ for (int i=0; i < length; i++) {
+ array[dstStart+i] = record.getDoubleElement(srcStart+i);
+ }
+ break;
+ }
+ default:
+ throw new CorruptDataException(new CorruptDataImpl("Primitive array has invalid type of "+record.getElementType()));
+ }
+ }
+
+ private static void checkArrayBounds(int arrayStart, int arrayLength, int dataLength) {
+ if ( arrayStart + dataLength > arrayLength) {
+ throw new IndexOutOfBoundsException("Destination array of length "+ arrayLength+" can't"
+ + " accomodate array copy with start of "+arrayStart+" and length of "+ dataLength);
+ }
+ }
+ @Override
+ public int getArraySize() throws CorruptDataException {
+ return record.getNumberOfElements();
+ }
+
+ @Override
+ public long getHashcode() throws DataUnavailable, CorruptDataException {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ @Override
+ public JavaHeap getHeap() throws CorruptDataException, DataUnavailable {
+ return heap;
+ }
+
+ @Override
+ public ImagePointer getID() {
+ return heap.getImagePointer(record.getID());
+ }
+
+ @Override
+ public JavaClass getJavaClass() throws CorruptDataException {
+ return heap.getPrimitiveArrayClass(record.getElementType());
+ }
+
+ @Override
+ public long getPersistentHashcode() throws DataUnavailable,
+ CorruptDataException {
+ throw new DataUnavailable("Persistent hashcode unknown."); }
+
+ @Override
+ public List getReferences() {
+ return new LinkedList();
+ }
+
+ @Override
+ public List getSections() {
+ return new LinkedList();
+ }
+
+ @Override
+ public long getSize() throws CorruptDataException {
+ return 0;
+ }
+
+ @Override
+ public boolean isArray() throws CorruptDataException {
+ return true;
+ }
+
+ /**
+ * Compares identity. This object may be created multiple times.
+ * Uses object ID for comparing identity.
+ * We don't check runtime identity.
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == null) {
+ return false;
+ }
+
+ if (!(obj instanceof JavaPrimitiveArrayImpl)) {
+ JavaPrimitiveArrayImpl other = (JavaPrimitiveArrayImpl) obj;
+
+ if (other.getObjectID() == record.getID()) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Return object ID of record on disk.
+ * @return
+ */
+ long getObjectID() {
+ return record.getID();
+ }
+}
Propchange: incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaPrimitiveArrayImpl.java
------------------------------------------------------------------------------
svn:eol-style = native