author | emc |
Wed, 05 Nov 2014 08:37:04 -0500 | |
changeset 27386 | 784414cffd9a |
parent 25859 | 3317bb8137f4 |
permissions | -rw-r--r-- |
22623
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
1 |
/* |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
2 |
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
3 |
* |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
4 |
* Redistribution and use in source and binary forms, with or without |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
5 |
* modification, are permitted provided that the following conditions |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
6 |
* are met: |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
7 |
* |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
8 |
* - Redistributions of source code must retain the above copyright |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
9 |
* notice, this list of conditions and the following disclaimer. |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
10 |
* |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
11 |
* - Redistributions in binary form must reproduce the above copyright |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
12 |
* notice, this list of conditions and the following disclaimer in the |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
13 |
* documentation and/or other materials provided with the distribution. |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
14 |
* |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
15 |
* - Neither the name of Oracle nor the names of its |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
16 |
* contributors may be used to endorse or promote products derived |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
17 |
* from this software without specific prior written permission. |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
18 |
* |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
20 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
21 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
22 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
26 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
27 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
29 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
30 |
*/ |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
31 |
|
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
32 |
import java.io.IOException; |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
33 |
import java.lang.reflect.Field; |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
34 |
|
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
35 |
/** |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
36 |
* The example illustrates how to use the default method for mixin. |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
37 |
* @see BuildType |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
38 |
* @see Debuggable |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
39 |
*/ |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
40 |
public class MixIn { |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
41 |
|
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
42 |
/** |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
43 |
* Implement this interface for a class that must be in debug print |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
44 |
*/ |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
45 |
public interface Debuggable { |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
46 |
|
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
47 |
/** |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
48 |
* Print the class name and all fields to a string. Uses reflection to |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
49 |
* obtain and access fields of this object. |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
50 |
* |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
51 |
* @return the string formatted like the following: <pre> |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
52 |
* State of the: <Class Name> |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
53 |
* <member name> : <value> |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
54 |
* ... |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
55 |
* </pre> |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
56 |
*/ |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
57 |
default String toDebugString() { |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
58 |
StringBuilder sb = new StringBuilder(); |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
59 |
sb.append("State of the: ").append( |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
60 |
this.getClass().getSimpleName()).append("\n"); |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
61 |
for (Class cls = this.getClass(); |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
62 |
cls != null; |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
63 |
cls = cls.getSuperclass()) { |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
64 |
for (Field f : cls.getDeclaredFields()) { |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
65 |
try { |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
66 |
f.setAccessible(true); |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
67 |
sb.append(f.getName()).append(" : "). |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
68 |
append(f.get(this)).append("\n"); |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
69 |
} catch (IllegalAccessException e) { |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
70 |
} |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
71 |
} |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
72 |
} |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
73 |
return sb.toString(); |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
74 |
} |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
75 |
} |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
76 |
|
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
77 |
/** |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
78 |
* Sample exception class to demonstrate mixin. This enum inherits the |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
79 |
* behavior of the {@link Debuggable} |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
80 |
*/ |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
81 |
public static enum BuildType implements Debuggable { |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
82 |
|
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
83 |
BUILD(0, "-build"), |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
84 |
PLAN(0, "-plan"), |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
85 |
EXCLUDE(1, "-exclude"), |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
86 |
TOTAL(2, "-total"); |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
87 |
|
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
88 |
private final int compareOrder; |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
89 |
private final String pathSuffix; |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
90 |
|
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
91 |
private BuildType(int compareOrder, String pathSuffix) { |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
92 |
this.compareOrder = compareOrder; |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
93 |
this.pathSuffix = pathSuffix; |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
94 |
} |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
95 |
|
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
96 |
public int getCompareOrder() { |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
97 |
return compareOrder; |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
98 |
} |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
99 |
|
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
100 |
public String getPathSuffix() { |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
101 |
return pathSuffix; |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
102 |
} |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
103 |
} |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
104 |
|
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
105 |
/** |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
106 |
* Illustrate the behavior of the MixClass |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
107 |
* |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
108 |
* @param args command-line arguments |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
109 |
* @throws java.io.IOException internal demo error |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
110 |
*/ |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
111 |
public static void main(final String[] args) throws IOException { |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
112 |
System.out.println(BuildType.BUILD.toDebugString()); |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
113 |
} |
29b25f1bd14b
8032056: Create demo to illustrate new practices of the default methods usage
psandoz
parents:
diff
changeset
|
114 |
} |