1 /*
2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 import java.lang.reflect.Method;
25 
26 /**
27  * @test
28  * @bug 8013900
29  * @summary More warning compiling jaxp.
30  *   This test only test one of the methods used to implement hashCode()
31  *   in com.sun.org.apache.xerces.internal.impl.dv.xs.PrecisionDecimalDV$XPrecisionDecimal.
32  *   Since that method is private the test unfortunately needs to use reflection
33  *   to invoke the method.
34  * @modules java.xml/com.sun.org.apache.xerces.internal.impl.dv.xs:open
35  * @run main XPrecisionDecimalToString
36  * @author Daniel Fuchs
37  */
38 public class XPrecisionDecimalToString {
39 
40     private static final String className =
41             "com.sun.org.apache.xerces.internal.impl.dv.xs.PrecisionDecimalDV$XPrecisionDecimal";
42     private static final String methodName = "canonicalToStringForHashCode";
43     private static final Class<?>[] signature = { String.class, String.class, int.class, int.class };
44     private static Method method;
45 
46     // Invokes XPrecisionDecimal.canonicalToStringForHashCode through reflection,
47     // because the method is private...
48     //
49     // Construct a canonical String representation of this number
50     // for the purpose of deriving a hashCode value compliant with
51     // equals.
52     // The toString representation will be:
53     // NaN for NaN, INF for +infinity, -INF for -infinity, 0 for zero,
54     // and [1-9]\.[0-9]*[1-9]?(E[1-9][0-9]*)? for other numbers.
canonicalToStringForHashCode(String ivalue, String fvalue, int sign, int pvalue)55     private static String canonicalToStringForHashCode(String ivalue, String fvalue, int sign, int pvalue) {
56         try {
57             if (method == null) {
58                 Class<?> type = Class.forName(className);
59                 method = type.getDeclaredMethod(methodName, signature);
60                 method.setAccessible(true);
61             }
62         } catch (Exception x) {
63             throw new Error("Impossible to find '"+className+"."+methodName+"': "+ x, x);
64         }
65         try {
66             return (String) method.invoke(null, new Object[] {ivalue, fvalue, sign, pvalue} );
67         } catch(Exception x) {
68             throw new Error("Failed to invoke "+className+"."+methodName+"(\""+
69                     ivalue+"\", \""+fvalue+"\", "+sign+", "+pvalue+"): " +x, x);
70         }
71     }
72 
73     /**
74      * @param args the command line arguments
75      */
main(String[] args)76     public static void main(String[] args) {
77         test("123","7890",-1,0,"-1.23789E2");
78         test("0","007890",-1,0,"-7.89E-3");
79         test("123","7890",1,0,"1.23789E2");
80         test("0","007890",1,0,"7.89E-3");
81         test("123","7890",1,10,"1.23789E12");
82         test("0","007890",1,33,"7.89E30");
83         test("INF","",1,0,"INF");
84         test("INF","",-1,0,"-INF");
85         test("NaN","",0,0,"NaN");
86         test("0","",1,0,"0");
87         test("00000","00000",1,10,"0");
88         test("00000","00000",-1,10,"0");
89         test("00000","000001",-1,-10,"-1E-16");
90     }
91 
test(String ival, String fval, int sign, int pvalue, String expected)92     private static void test(String ival, String fval, int sign, int pvalue, String expected) {
93         final String canonical = canonicalToStringForHashCode(ival, fval, sign, pvalue);
94         System.out.println((sign == -1 ? "-" : "") + ival +
95                 ("INF".equals(ival) || "NaN".equals(ival) ? ""
96                  : ( "." + fval + "E" + pvalue))
97                 + " => "+ canonical);
98         if (!expected.equals(canonical)) {
99             throw new Error("expected: "+expected+" got: "+ canonical);
100         }
101     }
102 
103 
104 }
105