Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/sample/lambda/DefaultMethods/MixIn.java
38829 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031import java.io.IOException;32import java.lang.reflect.Field;3334/**35* The example illustrates how to use the default method for mixin.36* @see BuildType37* @see Debuggable38*/39public class MixIn {4041/**42* Implement this interface for a class that must be in debug print43*/44public interface Debuggable {4546/**47* Print the class name and all fields to a string. Uses reflection to48* obtain and access fields of this object.49*50* @return the string formatted like the following: <pre>51* State of the: <Class Name>52* <member name> : <value>53* ...54* </pre>55*/56default String toDebugString() {57StringBuilder sb = new StringBuilder();58sb.append("State of the: ").append(59this.getClass().getSimpleName()).append("\n");60for (Class cls = this.getClass();61cls != null;62cls = cls.getSuperclass()) {63for (Field f : cls.getDeclaredFields()) {64try {65f.setAccessible(true);66sb.append(f.getName()).append(" : ").67append(f.get(this)).append("\n");68} catch (IllegalAccessException e) {69}70}71}72return sb.toString();73}74}7576/**77* Sample exception class to demonstrate mixin. This enum inherits the78* behavior of the {@link Debuggable}79*/80public static enum BuildType implements Debuggable {8182BUILD(0, "-build"),83PLAN(0, "-plan"),84EXCLUDE(1, "-exclude"),85TOTAL(2, "-total");8687private final int compareOrder;88private final String pathSuffix;8990private BuildType(int compareOrder, String pathSuffix) {91this.compareOrder = compareOrder;92this.pathSuffix = pathSuffix;93}9495public int getCompareOrder() {96return compareOrder;97}9899public String getPathSuffix() {100return pathSuffix;101}102}103104/**105* Illustrate the behavior of the MixClass106*107* @param args command-line arguments108* @throws java.io.IOException internal demo error109*/110public static void main(final String[] args) throws IOException {111System.out.println(BuildType.BUILD.toDebugString());112}113}114115116