0N/A/*
2362N/A * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4728816
0N/A * @summary JPDA: Add support for enums
0N/A *
0N/A * @author jjh
0N/A *
0N/A * @run build TestScaffold VMConnection TargetListener TargetAdapter
1485N/A * @run compile -g EnumTest.java
0N/A * @run main EnumTest
0N/A */
0N/Aimport com.sun.jdi.*;
0N/Aimport com.sun.jdi.event.*;
0N/Aimport com.sun.jdi.request.*;
0N/A
0N/Aimport java.util.*;
0N/A
0N/A /********** target program **********/
0N/A
0N/A
0N/Aenum Coin {
0N/A penny(1), nickel(5), dime(10), quarter(25);
0N/A
0N/A Coin(int value) { this.value = value; }
0N/A
0N/A private final int value;
0N/A
0N/A public int value() { return value; }
0N/A}
0N/A
0N/Aclass EnumTarg {
0N/A static Coin myCoin = Coin.penny;
0N/A public static void main(String[] args){
0N/A System.out.println("Howdy!");
0N/A System.out.println("Goodbye from EnumTarg!");
0N/A }
0N/A}
0N/A
0N/A /********** test program **********/
0N/A
0N/Apublic class EnumTest extends TestScaffold {
0N/A ReferenceType targetClass;
0N/A
0N/A EnumTest (String args[]) {
0N/A super(args);
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A new EnumTest(args).startTests();
0N/A }
0N/A
0N/A void fail(String reason) throws Exception {
0N/A failure(reason);
0N/A }
0N/A
0N/A /********** test core **********/
0N/A
0N/A
0N/A protected void runTests() throws Exception {
0N/A /*
0N/A * Get to the top of main()
0N/A * to determine targetClass
0N/A */
0N/A BreakpointEvent bpe = startToMain("EnumTarg");
0N/A targetClass = bpe.location().declaringType();
0N/A
0N/A ReferenceType rt = findReferenceType("EnumTarg");
0N/A Field myField = rt.fieldByName("myCoin");
0N/A ObjectReference enumObject = (ObjectReference)rt.getValue(myField);
0N/A ClassType enumClass =(ClassType) enumObject.referenceType();
0N/A ClassType superClass = enumClass.superclass();
0N/A if (!superClass.name().equals("java.lang.Enum")) {
0N/A fail("failure: Superclass of enum class is not java.lang.Enum: " + superClass.name());
0N/A }
0N/A if (!enumClass.isEnum()) {
0N/A fail("failure: isEnum() is false but should be true");
0N/A }
0N/A if (((ClassType)rt).isEnum()) {
0N/A fail("failure: isEnum() is true for EnumTarg but should be false");
0N/A }
0N/A Field enumConstant = enumClass.fieldByName("penny");
0N/A if (!enumConstant.isEnumConstant()) {
0N/A fail("failure: The 'penny' field is not marked " +
0N/A "as an enum constant.");
0N/A }
0N/A
0N/A /*
0N/A * This isn't really part of the test, it just
0N/A * shows how to look at all the enum constants,
0N/A * but not necessarily in the correct order
0N/A */
0N/A List allFields = enumClass.fields();
0N/A List enumConstantFields = new ArrayList();
0N/A StringBuffer enumDecl = new StringBuffer("enum " + enumClass.name() + " {");
0N/A char delim = ' ';
0N/A
0N/A for (Iterator iter = allFields.iterator(); iter.hasNext(); ) {
0N/A Field aField = (Field)iter.next();
0N/A if (aField.isEnumConstant()) {
0N/A enumDecl.append(' ');
0N/A enumDecl.append(aField.name());
0N/A enumDecl.append(delim);
0N/A delim = ',';
0N/A }
0N/A }
0N/A enumDecl.append("; };");
0N/A System.out.println("Enum decl is: " + enumDecl);
0N/A
0N/A listenUntilVMDisconnect();
0N/A
0N/A /*
0N/A * deal with results of test
0N/A * if anything has called failure("foo") testFailed will be true
0N/A */
0N/A if (!testFailed) {
0N/A println("EnumTest: passed");
0N/A } else {
0N/A throw new Exception("EnumTest: failed");
0N/A }
0N/A }
0N/A}