incubator-kato-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From monte...@apache.org
Subject svn commit: r891342 - /incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaClassImpl.java
Date Wed, 16 Dec 2009 17:35:36 GMT
Author: monteith
Date: Wed Dec 16 17:35:35 2009
New Revision: 891342

URL: http://svn.apache.org/viewvc?rev=891342&view=rev
Log:
Fix field type comparisons.

Modified:
    incubator/kato/trunk/org.apache.kato/kato.hprof.api/src/main/java/org/apache/kato/hprof/java/JavaClassImpl.java

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=891342&r1=891341&r2=891342&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
Wed Dec 16 17:35:35 2009
@@ -133,6 +133,96 @@
 			
 			return new String(characters);
 		}
+		
+		/**
+		 * Checks the compatibility of this field with a return type.
+		 * Only call from a specific type. 
+		 * Will always throw an exception if called against an object
+		 * reference field.
+		 * 
+		 * @param type the type this field is to be returned as
+		 * @throws IllegalArgumentException if type is incompatible
+		 */
+		void checkType(int type) {
+			switch(field.getType()) {
+			case BasicType.OBJECT:
+				throw new IllegalArgumentException("objcet field can only be returned as a object.");
+			case BasicType.BOOLEAN:
+				if(type != BasicType.BOOLEAN) {
+					throw new IllegalArgumentException("boolean field can only be returned as a boolean.");
+				}
+				break;
+			case BasicType.BYTE:
+				if (type == BasicType.BOOLEAN) {
+					throw new IllegalArgumentException("byte field can not be returned as a boolean.");
+				}
+				if(type == BasicType.CHAR) {
+					throw new IllegalArgumentException("byte field can not be returned as a char.");
+				}
+				break;
+			case BasicType.SHORT:
+				switch (type) {				
+				case BasicType.BOOLEAN:				
+				case BasicType.BYTE:
+				case BasicType.CHAR:	
+					throw new IllegalArgumentException("short field can not be returned as a smaller type.");
+				}
+				break;
+			case BasicType.CHAR:
+				switch (type) {
+				case BasicType.BOOLEAN:				
+				case BasicType.BYTE:
+				case BasicType.SHORT:
+					throw new IllegalArgumentException("char field can not be returned as a smaller type.");
+				}
+				break;
+			case BasicType.INT:
+				switch (type) {
+				case BasicType.BOOLEAN:				
+				case BasicType.BYTE:
+				case BasicType.SHORT:
+				case BasicType.CHAR:
+					throw new IllegalArgumentException("int field can not be returned as a smaller type.");
+				}
+				break;
+				
+			case BasicType.LONG:
+				switch (type) {
+				case BasicType.BOOLEAN:				
+				case BasicType.BYTE:
+				case BasicType.SHORT:
+				case BasicType.CHAR:
+				case BasicType.INT:
+					throw new IllegalArgumentException("long field can not be returned as a smaller type.");
+				}
+				break;
+			case BasicType.FLOAT:
+				switch (type) {
+				case BasicType.BOOLEAN:				
+				case BasicType.BYTE:
+				case BasicType.SHORT:
+				case BasicType.CHAR:
+				case BasicType.INT:
+				case BasicType.LONG:
+					throw new IllegalArgumentException("float field can not be returned as a smaller type.");
+				}
+				break;
+				
+			case BasicType.DOUBLE:
+				switch (type) {
+				case BasicType.BOOLEAN:				
+				case BasicType.BYTE:
+				case BasicType.SHORT:
+				case BasicType.CHAR:
+				case BasicType.INT:
+				case BasicType.LONG:
+				case BasicType.FLOAT:
+					throw new IllegalArgumentException("double field can not be returned as a smaller type.");
+				}
+				break;
+			}
+				
+		}
 	}
 	
 	class InstanceFieldImpl extends JavaFieldImpl {
@@ -173,7 +263,7 @@
 		 */
 		private int getOffset(JavaClassImpl clazz) throws CorruptDataException {
 			// The potential subclasses of this field have to be skipped before
-			// using the stored field offset.			
+			// using the stored field offset.		
 			int offset = ((InstanceFieldEntry)field).getOffset();			
 			while ((clazz != null) && !clazz.equals(JavaClassImpl.this)) {
 				offset += clazz.getOffsetSize(); // Or some variant.
@@ -186,6 +276,9 @@
 		public Object get(JavaObject obj) throws CorruptDataException,
 				MemoryAccessException {
 			
+			if (obj == null) {
+				throw new NullPointerException("Instance JavaField passed null object.");
+			}
 			if (! (obj instanceof JavaObjectInstanceImpl)) {
 				throw new IllegalArgumentException("Passed object must be an ordinary object");			
 			}
@@ -233,6 +326,7 @@
 		@Override
 		public boolean getBoolean(JavaObject obj) throws CorruptDataException,
 				MemoryAccessException {
+			checkType(BasicType.BOOLEAN);
 			JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
 			JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
 			int offset = getOffset(clazz);
@@ -243,6 +337,8 @@
 		@Override
 		public byte getByte(JavaObject obj) throws CorruptDataException,
 				MemoryAccessException {
+			checkType(BasicType.BYTE);
+			
 			JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
 			JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
 			int offset = getOffset(clazz);
@@ -253,6 +349,7 @@
 		@Override
 		public char getChar(JavaObject obj) throws CorruptDataException,
 				MemoryAccessException {
+			checkType(BasicType.CHAR);
 			JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
 			JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
 			int offset = getOffset(clazz);
@@ -263,6 +360,7 @@
 		@Override
 		public double getDouble(JavaObject obj) throws CorruptDataException,
 				MemoryAccessException {
+			checkType(BasicType.DOUBLE);
 			JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
 			JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
 			int offset = getOffset(clazz);
@@ -273,6 +371,7 @@
 		@Override
 		public float getFloat(JavaObject obj) throws CorruptDataException,
 				MemoryAccessException {
+			checkType(BasicType.FLOAT);
 			JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
 			JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
 			int offset = getOffset(clazz);
@@ -284,6 +383,7 @@
 		@Override
 		public int getInt(JavaObject obj) throws CorruptDataException,
 				MemoryAccessException {
+			checkType(BasicType.INT);
 			JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
 			JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
 			int offset = getOffset(clazz);
@@ -295,6 +395,7 @@
 		@Override
 		public long getLong(JavaObject obj) throws CorruptDataException,
 				MemoryAccessException {
+			checkType(BasicType.LONG);
 			JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
 			JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
 			int offset = getOffset(clazz);
@@ -306,6 +407,7 @@
 		@Override
 		public short getShort(JavaObject obj) throws CorruptDataException,
 				MemoryAccessException {
+			checkType(BasicType.SHORT);
 			JavaObjectInstanceImpl instance = ((JavaObjectInstanceImpl) obj);
 			JavaClassImpl clazz = (JavaClassImpl) obj.getJavaClass();
 			int offset = getOffset(clazz);
@@ -390,48 +492,60 @@
 		@Override
 		public boolean getBoolean(JavaObject arg0) throws CorruptDataException,
 				MemoryAccessException {
+			checkType(BasicType.BOOLEAN);
+
 			return ((StaticFieldEntry)field).getBooleanField();
 		}
 
 		@Override
 		public byte getByte(JavaObject arg0) throws CorruptDataException,
 				MemoryAccessException {
+			checkType(BasicType.BYTE);
 			return ((StaticFieldEntry)field).getByteField();
 		}
 
 		@Override
 		public char getChar(JavaObject arg0) throws CorruptDataException,
 				MemoryAccessException {
+			checkType(BasicType.CHAR);
 			return ((StaticFieldEntry)field).getCharField();
 		}
 
 		@Override
 		public double getDouble(JavaObject arg0) throws CorruptDataException,
 				MemoryAccessException {
+			checkType(BasicType.DOUBLE);
 			return ((StaticFieldEntry)field).getDoubleField();
 		}
 
 		@Override
 		public float getFloat(JavaObject arg0) throws CorruptDataException,
 				MemoryAccessException {
+			checkType(BasicType.FLOAT);
 			return ((StaticFieldEntry)field).getFloatField();
 		}
 
 		@Override
 		public int getInt(JavaObject arg0) throws CorruptDataException,
 				MemoryAccessException {
+			checkType(BasicType.INT);
+
 			return ((StaticFieldEntry)field).getIntegerField();
 		}
 
 		@Override
 		public long getLong(JavaObject arg0) throws CorruptDataException,
 				MemoryAccessException {
+			checkType(BasicType.LONG);
+
 			return ((StaticFieldEntry)field).getLongField();
 		}
 
 		@Override
 		public short getShort(JavaObject arg0) throws CorruptDataException,
 				MemoryAccessException {
+			checkType(BasicType.SHORT);
+
 			return ((StaticFieldEntry)field).getShortField();
 		}
 



Mime
View raw message