incubator-kato-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From monte...@apache.org
Subject svn commit: r887212 - /incubator/kato/trunk/org.apache.kato/kato.cjvmti/src/main/java/org/apache/kato/jvmti/javaruntime/model/JField.java
Date Fri, 04 Dec 2009 14:38:49 GMT
Author: monteith
Date: Fri Dec  4 14:38:49 2009
New Revision: 887212

URL: http://svn.apache.org/viewvc?rev=887212&view=rev
Log:
Fix JavaField.getString() implementation.

Modified:
    incubator/kato/trunk/org.apache.kato/kato.cjvmti/src/main/java/org/apache/kato/jvmti/javaruntime/model/JField.java

Modified: incubator/kato/trunk/org.apache.kato/kato.cjvmti/src/main/java/org/apache/kato/jvmti/javaruntime/model/JField.java
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato/kato.cjvmti/src/main/java/org/apache/kato/jvmti/javaruntime/model/JField.java?rev=887212&r1=887211&r2=887212&view=diff
==============================================================================
--- incubator/kato/trunk/org.apache.kato/kato.cjvmti/src/main/java/org/apache/kato/jvmti/javaruntime/model/JField.java
(original)
+++ incubator/kato/trunk/org.apache.kato/kato.cjvmti/src/main/java/org/apache/kato/jvmti/javaruntime/model/JField.java
Fri Dec  4 14:38:49 2009
@@ -23,6 +23,7 @@
 import javax.tools.diagnostics.runtime.java.JavaField;
 import javax.tools.diagnostics.runtime.java.JavaObject;
 
+import org.apache.kato.jvmti.process.CorruptDataImpl;
 import org.apache.kato.jvmti.reader.CLogger;
 
 public class JField implements JavaField, JValueHolder {
@@ -161,64 +162,76 @@
 	public String getString(JavaObject arg0) throws CorruptDataException,
 			MemoryAccessException {
 		// Manually look for the string class
+				
 		
-		// First check static value
-		if (staticValue != null) {
-			JObject stringObj = (JObject) staticValue;
-			JavaClass stringClass = stringObj.getJavaClass();
-			while (!stringClass.getName().equals("java/lang/String")) {
-				if ((stringClass = stringClass.getSuperclass()) == null) {
-					break;
-				}
-			}
+		if (!staticField && (arg0 == null) ) {
+			throw new NullPointerException();
+		}
+		
+		Object obj;
+		if(staticField) {
+			obj = staticValue;
+		} else {
+			obj = ((JObject)arg0).getValue(id);
+		}
 
-			if (stringClass != null) {
-				for (JavaField field : stringObj.getJavaClass()
-						.getDeclaredFields()) {
-					// Assume this is the correct field
-					if (field.getSignature().equals("[C")) {
-						Character[] stringField = (Character[])((JObject)field.get(stringObj)).objArray;
-						char[] charArray = new char[stringField.length];
-						for (int i = 0; i < stringField.length; i++){
-							charArray[i] = stringField[i];
-						}
-						CLogger.logr.log(Level.FINEST,"From static field from "+field.getName()+" "+(new String(charArray)));
-						return new String(charArray);
-					}
-				}
-			}
+		// If the field value is null, return null.
+		if (obj == null) {
+			return null;
+		}
+		
+		// Otherwise make sure we are looking at a JObject
+		if(!(obj instanceof JObject)) {
+			throw new IllegalArgumentException("Passed object must be CJVMTI JObject instance");
 		}
 		
+		JObject stringObj = (JObject) obj;
 		
+		JavaClass stringClass = stringObj.getJavaClass();
 		
+		// java.lang.String is final.
+		if(!stringClass.getName().equals("java/lang/String")) {
+			throw new IllegalArgumentException("Field must be instance of java/lang/String");
+		}
 		
-		if (arg0 != null) {
-			JObject obj = (JObject)arg0;
-			JavaObject stringObj = (JavaObject)obj.getValue(id);
-			JavaClass stringClass = stringObj.getJavaClass();
-			while (!stringClass.getName().equals("java/lang/String")) {
-				if ((stringClass = stringClass.getSuperclass()) == null) {
-					break;
-				}
+
+		JavaField valueField=null, countField=null, offsetField=null;
+		for (JavaField field : stringObj.getJavaClass()
+				.getDeclaredFields()) {
+			// Assume this is the correct field
+			String name = field.getName();
+			if (name.equals("value")) {
+				valueField = field;
+			} else if (name.equals("count")) {
+				countField = field;
+			} else if (name.equals("offset")) {
+				offsetField = field;
 			}
 			
-			if (stringClass != null) {
-				for (JavaField field : stringObj.getJavaClass()
-						.getDeclaredFields()) {
-					// Assume this is the correct field
-					if (field.getSignature().equals("[C")) {
-						Character[] stringField = (Character[])((JObject)field.get(stringObj)).objArray;
-						char[] charArray = new char[stringField.length];
-						for (int i = 0; i < stringField.length; i++){
-							charArray[i] = stringField[i];
-						}
-						CLogger.logr.log(Level.FINEST,"From field "+field.getName()+" "+ (new String(charArray)));
-						return new String(charArray);
-					}
-				}
+			// Escape once we have the fields we need.
+			if (valueField !=null && countField != null && offsetField != null) {
+				break;
 			}
 		}
-		return "testTest";
+		
+		if (valueField == null ||  countField == null || offsetField == null) {
+			throw new CorruptDataException(new CorruptDataImpl("Unable to find relevant string fields"));
+		}
+
+		
+		Character[] stringField = (Character[])((JObject)valueField.get(stringObj)).objArray;
+		int offset = offsetField.getInt(stringObj);
+		int count = countField.getInt(stringObj);
+		char[] charArray = new char[count];
+		for (int i = 0; i < count; i++){
+			charArray[i] = stringField[offset+i];
+		}
+		
+		String string = new String(charArray);
+		
+		CLogger.logr.log(Level.FINEST,"From field from "+this.getName()+" "+string);
+		
+		return string;
 	}
 
 	@Override



Mime
View raw message