1 /* 2 * Copyright (C) 2012 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.testing; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import com.google.common.base.CharMatcher; 22 import com.google.common.base.Charsets; 23 import com.google.common.base.Equivalence; 24 import com.google.common.base.Joiner; 25 import com.google.common.base.Predicate; 26 import com.google.common.base.Splitter; 27 import com.google.common.base.Stopwatch; 28 import com.google.common.base.Ticker; 29 import com.google.common.collect.BiMap; 30 import com.google.common.collect.ClassToInstanceMap; 31 import com.google.common.collect.ImmutableBiMap; 32 import com.google.common.collect.ImmutableClassToInstanceMap; 33 import com.google.common.collect.ImmutableCollection; 34 import com.google.common.collect.ImmutableList; 35 import com.google.common.collect.ImmutableListMultimap; 36 import com.google.common.collect.ImmutableMap; 37 import com.google.common.collect.ImmutableMultimap; 38 import com.google.common.collect.ImmutableMultiset; 39 import com.google.common.collect.ImmutableSet; 40 import com.google.common.collect.ImmutableSetMultimap; 41 import com.google.common.collect.ImmutableSortedMap; 42 import com.google.common.collect.ImmutableSortedSet; 43 import com.google.common.collect.ImmutableTable; 44 import com.google.common.collect.ListMultimap; 45 import com.google.common.collect.MapDifference; 46 import com.google.common.collect.Multimap; 47 import com.google.common.collect.Multiset; 48 import com.google.common.collect.PeekingIterator; 49 import com.google.common.collect.Range; 50 import com.google.common.collect.RowSortedTable; 51 import com.google.common.collect.SetMultimap; 52 import com.google.common.collect.SortedMapDifference; 53 import com.google.common.collect.SortedMultiset; 54 import com.google.common.collect.SortedSetMultimap; 55 import com.google.common.collect.Table; 56 import com.google.common.io.ByteSink; 57 import com.google.common.io.ByteSource; 58 import com.google.common.io.CharSink; 59 import com.google.common.io.CharSource; 60 import com.google.common.primitives.UnsignedInteger; 61 import com.google.common.primitives.UnsignedLong; 62 import com.google.common.util.concurrent.AtomicDouble; 63 import java.io.ByteArrayInputStream; 64 import java.io.ByteArrayOutputStream; 65 import java.io.File; 66 import java.io.IOException; 67 import java.io.InputStream; 68 import java.io.OutputStream; 69 import java.io.PrintStream; 70 import java.io.PrintWriter; 71 import java.io.Reader; 72 import java.io.StringReader; 73 import java.io.StringWriter; 74 import java.io.Writer; 75 import java.lang.reflect.AnnotatedElement; 76 import java.lang.reflect.GenericDeclaration; 77 import java.lang.reflect.Type; 78 import java.math.BigDecimal; 79 import java.math.BigInteger; 80 import java.nio.Buffer; 81 import java.nio.ByteBuffer; 82 import java.nio.CharBuffer; 83 import java.nio.DoubleBuffer; 84 import java.nio.FloatBuffer; 85 import java.nio.IntBuffer; 86 import java.nio.LongBuffer; 87 import java.nio.ShortBuffer; 88 import java.nio.charset.Charset; 89 import java.util.ArrayList; 90 import java.util.BitSet; 91 import java.util.Collection; 92 import java.util.Comparator; 93 import java.util.Currency; 94 import java.util.Deque; 95 import java.util.HashMap; 96 import java.util.Iterator; 97 import java.util.LinkedList; 98 import java.util.List; 99 import java.util.ListIterator; 100 import java.util.Locale; 101 import java.util.Map; 102 import java.util.NavigableMap; 103 import java.util.NavigableSet; 104 import java.util.Optional; 105 import java.util.OptionalDouble; 106 import java.util.OptionalInt; 107 import java.util.OptionalLong; 108 import java.util.PriorityQueue; 109 import java.util.Queue; 110 import java.util.Random; 111 import java.util.Set; 112 import java.util.SortedMap; 113 import java.util.SortedSet; 114 import java.util.TreeMap; 115 import java.util.TreeSet; 116 import java.util.UUID; 117 import java.util.concurrent.BlockingDeque; 118 import java.util.concurrent.BlockingQueue; 119 import java.util.concurrent.ConcurrentMap; 120 import java.util.concurrent.ConcurrentNavigableMap; 121 import java.util.concurrent.DelayQueue; 122 import java.util.concurrent.Executor; 123 import java.util.concurrent.PriorityBlockingQueue; 124 import java.util.concurrent.SynchronousQueue; 125 import java.util.concurrent.ThreadFactory; 126 import java.util.concurrent.TimeUnit; 127 import java.util.concurrent.atomic.AtomicBoolean; 128 import java.util.concurrent.atomic.AtomicInteger; 129 import java.util.concurrent.atomic.AtomicLong; 130 import java.util.concurrent.atomic.AtomicReference; 131 import java.util.regex.MatchResult; 132 import java.util.regex.Pattern; 133 import junit.framework.TestCase; 134 135 /** 136 * Unit test for {@link ArbitraryInstances}. 137 * 138 * @author Ben Yu 139 */ 140 public class ArbitraryInstancesTest extends TestCase { 141 testGet_primitives()142 public void testGet_primitives() { 143 assertNull(ArbitraryInstances.get(void.class)); 144 assertNull(ArbitraryInstances.get(Void.class)); 145 assertEquals(Boolean.FALSE, ArbitraryInstances.get(boolean.class)); 146 assertEquals(Boolean.FALSE, ArbitraryInstances.get(Boolean.class)); 147 assertEquals(Character.valueOf('\0'), ArbitraryInstances.get(char.class)); 148 assertEquals(Character.valueOf('\0'), ArbitraryInstances.get(Character.class)); 149 assertEquals(Byte.valueOf((byte) 0), ArbitraryInstances.get(byte.class)); 150 assertEquals(Byte.valueOf((byte) 0), ArbitraryInstances.get(Byte.class)); 151 assertEquals(Short.valueOf((short) 0), ArbitraryInstances.get(short.class)); 152 assertEquals(Short.valueOf((short) 0), ArbitraryInstances.get(Short.class)); 153 assertEquals(Integer.valueOf(0), ArbitraryInstances.get(int.class)); 154 assertEquals(Integer.valueOf(0), ArbitraryInstances.get(Integer.class)); 155 assertEquals(Long.valueOf(0), ArbitraryInstances.get(long.class)); 156 assertEquals(Long.valueOf(0), ArbitraryInstances.get(Long.class)); 157 assertEquals(Float.valueOf(0), ArbitraryInstances.get(float.class)); 158 assertEquals(Float.valueOf(0), ArbitraryInstances.get(Float.class)); 159 assertEquals(Double.valueOf(0), ArbitraryInstances.get(double.class)); 160 assertEquals(Double.valueOf(0), ArbitraryInstances.get(Double.class)); 161 assertEquals(UnsignedInteger.ZERO, ArbitraryInstances.get(UnsignedInteger.class)); 162 assertEquals(UnsignedLong.ZERO, ArbitraryInstances.get(UnsignedLong.class)); 163 assertEquals(0, ArbitraryInstances.get(BigDecimal.class).intValue()); 164 assertEquals(0, ArbitraryInstances.get(BigInteger.class).intValue()); 165 assertEquals("", ArbitraryInstances.get(String.class)); 166 assertEquals("", ArbitraryInstances.get(CharSequence.class)); 167 assertEquals(TimeUnit.SECONDS, ArbitraryInstances.get(TimeUnit.class)); 168 assertNotNull(ArbitraryInstances.get(Object.class)); 169 assertEquals(0, ArbitraryInstances.get(Number.class)); 170 assertEquals(Charsets.UTF_8, ArbitraryInstances.get(Charset.class)); 171 assertEquals(Optional.empty(), ArbitraryInstances.get(Optional.class)); 172 assertEquals(OptionalInt.empty(), ArbitraryInstances.get(OptionalInt.class)); 173 assertEquals(OptionalLong.empty(), ArbitraryInstances.get(OptionalLong.class)); 174 assertEquals(OptionalDouble.empty(), ArbitraryInstances.get(OptionalDouble.class)); 175 assertNotNull(ArbitraryInstances.get(UUID.class)); 176 } 177 testGet_collections()178 public void testGet_collections() { 179 assertEquals(ImmutableSet.of().iterator(), ArbitraryInstances.get(Iterator.class)); 180 assertFalse(ArbitraryInstances.get(PeekingIterator.class).hasNext()); 181 assertFalse(ArbitraryInstances.get(ListIterator.class).hasNext()); 182 assertEquals(ImmutableSet.of(), ArbitraryInstances.get(Iterable.class)); 183 assertEquals(ImmutableSet.of(), ArbitraryInstances.get(Set.class)); 184 assertEquals(ImmutableSet.of(), ArbitraryInstances.get(ImmutableSet.class)); 185 assertEquals(ImmutableSortedSet.of(), ArbitraryInstances.get(SortedSet.class)); 186 assertEquals(ImmutableSortedSet.of(), ArbitraryInstances.get(ImmutableSortedSet.class)); 187 assertEquals(ImmutableList.of(), ArbitraryInstances.get(Collection.class)); 188 assertEquals(ImmutableList.of(), ArbitraryInstances.get(ImmutableCollection.class)); 189 assertEquals(ImmutableList.of(), ArbitraryInstances.get(List.class)); 190 assertEquals(ImmutableList.of(), ArbitraryInstances.get(ImmutableList.class)); 191 assertEquals(ImmutableMap.of(), ArbitraryInstances.get(Map.class)); 192 assertEquals(ImmutableMap.of(), ArbitraryInstances.get(ImmutableMap.class)); 193 assertEquals(ImmutableSortedMap.of(), ArbitraryInstances.get(SortedMap.class)); 194 assertEquals(ImmutableSortedMap.of(), ArbitraryInstances.get(ImmutableSortedMap.class)); 195 assertEquals(ImmutableMultiset.of(), ArbitraryInstances.get(Multiset.class)); 196 assertEquals(ImmutableMultiset.of(), ArbitraryInstances.get(ImmutableMultiset.class)); 197 assertTrue(ArbitraryInstances.get(SortedMultiset.class).isEmpty()); 198 assertEquals(ImmutableMultimap.of(), ArbitraryInstances.get(Multimap.class)); 199 assertEquals(ImmutableMultimap.of(), ArbitraryInstances.get(ImmutableMultimap.class)); 200 assertTrue(ArbitraryInstances.get(SortedSetMultimap.class).isEmpty()); 201 assertEquals(ImmutableTable.of(), ArbitraryInstances.get(Table.class)); 202 assertEquals(ImmutableTable.of(), ArbitraryInstances.get(ImmutableTable.class)); 203 assertTrue(ArbitraryInstances.get(RowSortedTable.class).isEmpty()); 204 assertEquals(ImmutableBiMap.of(), ArbitraryInstances.get(BiMap.class)); 205 assertEquals(ImmutableBiMap.of(), ArbitraryInstances.get(ImmutableBiMap.class)); 206 assertTrue(ArbitraryInstances.get(ImmutableClassToInstanceMap.class).isEmpty()); 207 assertTrue(ArbitraryInstances.get(ClassToInstanceMap.class).isEmpty()); 208 assertTrue(ArbitraryInstances.get(ListMultimap.class).isEmpty()); 209 assertTrue(ArbitraryInstances.get(ImmutableListMultimap.class).isEmpty()); 210 assertTrue(ArbitraryInstances.get(SetMultimap.class).isEmpty()); 211 assertTrue(ArbitraryInstances.get(ImmutableSetMultimap.class).isEmpty()); 212 assertTrue(ArbitraryInstances.get(MapDifference.class).areEqual()); 213 assertTrue(ArbitraryInstances.get(SortedMapDifference.class).areEqual()); 214 assertEquals(Range.all(), ArbitraryInstances.get(Range.class)); 215 assertTrue(ArbitraryInstances.get(NavigableSet.class).isEmpty()); 216 assertTrue(ArbitraryInstances.get(NavigableMap.class).isEmpty()); 217 assertTrue(ArbitraryInstances.get(LinkedList.class).isEmpty()); 218 assertTrue(ArbitraryInstances.get(Deque.class).isEmpty()); 219 assertTrue(ArbitraryInstances.get(Queue.class).isEmpty()); 220 assertTrue(ArbitraryInstances.get(PriorityQueue.class).isEmpty()); 221 assertTrue(ArbitraryInstances.get(BitSet.class).isEmpty()); 222 assertTrue(ArbitraryInstances.get(TreeSet.class).isEmpty()); 223 assertTrue(ArbitraryInstances.get(TreeMap.class).isEmpty()); 224 assertFreshInstanceReturned( 225 LinkedList.class, 226 Deque.class, 227 Queue.class, 228 PriorityQueue.class, 229 BitSet.class, 230 TreeSet.class, 231 TreeMap.class); 232 } 233 testGet_misc()234 public void testGet_misc() { 235 assertNotNull(ArbitraryInstances.get(CharMatcher.class)); 236 assertNotNull(ArbitraryInstances.get(Currency.class).getCurrencyCode()); 237 assertNotNull(ArbitraryInstances.get(Locale.class)); 238 assertNotNull(ArbitraryInstances.get(Joiner.class).join(ImmutableList.of("a"))); 239 assertNotNull(ArbitraryInstances.get(Splitter.class).split("a,b")); 240 assertThat(ArbitraryInstances.get(com.google.common.base.Optional.class)).isAbsent(); 241 ArbitraryInstances.get(Stopwatch.class).start(); 242 assertNotNull(ArbitraryInstances.get(Ticker.class)); 243 assertFreshInstanceReturned(Random.class); 244 assertEquals( 245 ArbitraryInstances.get(Random.class).nextInt(), 246 ArbitraryInstances.get(Random.class).nextInt()); 247 } 248 testGet_concurrent()249 public void testGet_concurrent() { 250 assertTrue(ArbitraryInstances.get(BlockingDeque.class).isEmpty()); 251 assertTrue(ArbitraryInstances.get(BlockingQueue.class).isEmpty()); 252 assertTrue(ArbitraryInstances.get(DelayQueue.class).isEmpty()); 253 assertTrue(ArbitraryInstances.get(SynchronousQueue.class).isEmpty()); 254 assertTrue(ArbitraryInstances.get(PriorityBlockingQueue.class).isEmpty()); 255 assertTrue(ArbitraryInstances.get(ConcurrentMap.class).isEmpty()); 256 assertTrue(ArbitraryInstances.get(ConcurrentNavigableMap.class).isEmpty()); 257 ArbitraryInstances.get(Executor.class).execute(ArbitraryInstances.get(Runnable.class)); 258 assertNotNull(ArbitraryInstances.get(ThreadFactory.class)); 259 assertFreshInstanceReturned( 260 BlockingQueue.class, 261 BlockingDeque.class, 262 PriorityBlockingQueue.class, 263 DelayQueue.class, 264 SynchronousQueue.class, 265 ConcurrentMap.class, 266 ConcurrentNavigableMap.class, 267 AtomicReference.class, 268 AtomicBoolean.class, 269 AtomicInteger.class, 270 AtomicLong.class, 271 AtomicDouble.class); 272 } 273 274 @SuppressWarnings("unchecked") // functor classes have no type parameters testGet_functors()275 public void testGet_functors() { 276 assertEquals(0, ArbitraryInstances.get(Comparator.class).compare("abc", 123)); 277 assertTrue(ArbitraryInstances.get(Predicate.class).apply("abc")); 278 assertTrue(ArbitraryInstances.get(Equivalence.class).equivalent(1, 1)); 279 assertFalse(ArbitraryInstances.get(Equivalence.class).equivalent(1, 2)); 280 } 281 282 @SuppressWarnings("SelfComparison") testGet_comparable()283 public void testGet_comparable() { 284 @SuppressWarnings("unchecked") // The null value can compare with any Object 285 Comparable<Object> comparable = ArbitraryInstances.get(Comparable.class); 286 assertEquals(0, comparable.compareTo(comparable)); 287 assertTrue(comparable.compareTo("") > 0); 288 try { 289 comparable.compareTo(null); 290 fail(); 291 } catch (NullPointerException expected) { 292 } 293 } 294 testGet_array()295 public void testGet_array() { 296 assertThat(ArbitraryInstances.get(int[].class)).isEmpty(); 297 assertThat(ArbitraryInstances.get(Object[].class)).isEmpty(); 298 assertThat(ArbitraryInstances.get(String[].class)).isEmpty(); 299 } 300 testGet_enum()301 public void testGet_enum() { 302 assertNull(ArbitraryInstances.get(EmptyEnum.class)); 303 assertEquals(Direction.UP, ArbitraryInstances.get(Direction.class)); 304 } 305 testGet_interface()306 public void testGet_interface() { 307 assertNull(ArbitraryInstances.get(SomeInterface.class)); 308 } 309 testGet_runnable()310 public void testGet_runnable() { 311 ArbitraryInstances.get(Runnable.class).run(); 312 } 313 testGet_class()314 public void testGet_class() { 315 assertSame(SomeAbstractClass.INSTANCE, ArbitraryInstances.get(SomeAbstractClass.class)); 316 assertSame( 317 WithPrivateConstructor.INSTANCE, ArbitraryInstances.get(WithPrivateConstructor.class)); 318 assertNull(ArbitraryInstances.get(NoDefaultConstructor.class)); 319 assertSame( 320 WithExceptionalConstructor.INSTANCE, 321 ArbitraryInstances.get(WithExceptionalConstructor.class)); 322 assertNull(ArbitraryInstances.get(NonPublicClass.class)); 323 } 324 testGet_mutable()325 public void testGet_mutable() { 326 assertEquals(0, ArbitraryInstances.get(ArrayList.class).size()); 327 assertEquals(0, ArbitraryInstances.get(HashMap.class).size()); 328 assertThat(ArbitraryInstances.get(Appendable.class).toString()).isEmpty(); 329 assertThat(ArbitraryInstances.get(StringBuilder.class).toString()).isEmpty(); 330 assertThat(ArbitraryInstances.get(StringBuffer.class).toString()).isEmpty(); 331 assertFreshInstanceReturned( 332 ArrayList.class, 333 HashMap.class, 334 Appendable.class, 335 StringBuilder.class, 336 StringBuffer.class, 337 Throwable.class, 338 Exception.class); 339 } 340 testGet_io()341 public void testGet_io() throws IOException { 342 assertEquals(-1, ArbitraryInstances.get(InputStream.class).read()); 343 assertEquals(-1, ArbitraryInstances.get(ByteArrayInputStream.class).read()); 344 assertEquals(-1, ArbitraryInstances.get(Readable.class).read(CharBuffer.allocate(1))); 345 assertEquals(-1, ArbitraryInstances.get(Reader.class).read()); 346 assertEquals(-1, ArbitraryInstances.get(StringReader.class).read()); 347 assertEquals(0, ArbitraryInstances.get(Buffer.class).capacity()); 348 assertEquals(0, ArbitraryInstances.get(CharBuffer.class).capacity()); 349 assertEquals(0, ArbitraryInstances.get(ByteBuffer.class).capacity()); 350 assertEquals(0, ArbitraryInstances.get(ShortBuffer.class).capacity()); 351 assertEquals(0, ArbitraryInstances.get(IntBuffer.class).capacity()); 352 assertEquals(0, ArbitraryInstances.get(LongBuffer.class).capacity()); 353 assertEquals(0, ArbitraryInstances.get(FloatBuffer.class).capacity()); 354 assertEquals(0, ArbitraryInstances.get(DoubleBuffer.class).capacity()); 355 ArbitraryInstances.get(PrintStream.class).println("test"); 356 ArbitraryInstances.get(PrintWriter.class).println("test"); 357 assertNotNull(ArbitraryInstances.get(File.class)); 358 assertFreshInstanceReturned( 359 ByteArrayOutputStream.class, OutputStream.class, 360 Writer.class, StringWriter.class, 361 PrintStream.class, PrintWriter.class); 362 assertEquals(ByteSource.empty(), ArbitraryInstances.get(ByteSource.class)); 363 assertEquals(CharSource.empty(), ArbitraryInstances.get(CharSource.class)); 364 assertNotNull(ArbitraryInstances.get(ByteSink.class)); 365 assertNotNull(ArbitraryInstances.get(CharSink.class)); 366 } 367 testGet_reflect()368 public void testGet_reflect() { 369 assertNotNull(ArbitraryInstances.get(Type.class)); 370 assertNotNull(ArbitraryInstances.get(AnnotatedElement.class)); 371 assertNotNull(ArbitraryInstances.get(GenericDeclaration.class)); 372 } 373 testGet_regex()374 public void testGet_regex() { 375 assertEquals(Pattern.compile("").pattern(), ArbitraryInstances.get(Pattern.class).pattern()); 376 assertEquals(0, ArbitraryInstances.get(MatchResult.class).groupCount()); 377 } 378 testGet_usePublicConstant()379 public void testGet_usePublicConstant() { 380 assertSame(WithPublicConstant.INSTANCE, ArbitraryInstances.get(WithPublicConstant.class)); 381 } 382 testGet_useFirstPublicConstant()383 public void testGet_useFirstPublicConstant() { 384 assertSame(WithPublicConstants.FIRST, ArbitraryInstances.get(WithPublicConstants.class)); 385 } 386 testGet_nullConstantIgnored()387 public void testGet_nullConstantIgnored() { 388 assertSame(FirstConstantIsNull.SECOND, ArbitraryInstances.get(FirstConstantIsNull.class)); 389 } 390 testGet_constantWithGenericsNotUsed()391 public void testGet_constantWithGenericsNotUsed() { 392 assertNull(ArbitraryInstances.get(WithGenericConstant.class)); 393 } 394 testGet_nullConstant()395 public void testGet_nullConstant() { 396 assertNull(ArbitraryInstances.get(WithNullConstant.class)); 397 } 398 testGet_constantTypeDoesNotMatch()399 public void testGet_constantTypeDoesNotMatch() { 400 assertNull(ArbitraryInstances.get(ParentClassHasConstant.class)); 401 } 402 testGet_nonPublicConstantNotUsed()403 public void testGet_nonPublicConstantNotUsed() { 404 assertNull(ArbitraryInstances.get(NonPublicConstantIgnored.class)); 405 } 406 testGet_nonStaticFieldNotUsed()407 public void testGet_nonStaticFieldNotUsed() { 408 assertNull(ArbitraryInstances.get(NonStaticFieldIgnored.class)); 409 } 410 testGet_constructorPreferredOverConstants()411 public void testGet_constructorPreferredOverConstants() { 412 assertNotNull(ArbitraryInstances.get(WithPublicConstructorAndConstant.class)); 413 assertTrue( 414 ArbitraryInstances.get(WithPublicConstructorAndConstant.class) 415 != ArbitraryInstances.get(WithPublicConstructorAndConstant.class)); 416 } 417 testGet_nonFinalFieldNotUsed()418 public void testGet_nonFinalFieldNotUsed() { 419 assertNull(ArbitraryInstances.get(NonFinalFieldIgnored.class)); 420 } 421 assertFreshInstanceReturned(Class<?>.... mutableClasses)422 private static void assertFreshInstanceReturned(Class<?>... mutableClasses) { 423 for (Class<?> mutableClass : mutableClasses) { 424 Object instance = ArbitraryInstances.get(mutableClass); 425 assertNotNull("Expected to return non-null for: " + mutableClass, instance); 426 assertNotSame( 427 "Expected to return fresh instance for: " + mutableClass, 428 instance, 429 ArbitraryInstances.get(mutableClass)); 430 } 431 } 432 433 private enum EmptyEnum {} 434 435 private enum Direction { 436 UP, 437 DOWN 438 } 439 440 public interface SomeInterface {} 441 442 public abstract static class SomeAbstractClass { 443 public static final SomeAbstractClass INSTANCE = new SomeAbstractClass() {}; 444 SomeAbstractClass()445 public SomeAbstractClass() {} 446 } 447 448 static class NonPublicClass { NonPublicClass()449 public NonPublicClass() {} 450 } 451 452 private static class WithPrivateConstructor { 453 public static final WithPrivateConstructor INSTANCE = new WithPrivateConstructor(); 454 } 455 456 public static class NoDefaultConstructor { NoDefaultConstructor(@uppressWarnings"unused") int i)457 public NoDefaultConstructor(@SuppressWarnings("unused") int i) {} 458 } 459 460 public static class WithExceptionalConstructor { 461 public static final WithExceptionalConstructor INSTANCE = 462 new WithExceptionalConstructor("whatever"); 463 WithExceptionalConstructor()464 public WithExceptionalConstructor() { 465 throw new RuntimeException(); 466 } 467 WithExceptionalConstructor(String unused)468 private WithExceptionalConstructor(String unused) {} 469 } 470 471 private static class WithPublicConstant { 472 public static final WithPublicConstant INSTANCE = new WithPublicConstant(); 473 } 474 475 private static class ParentClassHasConstant extends WithPublicConstant {} 476 477 public static class WithGenericConstant<T> { 478 public static final WithGenericConstant<String> STRING_CONSTANT = new WithGenericConstant<>(); 479 WithGenericConstant()480 private WithGenericConstant() {} 481 } 482 483 public static class WithNullConstant { 484 public static final WithNullConstant NULL = null; 485 WithNullConstant()486 private WithNullConstant() {} 487 } 488 489 public static class WithPublicConstructorAndConstant { 490 public static final WithPublicConstructorAndConstant INSTANCE = 491 new WithPublicConstructorAndConstant(); 492 WithPublicConstructorAndConstant()493 public WithPublicConstructorAndConstant() {} 494 } 495 496 private static class WithPublicConstants { 497 public static final WithPublicConstants FIRST = new WithPublicConstants(); 498 499 // To test that we pick the first constant alphabetically 500 @SuppressWarnings("unused") 501 public static final WithPublicConstants SECOND = new WithPublicConstants(); 502 } 503 504 private static class FirstConstantIsNull { 505 // To test that null constant is ignored 506 @SuppressWarnings("unused") 507 public static final FirstConstantIsNull FIRST = null; 508 509 public static final FirstConstantIsNull SECOND = new FirstConstantIsNull(); 510 } 511 512 public static class NonFinalFieldIgnored { 513 public static NonFinalFieldIgnored instance = new NonFinalFieldIgnored(); 514 NonFinalFieldIgnored()515 private NonFinalFieldIgnored() {} 516 } 517 518 public static class NonPublicConstantIgnored { 519 static final NonPublicConstantIgnored INSTANCE = new NonPublicConstantIgnored(); 520 NonPublicConstantIgnored()521 private NonPublicConstantIgnored() {} 522 } 523 524 public static class NonStaticFieldIgnored { 525 // This should cause infinite recursion. But it shouldn't be used anyway. 526 public final NonStaticFieldIgnored instance = new NonStaticFieldIgnored(); 527 NonStaticFieldIgnored()528 private NonStaticFieldIgnored() {} 529 } 530 } 531