Skip to content

Commit

Permalink
JRE reflection stripping: refactoring code that tests class names.
Browse files Browse the repository at this point in the history
	Change on 2018/06/26 by antoniocortes <antoniocortes@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=202148557
  • Loading branch information
antonio-cortes-perez authored and Tom Ball committed Jul 31, 2018
1 parent 6e1d77e commit e315c59
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 26 deletions.
45 changes: 45 additions & 0 deletions jre_emul/Classes/com/google/j2objc/util/ReflectionUtil.java
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 com.google.j2objc.util;

import java.util.stream.Collectors;
import java.util.stream.Stream;

/** Utilities to interact with reflection-stripped code. */
public final class ReflectionUtil {

/**
* When reflection is stripped, the transpiled code uses NSStringFromClass to return the name of a
* class. For example, instead of getting something like java.lang.Throwable, we get
* JavaLangThrowable.
*
* <p>This method assumes that {@code actual} and {@code expected} contain a class name as prefix.
* Firts, it compares directly {@code actual} to {@code expected}; if it fails, then it compares
* {@actual} to the cammel case version of {@code expected}.
*/
public static boolean matchClassNamePrefix(String actual, String expected) {
if (actual.equals(expected)) {
return true;
}
expected =
Stream.of(expected.split("\\."))
.map(s -> s.substring(0, 1).toUpperCase() + s.substring(1))
.collect(Collectors.joining());
return actual.equals(expected);
}
}
9 changes: 5 additions & 4 deletions jre_emul/Tests/com/google/j2objc/ThrowableTest.java
Expand Up @@ -16,6 +16,7 @@


package com.google.j2objc; package com.google.j2objc;


import com.google.j2objc.util.ReflectionUtil;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.PrintWriter; import java.io.PrintWriter;
Expand Down Expand Up @@ -116,10 +117,10 @@ public void testNotWritableStackTrace() throws Exception {
} }


public void testThrowableToStringFormat() { public void testThrowableToStringFormat() {
String regex = "[Jj]ava\\.?[Ll]ang\\.?Throwable"; String expected = "java.lang.Throwable";
assertTrue(new Throwable().toString().matches(regex)); assertTrue(ReflectionUtil.matchClassNamePrefix(new Throwable().toString(), expected));
regex += ": oops"; expected += ": oops";
assertTrue(new Throwable("oops").toString().matches(regex)); assertTrue(ReflectionUtil.matchClassNamePrefix(new Throwable("oops").toString(), expected));
} }


