Permalink
Browse files

[GR-11312] Deprecate usages of java.beans package and create new API …

…to be used instead.
  • Loading branch information...
entlicher committed Aug 16, 2018
1 parent fd0d701 commit cf79d6f2e61a8eaaa0986d2f6014b42008b6caf0
View
@@ -25,6 +25,7 @@ This changelog summarizes major changes between Truffle versions relevant to lan
* Removed APIs for deprecated packages: `com.oracle.truffle.api.vm`, `com.oracle.truffle.api.metadata`, `com.oracle.truffle.api.interop.java`
* Removed deprecated class `TruffleTCK`.
* Debugger API methods now throw [DebugException](http://www.graalvm.org/truffle/javadoc/com/oracle/truffle/api/debug/DebugException.html) on language failures.
* Deprecated API methods that use `java.beans` package in [AllocationReporter](http://www.graalvm.org/truffle/javadoc/com/oracle/truffle/api/instrumentation/AllocationReporter.html) and [Debugger](http://www.graalvm.org/truffle/javadoc/com/oracle/truffle/api/debug/Debugger.html). New add/remove listener methods were introduced as a replacement.
## Version 1.0.0 RC5
@@ -24,39 +24,34 @@
*/
package com.oracle.truffle.api.debug.test;
import java.util.function.Consumer;
import org.junit.Assert;
import com.oracle.truffle.api.debug.Breakpoint;
import com.oracle.truffle.api.debug.Debugger;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import org.junit.Assert;
final class BreakpointDisposeListener implements PropertyChangeListener {
final class BreakpointDisposeListener implements Consumer<Breakpoint> {
static void register(boolean[] notified, Debugger debugger, Breakpoint globalBreakpoint) {
PropertyChangeListener disposeBPListener = new BreakpointDisposeListener(notified, debugger, globalBreakpoint);
debugger.addPropertyChangeListener(disposeBPListener);
Consumer<Breakpoint> disposeBPListener = new BreakpointDisposeListener(notified, globalBreakpoint);
debugger.addBreakpointAddedListener((breakpoint) -> Assert.fail("No new breakpoint is excpected to be added. Breakpoint = " + breakpoint));
debugger.addBreakpointRemovedListener(disposeBPListener);
}
private final boolean[] notified;
private final Debugger debugger;
private final Breakpoint globalBreakpoint;
private BreakpointDisposeListener(boolean[] notified, Debugger debugger, Breakpoint globalBreakpoint) {
private BreakpointDisposeListener(boolean[] notified, Breakpoint globalBreakpoint) {
this.notified = notified;
this.debugger = debugger;
this.globalBreakpoint = globalBreakpoint;
}
@Override
public void propertyChange(PropertyChangeEvent event) {
public void accept(Breakpoint breakpoint) {
notified[0] = true;
Assert.assertEquals(Debugger.PROPERTY_BREAKPOINTS, event.getPropertyName());
Assert.assertEquals(debugger, event.getSource());
Assert.assertNull(event.getNewValue());
Assert.assertNotEquals(globalBreakpoint, event.getOldValue());
Breakpoint oldBP = (Breakpoint) event.getOldValue();
Assert.assertNotEquals(globalBreakpoint, breakpoint);
try {
oldBP.dispose();
breakpoint.dispose();
Assert.fail("Public dispose must not be possible for global breakpoints.");
} catch (IllegalStateException ex) {
// O.K.
@@ -24,23 +24,27 @@
*/
package com.oracle.truffle.api.debug.test;
import java.util.function.Consumer;
import org.junit.Assert;
import com.oracle.truffle.api.debug.Breakpoint;
import com.oracle.truffle.api.debug.Debugger;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import org.junit.Assert;
final class BreakpointListener implements PropertyChangeListener {
final class BreakpointListener implements Consumer<Breakpoint> {
static BreakpointListener register(boolean[] notified, Debugger debugger, Breakpoint globalBreakpoint) {
BreakpointListener newBPListener = new BreakpointListener(notified, debugger, globalBreakpoint);
debugger.addPropertyChangeListener(newBPListener);
debugger.addBreakpointAddedListener(newBPListener);
Consumer<Breakpoint> removedListener = (breakpoint) -> Assert.fail("No breakpoint is excpected to be removed. Breakpoint = " + breakpoint);
newBPListener.removedListener = removedListener;
debugger.addBreakpointRemovedListener(removedListener);
return newBPListener;
}
private final boolean[] notified;
private final Debugger debugger;
private final Breakpoint globalBreakpoint;
private Consumer<Breakpoint> removedListener;
private BreakpointListener(boolean[] notified, Debugger debugger, Breakpoint globalBreakpoint) {
this.notified = notified;
@@ -49,23 +53,20 @@ private BreakpointListener(boolean[] notified, Debugger debugger, Breakpoint glo
}
@Override
public void propertyChange(PropertyChangeEvent event) {
public void accept(Breakpoint breakpoint) {
notified[0] = true;
Assert.assertEquals(Debugger.PROPERTY_BREAKPOINTS, event.getPropertyName());
Assert.assertEquals(debugger, event.getSource());
Assert.assertNull(event.getOldValue());
Assert.assertNotEquals(globalBreakpoint, event.getNewValue());
Breakpoint newBP = (Breakpoint) event.getNewValue();
Assert.assertNotEquals(globalBreakpoint, breakpoint);
try {
newBP.dispose();
breakpoint.dispose();
Assert.fail("Public dispose must not be possible for global breakpoints.");
} catch (IllegalStateException ex) {
// O.K.
}
}
void unregister() {
debugger.removePropertyChangeListener(this);
debugger.removeBreakpointAddedListener(this);
debugger.removeBreakpointRemovedListener(removedListener);
}
}
@@ -772,13 +772,6 @@ public void testInactive() throws Throwable {
@SuppressWarnings("try") // auto-closeable resource session is never referenced in body of
// corresponding try statement
public void testGlobalBreakpoints() throws Throwable {
try {
Class.forName("java.beans.PropertyChangeListener");
} catch (ClassNotFoundException ex) {
// skip the test if running only with java.base JDK9 module
return;
}
final Source source = testSource("ROOT(\n" +
" STATEMENT,\n" +
" STATEMENT\n" +
@@ -875,13 +868,6 @@ public void testGlobalBreakpoints() throws Throwable {
@Test
public void testGlobalBreakpointsInMultipleSessions() throws Throwable {
try {
Class.forName("java.beans.PropertyChangeListener");
} catch (ClassNotFoundException ex) {
// skip the test if running only with java.base JDK9 module
return;
}
final Source source = testSource("ROOT(\n" +
" STATEMENT,\n" +
" STATEMENT,\n" +
@@ -44,6 +44,7 @@ static void firePropertyChange(Debugger source, Breakpoint oldBreakpoint, Breakp
}
}
@SuppressWarnings("deprecation")
private BreakpointsPropertyChangeEvent(Object source, Breakpoint oldBreakpoint, Breakpoint newBreakpoint) {
super(source, Debugger.PROPERTY_BREAKPOINTS, oldBreakpoint, newBreakpoint);
}
@@ -31,6 +31,7 @@
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
import org.graalvm.polyglot.Engine;
@@ -82,12 +83,16 @@
* @since 0.27
* @see #getBreakpoints()
* @see #addPropertyChangeListener(java.beans.PropertyChangeListener)
* @deprecated Use {@link #addBreakpointAddedListener(Consumer)} and
* {@link #addBreakpointRemovedListener(Consumer)}
*/
public static final String PROPERTY_BREAKPOINTS = "breakpoints";
@Deprecated public static final String PROPERTY_BREAKPOINTS = "breakpoints";
static final boolean TRACE = Boolean.getBoolean("truffle.debug.trace");
private final Env env;
final List<Object> propSupport = new CopyOnWriteArrayList<>();
private final List<Consumer<Breakpoint>> breakpointAddedListeners = new CopyOnWriteArrayList<>();
private final List<Consumer<Breakpoint>> breakpointRemovedListeners = new CopyOnWriteArrayList<>();
final ObjectStructures.MessageNodes msgNodes;
private final Set<DebuggerSession> sessions = new HashSet<>();
private final List<Breakpoint> breakpoints = new ArrayList<>();
@@ -183,6 +188,9 @@ public Breakpoint install(Breakpoint breakpoint) {
s.install(breakpoint, true);
}
BreakpointsPropertyChangeEvent.firePropertyChange(this, null, breakpoint);
for (Consumer<Breakpoint> listener : breakpointAddedListeners) {
listener.accept(breakpoint.getROWrapper());
}
if (Debugger.TRACE) {
trace("installed debugger breakpoint %s", breakpoint);
}
@@ -195,11 +203,11 @@ public Breakpoint install(Breakpoint breakpoint) {
* snapshot of breakpoints, those that were {@link Breakpoint#dispose() disposed} are not
* included.
* <p>
* It's not possible to modify state of breakpoints returned from this list, or from the
* associated property change events, they are not {@link Breakpoint#isModifiable() modifiable}.
* An attempt to modify breakpoints state using any of their set method, or an attempt to
* dispose such breakpoints, fails with an {@link IllegalStateException}. Use the original
* installed breakpoint instance to change breakpoint state or dispose the breakpoint.
* It's not possible to modify state of breakpoints returned from this list, or from methods on
* listeners, they are not {@link Breakpoint#isModifiable() modifiable}. An attempt to modify
* breakpoints state using any of their set method, or an attempt to dispose such breakpoints,
* fails with an {@link IllegalStateException}. Use the original installed breakpoint instance
* to change breakpoint state or dispose the breakpoint.
*
* @since 0.27
* @see DebuggerSession#getBreakpoints()
@@ -229,6 +237,9 @@ void disposeBreakpoint(Breakpoint breakpoint) {
}
if (removed) {
BreakpointsPropertyChangeEvent.firePropertyChange(this, breakpoint, null);
for (Consumer<Breakpoint> listener : breakpointRemovedListeners) {
listener.accept(breakpoint.getROWrapper());
}
}
if (Debugger.TRACE) {
trace("disposed debugger breakpoint %s", breakpoint);
@@ -255,12 +266,55 @@ public void onLoad(LoadSourceEvent event) {
return Collections.unmodifiableList(sources);
}
/**
* Add a listener that is notified when a new breakpoint is added into {@link #getBreakpoints()
* list of breakpoints}. The reported breakpoint is not {@link Breakpoint#isModifiable()
* modifiable}.
*
* @since 1.0
*/
public void addBreakpointAddedListener(Consumer<Breakpoint> listener) {
breakpointAddedListeners.add(listener);
}
/**
* Remove a listener that was added by {@link #addBreakpointAddedListener(Consumer)}.
*
* @since 1.0
*/
public void removeBreakpointAddedListener(Consumer<Breakpoint> listener) {
breakpointAddedListeners.remove(listener);
}
/**
* Add a listener that is notified when a breakpoint is removed from {@link #getBreakpoints()
* list of breakpoints}. The reported breakpoint is not {@link Breakpoint#isModifiable()
* modifiable}.
*
* @since 1.0
*/
public void addBreakpointRemovedListener(Consumer<Breakpoint> listener) {
breakpointRemovedListeners.add(listener);
}
/**
* Remove a listener that was added by {@link #addBreakpointRemovedListener(Consumer)}.
*
* @since 1.0
*/
public void removeBreakpointRemovedListener(Consumer<Breakpoint> listener) {
breakpointRemovedListeners.remove(listener);
}
/**
* Add a property change listener that is notified when a property of this debugger changes.
*
* @since 0.27
* @see #PROPERTY_BREAKPOINTS
* @deprecated Use {@link #addBreakpointAddedListener(Consumer)} and
* {@link #addBreakpointRemovedListener(Consumer)}
*/
@Deprecated
public void addPropertyChangeListener(java.beans.PropertyChangeListener listener) {
// using FQN to avoid mx to generate dependency on java.desktop module
propSupport.add(listener);
@@ -271,7 +325,10 @@ public void addPropertyChangeListener(java.beans.PropertyChangeListener listener
*
* @since 0.27
* @see #addPropertyChangeListener(java.beans.PropertyChangeListener)
* @deprecated Use {@link #removeBreakpointAddedListener(Consumer)} and
* {@link #removeBreakpointRemovedListener(Consumer)}
*/
@Deprecated
public void removePropertyChangeListener(java.beans.PropertyChangeListener listener) {
// using FQN to avoid mx to generate dependency on java.desktop module
propSupport.remove(listener);
@@ -24,13 +24,13 @@
*/
package com.oracle.truffle.api.instrumentation.test;
import com.oracle.truffle.api.instrumentation.AllocationReporter;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import org.junit.Assert;
final class AllocationDeactivatedListener implements PropertyChangeListener {
import com.oracle.truffle.api.instrumentation.AllocationReporter;
final class AllocationDeactivatedListener implements Consumer<Boolean> {
private final AtomicInteger listenerCalls;
private final AllocationReporter source;
@@ -41,21 +41,19 @@ private AllocationDeactivatedListener(AllocationReporter source, AtomicInteger l
}
@Override
public void propertyChange(PropertyChangeEvent event) {
Assert.assertEquals(AllocationReporter.PROPERTY_ACTIVE, event.getPropertyName());
Assert.assertEquals(Boolean.TRUE, event.getOldValue());
Assert.assertEquals(Boolean.FALSE, event.getNewValue());
public void accept(Boolean active) {
Assert.assertEquals(false, active);
listenerCalls.incrementAndGet();
}
static AllocationDeactivatedListener register(AtomicInteger listenerCalls, AllocationReporter reporter) {
AllocationDeactivatedListener deactivatedListener = new AllocationDeactivatedListener(reporter, listenerCalls);
reporter.addPropertyChangeListener(deactivatedListener);
reporter.addActiveListener(deactivatedListener);
return deactivatedListener;
}
void unregister() {
source.removePropertyChangeListener(this);
source.removeActiveListener(this);
}
@@ -24,13 +24,13 @@
*/
package com.oracle.truffle.api.instrumentation.test;
import com.oracle.truffle.api.instrumentation.AllocationReporter;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import org.junit.Assert;
final class AllocationReporterListener implements PropertyChangeListener {
import com.oracle.truffle.api.instrumentation.AllocationReporter;
final class AllocationReporterListener implements Consumer<Boolean> {
private final AtomicInteger listenerCalls;
private final AllocationReporter source;
@@ -41,20 +41,18 @@ private AllocationReporterListener(AllocationReporter source, AtomicInteger list
}
@Override
public void propertyChange(PropertyChangeEvent event) {
Assert.assertEquals(AllocationReporter.PROPERTY_ACTIVE, event.getPropertyName());
Assert.assertEquals(Boolean.FALSE, event.getOldValue());
Assert.assertEquals(Boolean.TRUE, event.getNewValue());
public void accept(Boolean active) {
Assert.assertEquals(true, active);
listenerCalls.incrementAndGet();
}
static AllocationReporterListener register(AtomicInteger listenerCalls, AllocationReporter reporter) {
AllocationReporterListener activatedListener = new AllocationReporterListener(reporter, listenerCalls);
reporter.addPropertyChangeListener(activatedListener);
reporter.addActiveListener(activatedListener);
return activatedListener;
}
void unregister() {
source.removePropertyChangeListener(this);
source.removeActiveListener(this);
}
}
@@ -559,13 +559,6 @@ public void testUnregister() {
@Test
public void testReporterChangeListener() {
try {
Class.forName("java.beans.PropertyChangeListener");
} catch (ClassNotFoundException ex) {
// skip the test if running only with java.base JDK9 module
return;
}
// Test of AllocationReporter property change listener notifications
allocation.setEnabled(false);
Source source = Source.create(AllocationReporterLanguage.ID, "NEW");
Oops, something went wrong.

0 comments on commit cf79d6f

Please sign in to comment.