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 * @run main XPrecisionDecimalToString 35 * @author Daniel Fuchs 36 */ 37 public class XPrecisionDecimalToString { 38 39 private static final String className = 40 "com.sun.org.apache.xerces.internal.impl.dv.xs.PrecisionDecimalDV$XPrecisionDecimal"; 41 private static final String methodName = "canonicalToStringForHashCode"; 42 private static final Class<?>[] signature = { String.class, String.class, int.class, int.class }; 43 private static Method method; 44 45 // Invokes XPrecisionDecimal.canonicalToStringForHashCode through reflection, 46 // because the method is private... 47 // 48 // Construct a canonical String representation of this number 49 // for the purpose of deriving a hashCode value compliant with 50 // equals. 51 // The toString representation will be: 52 // NaN for NaN, INF for +infinity, -INF for -infinity, 0 for zero, 53 // and [1-9]\.[0-9]*[1-9]?(E[1-9][0-9]*)? for other numbers. 54 private static String canonicalToStringForHashCode(String ivalue, String fvalue, int sign, int pvalue) { 55 try { 56 if (method == null) { 57 Class<?> type = Class.forName(className); 58 method = type.getDeclaredMethod(methodName, signature); 59 method.setAccessible(true); 60 } 61 } catch (Exception x) { 62 throw new Error("Impossible to find '"+className+"."+methodName+"': "+ x, x); 63 } 64 try { 65 return (String) method.invoke(null, new Object[] {ivalue, fvalue, sign, pvalue} ); 66 } catch(Exception x) { 67 throw new Error("Failed to invoke "+className+"."+methodName+"(\""+ 68 ivalue+"\", \""+fvalue+"\", "+sign+", "+pvalue+"): " +x, x); 69 } 70 } 71 72 /** 73 * @param args the command line arguments 74 */ 75 public static void main(String[] args) { 76 test("123","7890",-1,0,"-1.23789E2"); 77 test("0","007890",-1,0,"-7.89E-3"); 78 test("123","7890",1,0,"1.23789E2"); 79 test("0","007890",1,0,"7.89E-3"); 80 test("123","7890",1,10,"1.23789E12"); 81 test("0","007890",1,33,"7.89E30"); 82 test("INF","",1,0,"INF"); 83 test("INF","",-1,0,"-INF"); 84 test("NaN","",0,0,"NaN"); 85 test("0","",1,0,"0"); 86 test("00000","00000",1,10,"0"); 87 test("00000","00000",-1,10,"0"); 88 test("00000","000001",-1,-10,"-1E-16"); 89 } 90 91 private static void test(String ival, String fval, int sign, int pvalue, String expected) { 92 final String canonical = canonicalToStringForHashCode(ival, fval, sign, pvalue); 93 System.out.println((sign == -1 ? "-" : "") + ival + 94 ("INF".equals(ival) || "NaN".equals(ival) ? "" 95 : ( "." + fval + "E" + pvalue)) 96 + " => "+ canonical); 97 if (!expected.equals(canonical)) { 98 throw new Error("expected: "+expected+" got: "+ canonical); 99 } 100 } 101 102 103 }