public void testNSExceptionDescriptionUnchanged() { public void testNSExceptionDescriptionUnchanged() {
Expand Down
Expand Up @@ -16,19 +16,20 @@


package libcore.java.awt.font; package libcore.java.awt.font;


import com.google.j2objc.util.ReflectionUtil;
import java.awt.font.TextAttribute; import java.awt.font.TextAttribute;
import junit.framework.TestCase; import junit.framework.TestCase;


public class TextAttributeTest extends TestCase { public class TextAttributeTest extends TestCase {


public void testAttributeNames() { public void testAttributeNames() {
// J2ObjC reflection-stripping change. // J2ObjC reflection-stripping change.
assertTrue(TextAttribute.KERNING.toString().matches( assertTrue(ReflectionUtil.matchClassNamePrefix(TextAttribute.KERNING.toString(),
"[Jj]ava\\.?[Aa]wt\\.?[Ff]ont\\.?TextAttribute\\(kerning\\)")); "java.awt.font.TextAttribute(kerning)"));
assertTrue(TextAttribute.LIGATURES.toString().matches( assertTrue(ReflectionUtil.matchClassNamePrefix(TextAttribute.LIGATURES.toString(),
"[Jj]ava\\.?[Aa]wt\\.?[Ff]ont\\.?TextAttribute\\(ligatures\\)")); "java.awt.font.TextAttribute(ligatures)"));
assertTrue(TextAttribute.TRACKING.toString().matches( assertTrue(ReflectionUtil.matchClassNamePrefix(TextAttribute.TRACKING.toString(),
"[Jj]ava\\.?[Aa]wt\\.?[Ff]ont\\.?TextAttribute\\(tracking\\)")); "java.awt.font.TextAttribute(tracking)"));
} }


public void testAttributeValues() { public void testAttributeValues() {
Expand Down
Expand Up @@ -16,6 +16,7 @@
*/ */
package org.apache.harmony.tests.java.text; package org.apache.harmony.tests.java.text;


import com.google.j2objc.util.ReflectionUtil;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.FieldPosition; import java.text.FieldPosition;


Expand Down Expand Up @@ -220,19 +221,19 @@ public void test_toString() {
fpos.setBeginIndex(2); fpos.setBeginIndex(2);
fpos.setEndIndex(3); fpos.setEndIndex(3);
// J2ObjC reflection-stripping change. // J2ObjC reflection-stripping change.
String regex = "[Jj]ava\\.?[Tt]ext\\.?FieldPosition" String expected = "java.text.FieldPosition"
+ "\\[field=1,attribute=null,beginIndex=2,endIndex=3\\]"; + "[field=1,attribute=null,beginIndex=2,endIndex=3]";
assertTrue("ToString returned the wrong value:", fpos.toString().matches(regex)); assertTrue("ToString returned the wrong value:",
ReflectionUtil.matchClassNamePrefix(fpos.toString(), expected));


FieldPosition fpos2 = new FieldPosition(DateFormat.Field.ERA); FieldPosition fpos2 = new FieldPosition(DateFormat.Field.ERA);
fpos2.setBeginIndex(4); fpos2.setBeginIndex(4);
fpos2.setEndIndex(5); fpos2.setEndIndex(5);
// J2ObjC reflection-stripping change. // J2ObjC reflection-stripping change.
String fieldRegex = DateFormat.Field.ERA.toString() expected = "java.text.FieldPosition[field=-1,attribute="
.replace("(", "\\(").replace(")", "\\)").replace("$", "\\$"); + DateFormat.Field.ERA.toString() + ",beginIndex=4,endIndex=5]";
regex = "[Jj]ava\\.?[Tt]ext\\.?FieldPosition\\[field=\\-1,attribute=" assertTrue("ToString returned the wrong value:",
+ fieldRegex + ",beginIndex=4,endIndex=5\\]"; ReflectionUtil.matchClassNamePrefix(fpos2.toString(), expected));
assertTrue("ToString returned the wrong value:", fpos2.toString().matches(regex));
} }


/** /**
Expand Down
Expand Up @@ -16,6 +16,7 @@
*/ */
package org.apache.harmony.tests.java.text; package org.apache.harmony.tests.java.text;


import com.google.j2objc.util.ReflectionUtil;
import java.text.ParsePosition; import java.text.ParsePosition;


public class ParsePositionTest extends junit.framework.TestCase { public class ParsePositionTest extends junit.framework.TestCase {
Expand Down Expand Up @@ -97,9 +98,8 @@ public void test_setIndexI() {
public void test_toString() { public void test_toString() {
// Test for method java.lang.String java.text.ParsePosition.toString() // Test for method java.lang.String java.text.ParsePosition.toString()
// J2ObjC reflection-stripping change. // J2ObjC reflection-stripping change.
assertTrue("toString failed.", assertTrue("toString failed.", ReflectionUtil.matchClassNamePrefix(pp.toString(),
pp.toString().matches("[Jj]ava\\.?[Tt]ext\\.?ParsePosition" "java.text.ParsePosition[index=2147483647,errorIndex=-1]"));
+ "\\[index=2147483647,errorIndex=\\-1\\]"));
} }


/** /**
Expand Down
Expand Up @@ -17,6 +17,7 @@


package libcore.java.util; package libcore.java.util;


import com.google.j2objc.util.ReflectionUtil;
import java.util.EventObject; import java.util.EventObject;
import junit.framework.TestCase; import junit.framework.TestCase;
import libcore.util.SerializationTester; import libcore.util.SerializationTester;
Expand All @@ -38,8 +39,8 @@ public void testGetSource() {


public void testToString() { public void testToString() {
// J2ObjC reflection-stripping change. // J2ObjC reflection-stripping change.
String regex = "[Jj]ava\\.?[Uu]til\\.?EventObject\\[source=x\\]"; String expected = "java.util.EventObject[source=x]";
assertTrue(new EventObject("x").toString().matches(regex)); assertTrue(ReflectionUtil.matchClassNamePrefix(new EventObject("x").toString(), expected));
} }


public void testSerializationNullsOutSource() { public void testSerializationNullsOutSource() {
Expand Down
Expand Up @@ -16,6 +16,7 @@
*/ */
package libcore.java.util.jar; package libcore.java.util.jar;


import com.google.j2objc.util.ReflectionUtil;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
Expand Down Expand Up @@ -82,8 +83,8 @@ public void test_clone() throws IOException {
.getMainAttributes().isEmpty()); .getMainAttributes().isEmpty());
assertEquals(emptyClone, emptyManifest); assertEquals(emptyClone, emptyManifest);
// J2ObjC reflection-stripping change. // J2ObjC reflection-stripping change.
assertTrue(emptyManifest.clone().getClass().getName().matches( assertTrue(ReflectionUtil.matchClassNamePrefix(emptyManifest.clone().getClass().getName(),
"[Jj]ava\\.?[Uu]til\\.?[Jj]ar\\.?Manifest")); "java.util.jar.Manifest"));


Manifest manifest = new Manifest(new URL(Support_Resources Manifest manifest = new Manifest(new URL(Support_Resources
.getURL("manifest/hyts_MANIFEST.MF")).openStream()); .getURL("manifest/hyts_MANIFEST.MF")).openStream());
Expand Down
Expand Up @@ -39,6 +39,7 @@


package java.text; package java.text;


import com.google.j2objc.util.ReflectionUtil;
import java.io.IOException; import java.io.IOException;
import java.io.InvalidObjectException; import java.io.InvalidObjectException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
Expand Down Expand Up @@ -2251,7 +2252,8 @@ private boolean useDateFormatSymbols() {


private boolean isGregorianCalendar() { private boolean isGregorianCalendar() {
// J2ObjC reflection-stripping change. // J2ObjC reflection-stripping change.
return calendar.getClass().getName().matches("[Jj]ava\\.?[Uu]til\\.?GregorianCalendar"); return ReflectionUtil.matchClassNamePrefix(calendar.getClass().getName(),
"java.util.GregorianCalendar");
} }


/** /**
Expand Down
1 change: 1 addition & 0 deletions jre_emul/jre_sources.mk
Expand Up @@ -526,6 +526,7 @@ JAVA_PRIVATE_SOURCES_CORE = \
com/google/j2objc/nio/charset/IconvCharsetDecoder.java \ com/google/j2objc/nio/charset/IconvCharsetDecoder.java \
com/google/j2objc/nio/charset/IconvCharsetEncoder.java \ com/google/j2objc/nio/charset/IconvCharsetEncoder.java \
com/google/j2objc/util/NativeTimeZone.java \ com/google/j2objc/util/NativeTimeZone.java \
com/google/j2objc/util/ReflectionUtil.java \
dalvik/system/BlockGuard.java \ dalvik/system/BlockGuard.java \
dalvik/system/CloseGuard.java \ dalvik/system/CloseGuard.java \
java/io/EmulatedFields.java \ java/io/EmulatedFields.java \
Expand Down

0 comments on commit e315c59

Please sign in to comment.