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  * @author jjh
  29  *
  30  * @run build TestScaffold VMConnection TargetListener TargetAdapter
  31  * @run compile -g EnumTest.java
  32  * @run driver EnumTest
  33  */
  34 import com.sun.jdi.*;
  35 import com.sun.jdi.event.*;
  36 import com.sun.jdi.request.*;
  37 
  38 import java.util.*;
  39 
  40     /********** target program **********/
  41 
  42 
  43 enum Coin {
  44     penny(1), nickel(5), dime(10), quarter(25);
  45 
  46     Coin(int value) { this.value = value; }
  47 
  48     private final int value;
  49 
  50     public int value() { return value; }
  51 }
  52 
  53 class EnumTarg {
  54     static Coin myCoin = Coin.penny;
  55     public static void main(String[] args){
  56         System.out.println("Howdy!");
  57         System.out.println("Goodbye from EnumTarg!");
  58     }
  59 }
  60 
  61     /********** test program **********/
  62 
  63 public class EnumTest extends TestScaffold {
  64     ReferenceType targetClass;
  65 
  66     EnumTest (String args[]) {
  67         super(args);
  68     }
  69 
  70     public static void main(String[] args)      throws Exception {
  71         new EnumTest(args).startTests();
  72     }
  73 
  74     void fail(String reason) throws Exception {
  75         failure(reason);
  76     }
  77 
  78     /********** test core **********/
  79 
  80 
  81     protected void runTests() throws Exception {
  82         /*
  83          * Get to the top of main()
  84          * to determine targetClass
  85          */
  86         BreakpointEvent bpe = startToMain("EnumTarg");
  87         targetClass = bpe.location().declaringType();
  88 
  89         ReferenceType rt = findReferenceType("EnumTarg");
  90         Field myField = rt.fieldByName("myCoin");
  91         ObjectReference enumObject = (ObjectReference)rt.getValue(myField);
  92         ClassType enumClass =(ClassType) enumObject.referenceType();
  93         ClassType superClass = enumClass.superclass();
  94         if (!superClass.name().equals("java.lang.Enum")) {
  95             fail("failure: Superclass of enum class is not java.lang.Enum: " + superClass.name());
  96         }
  97         if (!enumClass.isEnum()) {
  98             fail("failure: isEnum() is false but should be true");
  99         }
 100         if (((ClassType)rt).isEnum()) {
 101             fail("failure: isEnum() is true for EnumTarg but should be false");
 102         }
 103         Field enumConstant = enumClass.fieldByName("penny");
 104         if (!enumConstant.isEnumConstant()) {
 105             fail("failure: The 'penny' field is not marked " +
 106                  "as an enum constant.");
 107         }
 108 
 109         /*
 110          * This isn't really part of the test, it just
 111          * shows how to look at all the enum constants,
 112          * but not necessarily in the correct order
 113          */
 114         List allFields = enumClass.fields();
 115         List enumConstantFields = new ArrayList();
 116         StringBuffer enumDecl = new StringBuffer("enum " + enumClass.name() + " {");
 117         char delim = ' ';
 118 
 119         for (Iterator iter = allFields.iterator(); iter.hasNext(); ) {
 120             Field aField = (Field)iter.next();
 121             if (aField.isEnumConstant()) {
 122                 enumDecl.append(' ');
 123                 enumDecl.append(aField.name());
 124                 enumDecl.append(delim);
 125                 delim = ',';
 126             }
 127         }
 128         enumDecl.append("; };");
 129         System.out.println("Enum decl is: " + enumDecl);
 130 
 131         listenUntilVMDisconnect();
 132 
 133         /*
 134          * deal with results of test
 135          * if anything has called failure("foo") testFailed will be true
 136          */
 137         if (!testFailed) {
 138             println("EnumTest: passed");
 139         } else {
 140             throw new Exception("EnumTest: failed");
 141         }
 142     }
 143 }