Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/FieldAccessWatch/FieldAccessWatch.java
41153 views
/*1* Copyright (c) 2018, 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 819336926* @summary Tests that all FieldAccess and FieldModification notifications27are generated.28* @requires vm.jvmti29* @compile FieldAccessWatch.java30* @run main/othervm/native -agentlib:FieldAccessWatch FieldAccessWatch31*/3233import java.lang.reflect.Field;34import java.util.ArrayList;35import java.util.Arrays;36import java.util.List;373839public class FieldAccessWatch {4041private static final String agentLib = "FieldAccessWatch";4243private static class MyItem {44}4546private static class MyList {47public List<MyItem> items = new ArrayList<>();48}4950public static void main(String[] args) throws Exception {51try {52System.loadLibrary(agentLib);53} catch (UnsatisfiedLinkError ex) {54System.err.println("Failed to load " + agentLib + " lib");55System.err.println("java.library.path: " + System.getProperty("java.library.path"));56throw ex;57}5859if (!initWatchers(MyList.class, MyList.class.getDeclaredField("items"))) {60throw new RuntimeException("Watchers initializations error");61}6263MyList list = new MyList();6465test("[1]items.add(0, object)",() -> list.items.add(0, new MyItem()));66test("[2]items.add(object)", () -> list.items.add(new MyItem()));67test("[3]items.add(1, object)", () -> list.items.add(1, new MyItem()));68test("[4]items.add(object)", () -> list.items.add(new MyItem()));69test("[5]items.add(1, object)", () -> list.items.add(1, new MyItem()));70}7172private static void log(String msg) {73System.out.println(msg);74System.out.flush();75}7677// For every access/modify notification native part tries to locate78// boolean "<field_name>_access"/"<field_name>_modify" field and sets it to true79private static class TestResult {80// MyList.items81public boolean items_access;8283// AbstractList.modCount84public boolean modCount_access;85public boolean modCount_modify;8687// ArrayList.size88public boolean size_access;89public boolean size_modify;9091// ArrayList.elementData92public boolean elementData_access;9394// verify that all fields are set to true95public void verify() {96Arrays.stream(this.getClass().getDeclaredFields()).forEach(f -> verify(f));97}9899private void verify(Field f) {100try {101if (!f.getBoolean(this)) {102throw new RuntimeException(f.getName() + " notification is missed");103}104} catch (IllegalAccessException ex) {105throw new RuntimeException(ex);106}107}108}109110@FunctionalInterface111private interface TestAction {112void apply();113}114115private static void test(String descr, TestAction action) throws Exception {116log(descr + ": starting");117TestResult result = new TestResult();118if (!startTest(result)) {119throw new RuntimeException("startTest failed");120}121action.apply();122// wait some time to ensure all posted events are handled123Thread.sleep(500);124125stopTest();126127// check the results128result.verify();129130log(descr + ": OK");131}132133private static native boolean initWatchers(Class cls, Field field);134private static native boolean startTest(TestResult results);135private static native void stopTest();136137}138139140