Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/jdi/EnumTest.java
38855 views
/*1* Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 472881626* @summary JPDA: Add support for enums27*28* @author jjh29*30* @run build TestScaffold VMConnection TargetListener TargetAdapter31* @run compile -g EnumTest.java32* @run main EnumTest33*/34import com.sun.jdi.*;35import com.sun.jdi.event.*;36import com.sun.jdi.request.*;3738import java.util.*;3940/********** target program **********/414243enum Coin {44penny(1), nickel(5), dime(10), quarter(25);4546Coin(int value) { this.value = value; }4748private final int value;4950public int value() { return value; }51}5253class EnumTarg {54static Coin myCoin = Coin.penny;55public static void main(String[] args){56System.out.println("Howdy!");57System.out.println("Goodbye from EnumTarg!");58}59}6061/********** test program **********/6263public class EnumTest extends TestScaffold {64ReferenceType targetClass;6566EnumTest (String args[]) {67super(args);68}6970public static void main(String[] args) throws Exception {71new EnumTest(args).startTests();72}7374void fail(String reason) throws Exception {75failure(reason);76}7778/********** test core **********/798081protected void runTests() throws Exception {82/*83* Get to the top of main()84* to determine targetClass85*/86BreakpointEvent bpe = startToMain("EnumTarg");87targetClass = bpe.location().declaringType();8889ReferenceType rt = findReferenceType("EnumTarg");90Field myField = rt.fieldByName("myCoin");91ObjectReference enumObject = (ObjectReference)rt.getValue(myField);92ClassType enumClass =(ClassType) enumObject.referenceType();93ClassType superClass = enumClass.superclass();94if (!superClass.name().equals("java.lang.Enum")) {95fail("failure: Superclass of enum class is not java.lang.Enum: " + superClass.name());96}97if (!enumClass.isEnum()) {98fail("failure: isEnum() is false but should be true");99}100if (((ClassType)rt).isEnum()) {101fail("failure: isEnum() is true for EnumTarg but should be false");102}103Field enumConstant = enumClass.fieldByName("penny");104if (!enumConstant.isEnumConstant()) {105fail("failure: The 'penny' field is not marked " +106"as an enum constant.");107}108109/*110* This isn't really part of the test, it just111* shows how to look at all the enum constants,112* but not necessarily in the correct order113*/114List allFields = enumClass.fields();115List enumConstantFields = new ArrayList();116StringBuffer enumDecl = new StringBuffer("enum " + enumClass.name() + " {");117char delim = ' ';118119for (Iterator iter = allFields.iterator(); iter.hasNext(); ) {120Field aField = (Field)iter.next();121if (aField.isEnumConstant()) {122enumDecl.append(' ');123enumDecl.append(aField.name());124enumDecl.append(delim);125delim = ',';126}127}128enumDecl.append("; };");129System.out.println("Enum decl is: " + enumDecl);130131listenUntilVMDisconnect();132133/*134* deal with results of test135* if anything has called failure("foo") testFailed will be true136*/137if (!testFailed) {138println("EnumTest: passed");139} else {140throw new Exception("EnumTest: failed");141}142}143}144145146