1 // Copyright 2017 The Bazel Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package testsubjects; 15 16 import java.io.BufferedReader; 17 import java.io.Closeable; 18 import java.io.File; 19 import java.io.FileReader; 20 import java.io.IOException; 21 import java.sql.Connection; 22 import java.sql.SQLException; 23 import java.sql.Statement; 24 import java.util.List; 25 import java.util.function.BinaryOperator; 26 import java.util.regex.Pattern; 27 import java.util.regex.PatternSyntaxException; 28 29 /** 30 * Test subject for testing bytecode type inference {@link 31 * com.google.devtools.build.android.desugar.BytecodeTypeInference} 32 */ 33 public class TestSubject { 34 35 private static int VALUE_ONE = 1; 36 private static int VALUE_TWO = 2; 37 38 static int catchTest(Object key, Object value) { 39 if (!(key instanceof String)) { 40 return VALUE_ONE; 41 } 42 try { 43 Pattern.compile((String) key); 44 } catch (PatternSyntaxException e) { 45 return VALUE_TWO; 46 } 47 return VALUE_ONE; 48 } 49 50 public static void assertEquals(String message, double expected, double actual, double delta) { 51 if (Double.compare(expected, actual) == 0) { 52 return; 53 } 54 if (!(Math.abs(expected - actual) <= delta)) { 55 throw new RuntimeException(message + new Double(expected) + new Double(actual)); 56 } 57 } 58 59 /** 60 * A simple resource implementation which implements Closeable. 61 */ 62 public static class SimpleResource implements Closeable { 63 64 public void call(boolean throwException) { 65 if (throwException) { 66 throw new RuntimeException("exception in call()"); 67 } 68 } 69 70 @Override 71 public void close() throws IOException { 72 throw new IOException("exception in close()."); 73 } 74 } 75 76 public static void simpleTryWithResources() throws Exception { 77 // Throwable.addSuppressed(Throwable) should be called in the following block. 78 try (SimpleResource resource = new SimpleResource()) { 79 resource.call(true); 80 } 81 } 82 83 private static long internalCompare(long a, long b, BinaryOperator<Long> func) { 84 return func.apply(a, b); 85 } 86 87 public void closeResourceArray(Statement[] resources) throws Exception { 88 for (Statement stmt : resources) { 89 closeResource(stmt, null); 90 } 91 } 92 93 public void closeResourceMultiArray(Statement[][] resources) throws Exception { 94 for (Statement[] stmts : resources) { 95 for (Statement stmt : stmts) { 96 closeResource(stmt, null); 97 } 98 } 99 } 100 101 public void closeResourceArrayList(List<Statement> resources) throws Exception { 102 for (Statement stmt : resources) { 103 closeResource(stmt, null); 104 } 105 } 106 107 public void closeSqlStmt(Connection connection) throws Exception { 108 Statement stmt = null; 109 110 try { 111 stmt = connection.createStatement(); 112 } catch (SQLException e) { 113 closeResource(stmt, e); 114 } 115 closeResource(stmt, null); 116 } 117 118 public void closeResource(AutoCloseable resource, Throwable suppressor) throws Exception { 119 if (resource == null) { 120 return; 121 } 122 try { 123 resource.close(); 124 } catch (Exception e) { 125 if (suppressor != null) { 126 suppressor.addSuppressed(e); 127 } 128 throw e; 129 } 130 } 131 132 public static int intAdd(int i, int j) { 133 int tmp = i; 134 tmp++; 135 ++tmp; 136 tmp += j; 137 tmp--; 138 --tmp; 139 tmp -= j; 140 tmp *= j; 141 tmp /= j; 142 tmp = tmp % j; 143 tmp = tmp << 2; 144 tmp = tmp >> j; 145 tmp = tmp >>> 3; 146 long longTemp = tmp; 147 longTemp = longTemp << j; 148 return (int) longTemp; 149 } 150 151 public static Number createNumberWithDiamond(boolean flag) { 152 Number n = null; 153 if (flag) { 154 n = new Integer(1); 155 } else { 156 n = new Double(1); 157 } 158 return n; 159 } 160 161 public static Object[][] createMultiObjectArray() { 162 return new Object[0][0]; 163 } 164 165 public static Object[] createObjectArray() { 166 return new Object[0]; 167 } 168 169 public static int[] createIntArray() { 170 return new int[0]; 171 } 172 173 public static void staticEmpty1() {} 174 175 public void instanceEmpty1() {} 176 177 public static boolean identity(boolean result) { 178 return result; 179 } 180 181 public static boolean identity2(boolean result) { 182 boolean temp = result; 183 return temp; 184 } 185 186 public void readFile(File file) throws Exception { 187 try (AutoCloseable reader = new BufferedReader(new FileReader(file)); 188 AutoCloseable reader2 = new BufferedReader(new FileReader(file)); 189 AutoCloseable reader3 = new BufferedReader(new FileReader(file)); 190 AutoCloseable reader4 = new BufferedReader(new FileReader(file))) { 191 192 } catch (IOException e) { 193 e.printStackTrace(); 194 } 195 } 196 197 public double testWithDoubleTypes() { 198 double result = 1; 199 for (double i = 1; i < 22; i = i + 1) { 200 System.out.println(i); 201 result += i; 202 } 203 return result; 204 } 205 206 public float testWithFloatAndDoubleTypes() { 207 float result = 1; 208 for (double i = 1; i < 22; i = i + 1) { 209 System.out.println(i); 210 result += (float) i; 211 } 212 return result; 213 } 214 } 215