Advertisement
DulcetAirman

StackOverflowError

Jan 14th, 2016
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. package com.example.foo;
  2.  
  3. import java.util.AbstractMap.SimpleImmutableEntry;
  4. import java.util.Map.Entry;
  5.  
  6. public class SomeClass {
  7.  
  8.   public static void main(final String[] args) {
  9.  
  10.     class Ref {
  11.       Object value;
  12.  
  13.       @Override
  14.       public String toString() {
  15.         try {
  16.           return this.value.toString();
  17.         } catch (final StackOverflowError e) {
  18.           return "...";
  19.         }
  20.       }
  21.     }
  22.     Entry<Ref, Ref> entry;
  23.     final Ref ar1 = new Ref();
  24.     final Ref ar2 = new Ref();
  25.     entry = new SimpleImmutableEntry<>(ar1, ar2);
  26.     ar1.value = entry;
  27.     ar2.value = ar1;
  28.     // Object-graph contains loop:
  29.     // Entry[key=Entry, value=key]
  30.     try {
  31.       System.out.println(entry.toString());
  32.     } catch (final Throwable e) {
  33.       e.printStackTrace();
  34.     }
  35.   }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement