0N/A#!/bin/sh
0N/A
0N/A#
2362N/A# Copyright (c) 2006, 2007, 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# @test
0N/A# @bug 5002251 6407335 6412391
0N/A# @summary Redefine a class that has an annotation and verify that the
0N/A# new annotation is returned.
0N/A#
0N/A# @run shell RedefineAnnotation.sh
0N/A
0N/AcompileOptions=-g
0N/A
0N/A# Uncomment this to see the JDI trace
0N/A#jdbOptions=-dbgtrace
0N/A
0N/AcreateJavaFile()
0N/A{
0N/A cat <<EOF > $1.java.1
0N/A
0N/Aimport java.lang.annotation.*;
0N/Aimport java.lang.reflect.*;
0N/A
0N/A/**
0N/A */
0N/A@Foo(Constants.class_annotation) // @1 commentout
0N/A// @1 uncomment @Foo(Constants.new_class_annotation)
0N/Apublic class $1 {
0N/A@Foo(Constants.field_annotation) // @1 commentout
0N/A// @1 uncomment @Foo(Constants.new_field_annotation)
0N/A public int dummy_field;
0N/A
0N/A public static void main(String[] args) {
0N/A MySubClass sub = new MySubClass();
0N/A MySubSubClass subsub = new MySubSubClass();
0N/A new $1().hi(false);
0N/A new $1().hi(true); // @1 breakpoint
0N/A sub.hi(true);
0N/A subsub.hi(true);
0N/A }
0N/A
0N/A@Foo(Constants.method_annotation) // @1 commentout
0N/A// @1 uncomment @Foo(Constants.new_method_annotation)
0N/A public void hi(
0N/A@Foo(Constants.method_parameter_annotation) // @1 commentout
0N/A// @1 uncomment @Foo(Constants.new_method_parameter_annotation)
0N/A boolean isNewVersion) {
0N/A
0N/A if (isNewVersion) {
0N/A System.out.println("Checking for NEW versions of annotations in "
0N/A + getClass());
0N/A }
0N/A
0N/A // class annotations check:
0N/A Foo foo = getClass().getAnnotation(Foo.class);
0N/A if (foo == null) {
0N/A throw new Error("FAIL: cannot get class_annotation from "
0N/A + getClass());
0N/A }
0N/A
0N/A String class_annotation = foo.value();
0N/A System.out.println("class annotation is: " + class_annotation);
0N/A if (isNewVersion) {
0N/A if (class_annotation.equals(Constants.new_class_annotation)) {
0N/A System.out.println("PASS: class_annotation was changed.");
0N/A } else {
0N/A System.out.println("FAIL: class_annotation was NOT changed.");
0N/A }
0N/A }
0N/A
0N/A // field annotations check:
0N/A try {
0N/A Field my_field = getClass().getField("dummy_field");
0N/A foo = my_field.getAnnotation(Foo.class);
0N/A if (foo == null) {
0N/A throw new Error("FAIL: cannot get field_annotation from "
0N/A + getClass() + ".dummy_field");
0N/A }
0N/A String field_annotation = foo.value();
0N/A System.out.println("field annotation is: " + field_annotation);
0N/A if (isNewVersion) {
0N/A if (field_annotation.equals(Constants.new_field_annotation)) {
0N/A System.out.println("PASS: field_annotation was changed.");
0N/A } else {
0N/A System.out.println(
0N/A "FAIL: field_annotation was NOT changed.");
0N/A }
0N/A }
0N/A } catch (NoSuchFieldException nsfe) {
0N/A throw new Error("FAIL: cannot find field 'dummy_field' in "
0N/A + getClass());
0N/A }
0N/A
0N/A // method annotations check:
0N/A try {
0N/A Class params[] = new Class[1];
0N/A params[0] = Boolean.TYPE;
0N/A Method my_method = getClass().getMethod("hi", params);
0N/A foo = my_method.getAnnotation(Foo.class);
0N/A if (foo == null) {
0N/A throw new Error("FAIL: cannot get field_annotation from "
0N/A + getClass() + ".hi()");
0N/A }
0N/A String method_annotation = foo.value();
0N/A System.out.println("method annotation is: " + method_annotation);
0N/A if (isNewVersion) {
0N/A if (method_annotation.equals(Constants.new_method_annotation)) {
0N/A System.out.println("PASS: method_annotation was changed.");
0N/A } else {
0N/A System.out.println(
0N/A "FAIL: method_annotation was NOT changed.");
0N/A }
0N/A }
0N/A } catch (NoSuchMethodException nsme) {
0N/A throw new Error("FAIL: cannot find method 'hi' in " + getClass());
0N/A }
0N/A
0N/A // method parameter annotations check:
0N/A try {
0N/A Class params[] = new Class[1];
0N/A params[0] = Boolean.TYPE;
0N/A Method my_method = getClass().getMethod("hi", params);
0N/A Annotation my_annotations[][] = my_method.getParameterAnnotations();
0N/A if (my_annotations.length != 1) {
0N/A throw new Error("FAIL: unexpected my_annotations.length ("
0N/A + my_annotations.length);
0N/A }
0N/A Annotation my_annotation[] = my_annotations[0];
0N/A if (my_annotation.length != 1) {
0N/A throw new Error("FAIL: unexpected my_annotation.length ("
0N/A + my_annotation.length);
0N/A }
0N/A foo = (Foo)my_annotation[0];
0N/A String method_parameter_annotation = foo.value();
0N/A System.out.println("method parameter annotation is: "
0N/A + method_parameter_annotation);
0N/A if (isNewVersion) {
0N/A if (method_parameter_annotation.equals(
0N/A Constants.new_method_parameter_annotation)) {
0N/A System.out.println(
0N/A "PASS: method_parameter_annotation was changed.");
0N/A } else {
0N/A System.out.println(
0N/A "FAIL: method_parameter_annotation was NOT changed.");
0N/A }
0N/A }
0N/A } catch (NoSuchMethodException nsme) {
0N/A throw new Error("FAIL: cannot find method 'hi' in " + getClass());
0N/A }
0N/A }
0N/A}
0N/A
0N/A// this subclass exists just to make the RedefineClasses() code do a
0N/A// subclass walk to update the counter
0N/Aclass MySubClass extends $1 {
0N/A int my_int_field_makes_me_different;
0N/A}
0N/A
0N/A// this subclass exists just to make the RedefineClasses() code do a
0N/A// sub-subclass walk to update the counter
0N/Aclass MySubSubClass extends MySubClass {
0N/A float my_float_field_makes_me_different;
0N/A}
0N/A
0N/Aclass Constants {
0N/A static final String class_annotation = "Patrick's class comment";
0N/A static final String new_class_annotation = "*NEW* Patrick's class comment";
0N/A
0N/A static final String field_annotation = "dummy_field comment";
0N/A static final String new_field_annotation = "*NEW* dummy_field comment";
0N/A
0N/A static final String method_annotation = "method hi() comment";
0N/A static final String new_method_annotation = "*NEW* method hi() comment";
0N/A
0N/A static final String method_parameter_annotation =
0N/A "param isNewVersion comment";
0N/A static final String new_method_parameter_annotation =
0N/A "*NEW* param isNewVersion comment";
0N/A}
0N/A
0N/A
0N/A/**
0N/A */
0N/A@Retention(RetentionPolicy.RUNTIME)
0N/A@Inherited
0N/A@interface Foo {
0N/A String value();
0N/A}
0N/A
0N/AEOF
0N/A}
0N/A
0N/A# This is called to feed cmds to jdb.
0N/AdojdbCmds()
0N/A{
0N/A setBkpts @1
0N/A runToBkpt @1
0N/A redefineClass @1
0N/A cmd cont
0N/A}
0N/A
0N/A
0N/Amysetup()
0N/A{
0N/A if [ -z "$TESTSRC" ] ; then
0N/A TESTSRC=.
0N/A fi
0N/A
0N/A for ii in . $TESTSRC $TESTSRC/.. ; do
0N/A if [ -r "$ii/ShellScaffold.sh" ] ; then
0N/A . $ii/ShellScaffold.sh
0N/A break
0N/A fi
0N/A done
0N/A}
0N/A
0N/A# You could replace this next line with the contents
0N/A# of ShellScaffold.sh and this script will run just the same.
0N/Amysetup
0N/A
0N/Arunit
0N/A
0N/AdebuggeeFailIfPresent 'FAIL:'
0N/Apass