1 /* 2 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 4728816 27 * @summary JPDA: Add support for enums 28 * 29 * @author jjh 30 * 31 * @modules jdk.jdi 32 * @run build TestScaffold VMConnection TargetListener TargetAdapter 33 * @run compile -g EnumTest.java 34 * @run driver EnumTest 35 */ 36 import com.sun.jdi.*; 37 import com.sun.jdi.event.*; 38 import com.sun.jdi.request.*; 39 40 import java.util.*; 41 42 /********** target program **********/ 43 44 45 enum Coin { 46 penny(1), nickel(5), dime(10), quarter(25); 47 48 Coin(int value) { this.value = value; } 49 50 private final int value; 51 52 public int value() { return value; } 53 } 54 55 class EnumTarg { 56 static Coin myCoin = Coin.penny; 57 public static void main(String[] args){ 58 System.out.println("Howdy!"); 59 System.out.println("Goodbye from EnumTarg!"); 60 } 61 } 62 63 /********** test program **********/ 64 65 public class EnumTest extends TestScaffold { 66 ReferenceType targetClass; 67 68 EnumTest (String args[]) { 69 super(args); 70 } 71 72 public static void main(String[] args) throws Exception { 73 new EnumTest(args).startTests(); 74 } 75 76 void fail(String reason) throws Exception { 77 failure(reason); 78 } 79 80 /********** test core **********/ 81 82 83 protected void runTests() throws Exception { 84 /* 85 * Get to the top of main() 86 * to determine targetClass 87 */ 88 BreakpointEvent bpe = startToMain("EnumTarg"); 89 targetClass = bpe.location().declaringType(); 90 91 ReferenceType rt = findReferenceType("EnumTarg"); 92 Field myField = rt.fieldByName("myCoin"); 93 ObjectReference enumObject = (ObjectReference)rt.getValue(myField); 94 ClassType enumClass =(ClassType) enumObject.referenceType(); 95 ClassType superClass = enumClass.superclass(); 96 if (!superClass.name().equals("java.lang.Enum")) { 97 fail("failure: Superclass of enum class is not java.lang.Enum: " + superClass.name()); 98 } 99 if (!enumClass.isEnum()) { 100 fail("failure: isEnum() is false but should be true"); 101 } 102 if (((ClassType)rt).isEnum()) { 103 fail("failure: isEnum() is true for EnumTarg but should be false"); 104 } 105 Field enumConstant = enumClass.fieldByName("penny"); 106 if (!enumConstant.isEnumConstant()) { 107 fail("failure: The 'penny' field is not marked " + 108 "as an enum constant."); 109 } 110 111 /* 112 * This isn't really part of the test, it just 113 * shows how to look at all the enum constants, 114 * but not necessarily in the correct order 115 */ 116 List allFields = enumClass.fields(); 117 List enumConstantFields = new ArrayList(); 118 StringBuffer enumDecl = new StringBuffer("enum " + enumClass.name() + " {"); 119 char delim = ' '; 120 121 for (Iterator iter = allFields.iterator(); iter.hasNext(); ) { 122 Field aField = (Field)iter.next(); 123 if (aField.isEnumConstant()) { 124 enumDecl.append(' '); 125 enumDecl.append(aField.name()); 126 enumDecl.append(delim); 127 delim = ','; 128 } 129 } 130 enumDecl.append("; };"); 131 System.out.println("Enum decl is: " + enumDecl); 132 133 listenUntilVMDisconnect(); 134 135 /* 136 * deal with results of test 137 * if anything has called failure("foo") testFailed will be true 138 */ 139 if (!testFailed) { 140 println("EnumTest: passed"); 141 } else { 142 throw new Exception("EnumTest: failed"); 143 } 144 } 145 }