harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sjanu...@apache.org
Subject svn commit: r634338 - in /harmony/enhanced/classlib/trunk/modules/pack200/src: main/java/org/apache/harmony/pack200/SegmentConstantPool.java test/java/org/apache/harmony/pack200/tests/SegmentConstantPoolTest.java
Date Thu, 06 Mar 2008 16:38:04 GMT
Author: sjanuary
Date: Thu Mar  6 08:38:00 2008
New Revision: 634338

URL: http://svn.apache.org/viewvc?rev=634338&view=rev
Log:
Apply patch for HARMONY-5578 ([classlib][pack200] Remove regex dependency of Pack200)

Modified:
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/SegmentConstantPool.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/SegmentConstantPoolTest.java

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/SegmentConstantPool.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/SegmentConstantPool.java?rev=634338&r1=634337&r2=634338&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/SegmentConstantPool.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/SegmentConstantPool.java
Thu Mar  6 08:38:00 2008
@@ -53,7 +53,10 @@
     public static final int CP_METHOD = 11;
     public static final int CP_IMETHOD = 12;
 
-    // TODO: All CP***??
+    protected static final String REGEX_MATCH_ALL = ".*";
+    protected static final String INITSTRING = "<init>";
+    protected static final String REGEX_MATCH_INIT = "^" + INITSTRING + ".*";
+
     public Object getValue(int cp, long value) throws Pack200Exception {
         int index = (int) value;
         if (index == -1) {
@@ -79,8 +82,7 @@
         } else if (cp == CP_DESCR) {
             return bands.getCpDescriptor()[index];
         } else {
-            // etc
-            throw new Error("Get value incomplete");
+            throw new Error("Tried to get a value I don't know about: " + cp);
         }
     }
 
@@ -144,7 +146,7 @@
      */
     public ConstantPoolEntry getInitMethodPoolEntry(int cp, long value, String desiredClassName)
