diff options
author | Paul Duffin <paulduffin@google.com> | 2017-03-20 15:56:40 +0000 |
---|---|---|
committer | Paul Duffin <paulduffin@google.com> | 2017-03-21 16:19:39 +0000 |
commit | 3b84259b78fc811b14079dfde655d68a389b36db (patch) | |
tree | 173a772440f53a82fb5ddad139888c008d871087 | |
parent | a5323b07348221b528c9efed103036996981fedb (diff) | |
download | icu-3b84259b78fc811b14079dfde655d68a389b36db.tar.gz |
Made DigitList public for testing
* Added new rule to make classes visible for testing.
* Updated tools/srcgen/generate_android_icu4j.sh to delete
previous work around for DigitListTest.
* Added DigitListTest to android_icu4j
Bug: 33448125
Test: tools/srcgen/generate_android_icu4j.sh
Change-Id: I5126a51b895e877cdac5adad66cfce60115016e4
5 files changed, 152 insertions, 5 deletions
diff --git a/android_icu4j/src/main/java/android/icu/text/DigitList.java b/android_icu4j/src/main/java/android/icu/text/DigitList.java index 83f1bddd0..78f9eeb7f 100644 --- a/android_icu4j/src/main/java/android/icu/text/DigitList.java +++ b/android_icu4j/src/main/java/android/icu/text/DigitList.java @@ -43,8 +43,9 @@ import java.math.BigInteger; * @see java.text.MessageFormat * @version 1.18 08/12/98 * @author Mark Davis, Alan Liu + * @hide Made public for testing * */ -final class DigitList { +public final class DigitList { /** * The maximum number of significant digits in an IEEE 754 double, that * is, in a Java double. This must not be increased, or garbage digits diff --git a/android_icu4j/src/main/tests/android/icu/text/DigitListTest.java b/android_icu4j/src/main/tests/android/icu/text/DigitListTest.java new file mode 100644 index 000000000..631deb9a8 --- /dev/null +++ b/android_icu4j/src/main/tests/android/icu/text/DigitListTest.java @@ -0,0 +1,45 @@ +/* GENERATED SOURCE. DO NOT MODIFY. */ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html#License + +package android.icu.text; + +import org.junit.Before; +import org.junit.Test; + +import android.icu.dev.test.TestFmwk; + + +public class DigitListTest extends TestFmwk { + + private static DigitList digitList = new DigitList(); + private static long testdata = 1414213562; + + @Before + public void init() { + digitList.set(testdata); + } + + @Test + public void TestToString() { + String digitListStr = digitList.toString(); + assertEquals("DigitList incorrect", "0.1414213562x10^10", digitListStr); + } + @Test + public void TestHashCode() { + int dlHashcode = digitList.hashCode(); + assertEquals("DigitList hash code incorrect", -616183837, dlHashcode); + } + + @Test + public void TestEquals() { + DigitList digitList2 = new DigitList(); + + // Test for success + digitList2.set(testdata); + assertTrue("DigitList objects with same values found unequal", digitList.equals(digitList2)); + // Test for failure + digitList2.set(testdata+1); + assertFalse("DigitList objects with different values found equal", digitList.equals(digitList2)); + } +} diff --git a/tools/srcgen/currysrc/src/main/java/com/google/currysrc/processors/MakeClassesPublic.java b/tools/srcgen/currysrc/src/main/java/com/google/currysrc/processors/MakeClassesPublic.java new file mode 100644 index 000000000..26d1164c2 --- /dev/null +++ b/tools/srcgen/currysrc/src/main/java/com/google/currysrc/processors/MakeClassesPublic.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * 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 com.google.currysrc.processors; + +import com.google.common.collect.Lists; +import com.google.currysrc.api.process.Context; +import com.google.currysrc.api.process.JavadocUtils; +import com.google.currysrc.api.process.Processor; +import com.google.currysrc.api.process.ast.TypeLocator; +import java.util.ArrayList; +import java.util.List; +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ASTVisitor; +import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; +import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.EnumDeclaration; +import org.eclipse.jdt.core.dom.Modifier; +import org.eclipse.jdt.core.dom.TypeDeclaration; +import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; +import org.eclipse.jdt.core.dom.rewrite.ListRewrite; + +/** + * Makes classes public. + */ +public final class MakeClassesPublic implements Processor { + + private final List<TypeLocator> whitelist; + + private final String reason; + + public MakeClassesPublic(List<TypeLocator> whitelist, String reason) { + this.whitelist = whitelist; + this.reason = reason; + } + + @Override public final void process(Context context, CompilationUnit cu) { + final List<AbstractTypeDeclaration> toMakePublic = new ArrayList<>(); + cu.accept(new ASTVisitor() { + @Override + public boolean visit(TypeDeclaration node) { + return visitAbstract(node); + } + + @Override + public boolean visit(EnumDeclaration node) { + return visitAbstract(node); + } + + private boolean visitAbstract(AbstractTypeDeclaration node) { + for (TypeLocator whitelistedType : whitelist) { + if (whitelistedType.matches(node)) { + toMakePublic.add(node); + break; + } + } + return false; + } + }); + ASTRewrite rewrite = context.rewrite(); + for (AbstractTypeDeclaration node : Lists.reverse(toMakePublic)) { + JavadocUtils.addJavadocTag(rewrite, node, reason); + ChildListPropertyDescriptor modifiersProperty = node.getModifiersProperty(); + AST ast = rewrite.getAST(); + ListRewrite listRewrite = rewrite.getListRewrite(node, modifiersProperty); + // This will not work properly if the node already has a modifier such as protected or + // private but as this is intended for handling package private classes that is not an issue. + listRewrite.insertAt(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD), 0, null); + } + } +} diff --git a/tools/srcgen/generate_android_icu4j.sh b/tools/srcgen/generate_android_icu4j.sh index e4f260964..e5edb4622 100755 --- a/tools/srcgen/generate_android_icu4j.sh +++ b/tools/srcgen/generate_android_icu4j.sh @@ -75,10 +75,6 @@ ALL_TEST_INPUT_DIRS="${TEST_INPUT_DIRS} ${TESTDATA_DIR}" ${SRCGEN_JAVA_BINARY} ${SRCGEN_JAVA_ARGS} -cp ${CLASSPATH} com.android.icu4j.srcgen.Icu4jTestsTransform \ ${ALL_TEST_INPUT_DIRS} ${TEST_DEST_DIR} -# Bug work-around (b/33448125), CTS does not cope with DigitListTest accessing -# a non-public class. -rm -f ${TEST_DEST_DIR}/android/icu/text/DigitListTest.java - # Copy the data files. echo Copying test data for INPUT_DIR in ${ALL_TEST_INPUT_DIRS}; do diff --git a/tools/srcgen/src/main/java/com/android/icu4j/srcgen/Icu4jTransform.java b/tools/srcgen/src/main/java/com/android/icu4j/srcgen/Icu4jTransform.java index be2adc396..d1b0e7e86 100644 --- a/tools/srcgen/src/main/java/com/android/icu4j/srcgen/Icu4jTransform.java +++ b/tools/srcgen/src/main/java/com/android/icu4j/srcgen/Icu4jTransform.java @@ -29,6 +29,7 @@ import com.google.currysrc.api.process.ast.BodyDeclarationLocators; import com.google.currysrc.api.process.ast.TypeLocator; import com.google.currysrc.processors.HidePublicClasses; import com.google.currysrc.processors.InsertHeader; +import com.google.currysrc.processors.MakeClassesPublic; import com.google.currysrc.processors.ModifyQualifiedNames; import com.google.currysrc.processors.ModifyStringLiterals; import com.google.currysrc.processors.RemoveJavaDocTags; @@ -50,6 +51,12 @@ import static com.android.icu4j.srcgen.Icu4jTransformRules.createOptionalRule; */ public class Icu4jTransform { + // The list of non-public ICU API classes exposed on Android for testing. + static final String[] MAKE_PUBLIC_FOR_TESTING = new String[] { + /* ASCII order please. */ + "android.icu.text.DigitList", + }; + // The list of public ICU API classes exposed on Android. If you change this, you should change // the INITIAL_DEPRECATED_SET below to include entries from the new classes. static final String[] PUBLIC_API_CLASSES = new String[] { @@ -679,6 +686,11 @@ public class Icu4jTransform { // AST change: Hide all ICU public classes except those in the whitelist. createHidePublicClassesRule(), + + // AST change: Make whitelisted ICU classes public for testing. This should come after the + // createHidePublicClassesRule() as otherwise there will be two @hide tags added. + createMakePublicForTesting(), + // AST change: Hide ICU methods that are deprecated and Android does not want to make // public. createHideOriginalDeprecatedClassesRule(), @@ -738,6 +750,15 @@ public class Icu4jTransform { new TagMatchingDeclarations(blacklist, "@hide unsupported on Android")); } + private static Rule createMakePublicForTesting() { + ImmutableList.Builder<TypeLocator> apiClassesWhitelistBuilder = ImmutableList.builder(); + for (String publicClassName : MAKE_PUBLIC_FOR_TESTING) { + apiClassesWhitelistBuilder.add(new TypeLocator(publicClassName)); + } + return createOptionalRule(new MakeClassesPublic(apiClassesWhitelistBuilder.build(), + "@hide Made public for testing")); + } + private static Rule createHidePublicClassesRule() { ImmutableList.Builder<TypeLocator> apiClassesWhitelistBuilder = ImmutableList.builder(); for (String publicClassName : PUBLIC_API_CLASSES) { |