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