throws Pack200Exception {
     	int realIndex = -1;
-    	String desiredRegex = "^<init>.*";
+    	String desiredRegex = REGEX_MATCH_INIT;
     	if (cp == CP_METHOD) {
     		realIndex = matchSpecificPoolEntryIndex(bands.getCpMethodClass(), bands.getCpMethodDescriptor(),
desiredClassName, desiredRegex, (int)value);
     	} else {
@@ -182,7 +184,7 @@
      * @return int index into nameArray, or -1 if not found.
      */
     protected int matchSpecificPoolEntryIndex(String[] nameArray, String compareString, int
desiredIndex) {
-    	return matchSpecificPoolEntryIndex(nameArray, nameArray, compareString, ".*", desiredIndex);
+    	return matchSpecificPoolEntryIndex(nameArray, nameArray, compareString, REGEX_MATCH_ALL,
desiredIndex);
     }
 
     /**
@@ -205,7 +207,7 @@
     	int instanceCount = -1;
     	for(int index=0; index < primaryArray.length; index++) {
     		if((primaryArray[index].equals(primaryCompareString)) &&
-    				secondaryArray[index].matches(secondaryCompareRegex)) {
+    				regexMatches(secondaryCompareRegex, secondaryArray[index]) ) {
     			instanceCount++;
     			if(instanceCount == desiredIndex) {
     				return index;
@@ -217,6 +219,33 @@
     	return -1;
     }
 
+    /**
+     * We don't want a dependency on regex in Pack200. The
+     * only place one exists is in matchSpecificPoolEntryIndex().
+     * To eliminate this dependency, we've implemented the
+     * world's stupidest regexMatch. It knows about the two
+     * forms we care about: 
+     *  .* (aka REGEX_MATCH_ALL)
+     *  ^<init>.* (aka REGEX_MATCH_INIT)
+     * and will answer correctly if those are passed as the
+     * regexString.
+     * @param regexString String against which the compareString will be matched
+     * @param compareString String to match against the regexString
+     * @return boolean true if the compareString matches the regexString;
+     *  otherwise false.
+     */
+    protected static boolean regexMatches(String regexString, String compareString) {
+        if(REGEX_MATCH_ALL.equals(regexString)) {
+            return true;
+        }
+        if(REGEX_MATCH_INIT.equals(regexString)) {
+            if(compareString.length() < (INITSTRING.length())) {
+                return false;
+            }
+            return(INITSTRING.equals(compareString.substring(0, INITSTRING.length())));
+        }
+        throw new Error("regex trying to match a pattern I don't know: " + regexString);
+    }
 
     public ConstantPoolEntry getConstantPoolEntry(int cp, long value) throws Pack200Exception
{
         int index = (int) value;

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/SegmentConstantPoolTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/SegmentConstantPoolTest.java?rev=634338&r1=634337&r2=634338&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/SegmentConstantPoolTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/SegmentConstantPoolTest.java
Thu Mar  6 08:38:00 2008
@@ -39,6 +39,10 @@
         public int matchSpecificPoolEntryIndex(String[] classNameArray, String[] methodNameArray,
String desiredClassName, String desiredMethodRegex, int desiredIndex) {
         	return super.matchSpecificPoolEntryIndex(classNameArray, methodNameArray, desiredClassName,
desiredMethodRegex, desiredIndex);
         };
+        
+        public boolean regexMatchesVisible(String regexString, String compareString) {
+            return SegmentConstantPool.regexMatches(regexString, compareString);
+        }
     }
 
 	String[] testClassArray = {"Object", "Object" , "java/lang/String", "java/lang/String",
"Object", "Other" };
@@ -65,20 +69,27 @@
 	public void testMatchSpecificPoolEntryIndex_DoubleArray() throws Exception {
 		MockSegmentConstantPool mockInstance = new MockSegmentConstantPool();
 		// Elements should be found at the proper position.
-		assertEquals(0, mockInstance.matchSpecificPoolEntryIndex(testClassArray, testMethodArray,
"Object", "<init>.*", 0));
-		assertEquals(1, mockInstance.matchSpecificPoolEntryIndex(testClassArray, testMethodArray,
"Object", "clone.*", 0));
-		assertEquals(2, mockInstance.matchSpecificPoolEntryIndex(testClassArray, testMethodArray,
"java/lang/String", "equals.*", 0));
-		assertEquals(3, mockInstance.matchSpecificPoolEntryIndex(testClassArray, testMethodArray,
"java/lang/String", "<init>.*", 0));
-		assertEquals(4, mockInstance.matchSpecificPoolEntryIndex(testClassArray, testMethodArray,
"Object", "isNull.*", 0));
-		assertEquals(5, mockInstance.matchSpecificPoolEntryIndex(testClassArray, testMethodArray,
"Other", "Other", 0));
+		assertEquals(0, mockInstance.matchSpecificPoolEntryIndex(testClassArray, testMethodArray,
"Object", "^<init>.*", 0));
+		assertEquals(2, mockInstance.matchSpecificPoolEntryIndex(testClassArray, testMethodArray,
"java/lang/String", ".*", 0));
+		assertEquals(3, mockInstance.matchSpecificPoolEntryIndex(testClassArray, testMethodArray,
"java/lang/String", "^<init>.*", 0));
+		assertEquals(5, mockInstance.matchSpecificPoolEntryIndex(testClassArray, testMethodArray,
"Other", ".*", 0));
 
 		// Elements that don't exist shouldn't be found
-		assertEquals(-1, mockInstance.matchSpecificPoolEntryIndex(testClassArray, testMethodArray,
"NotThere", "NotThere", 0));
-		assertEquals(-1, mockInstance.matchSpecificPoolEntryIndex(testClassArray, testMethodArray,
"Object", "NotThere", 0));
+		assertEquals(-1, mockInstance.matchSpecificPoolEntryIndex(testClassArray, testMethodArray,
"NotThere", "^<init>.*", 0));
 
 		// Elements that exist but don't have the requisite number
 		// of hits shouldn't be found.
-		assertEquals(-1, mockInstance.matchSpecificPoolEntryIndex(testClassArray, testMethodArray,
"java/lang/String", "<init>.*", 1));
+		assertEquals(-1, mockInstance.matchSpecificPoolEntryIndex(testClassArray, testMethodArray,
"java/lang/String", "^<init>.*", 1));
 	}
 
+	public void testRegexReplacement() {
+	    MockSegmentConstantPool mockPool = new MockSegmentConstantPool();
+	    assertTrue(mockPool.regexMatchesVisible(".*", "anything"));
+        assertTrue(mockPool.regexMatchesVisible(".*", ""));
+        assertTrue(mockPool.regexMatchesVisible("^<init>.*", "<init>"));
+        assertTrue(mockPool.regexMatchesVisible("^<init>.*", "<init>stuff"));
+        assertFalse(mockPool.regexMatchesVisible("^<init>.*", "init>stuff"));
+        assertFalse(mockPool.regexMatchesVisible("^<init>.*", "<init"));	    
+        assertFalse(mockPool.regexMatchesVisible("^<init>.*", ""));        
+	}
 }



Mime
View raw message