From a0a9595d8bd4a8d9946caa6148fd5dff04b47adb Mon Sep 17 00:00:00 2001 From: Sergey Bylokhov Date: Sun, 26 Apr 2020 18:49:18 -0700 Subject: [PATCH 1/3] 8236980: Cleanup of toString methods in JavaSound Reviewed-by: prr --- .../classes/javax/sound/midi/MidiDevice.java | 6 +- .../classes/javax/sound/midi/Sequencer.java | 9 +- .../javax/sound/sampled/AudioFileFormat.java | 37 +++----- .../javax/sound/sampled/AudioFormat.java | 91 ++++++------------- .../javax/sound/sampled/BooleanControl.java | 9 +- .../javax/sound/sampled/CompoundControl.java | 25 ++--- .../classes/javax/sound/sampled/Control.java | 11 +-- .../classes/javax/sound/sampled/DataLine.java | 37 ++++---- .../javax/sound/sampled/EnumControl.java | 9 +- .../javax/sound/sampled/FloatControl.java | 11 ++- .../classes/javax/sound/sampled/Line.java | 6 +- .../javax/sound/sampled/LineEvent.java | 21 ++--- .../classes/javax/sound/sampled/Mixer.java | 8 +- .../classes/javax/sound/sampled/Port.java | 9 +- .../javax/sound/sampled/ReverbType.java | 26 +++--- .../javax/sound/midi/Devices/ToString.java | 38 ++++++++ .../javax/sound/midi/Sequencer/ToString.java | 38 ++++++++ .../sampled/AudioFileFormat/ToString.java | 39 ++++++++ .../sound/sampled/AudioFormat/ToString.java | 38 ++++++++ .../sound/sampled/Controls/ToString.java | 38 ++++++++ .../sound/sampled/LineEvent/ToString.java | 38 ++++++++ .../sound/sampled/ReverbType/ToString.java | 38 ++++++++ 22 files changed, 397 insertions(+), 185 deletions(-) create mode 100644 test/jdk/javax/sound/midi/Devices/ToString.java create mode 100644 test/jdk/javax/sound/midi/Sequencer/ToString.java create mode 100644 test/jdk/javax/sound/sampled/AudioFileFormat/ToString.java create mode 100644 test/jdk/javax/sound/sampled/AudioFormat/ToString.java create mode 100644 test/jdk/javax/sound/sampled/Controls/ToString.java create mode 100644 test/jdk/javax/sound/sampled/LineEvent/ToString.java create mode 100644 test/jdk/javax/sound/sampled/ReverbType/ToString.java diff --git a/src/java.desktop/share/classes/javax/sound/midi/MidiDevice.java b/src/java.desktop/share/classes/javax/sound/midi/MidiDevice.java index 0c3246aabc5..335ff97a921 100644 --- a/src/java.desktop/share/classes/javax/sound/midi/MidiDevice.java +++ b/src/java.desktop/share/classes/javax/sound/midi/MidiDevice.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -350,9 +350,9 @@ public final String getVersion() { } /** - * Provides a string representation of the device information. + * Returns a string representation of the info object. * - * @return a description of the info object + * @return a string representation of the info object */ @Override public final String toString() { diff --git a/src/java.desktop/share/classes/javax/sound/midi/Sequencer.java b/src/java.desktop/share/classes/javax/sound/midi/Sequencer.java index 90e5248a3ad..4ee2cbcfd5a 100644 --- a/src/java.desktop/share/classes/javax/sound/midi/Sequencer.java +++ b/src/java.desktop/share/classes/javax/sound/midi/Sequencer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -720,14 +720,13 @@ public final int hashCode() { } /** - * Provides this synchronization mode's name as the string - * representation of the mode. + * Returns mode's name as the string representation of the + * synchronization mode. * - * @return the name of this synchronization mode + * @return a string representation of the synchronization mode */ @Override public final String toString() { - return name; } diff --git a/src/java.desktop/share/classes/javax/sound/sampled/AudioFileFormat.java b/src/java.desktop/share/classes/javax/sound/sampled/AudioFileFormat.java index 38046059727..03e4d0c882f 100644 --- a/src/java.desktop/share/classes/javax/sound/sampled/AudioFileFormat.java +++ b/src/java.desktop/share/classes/javax/sound/sampled/AudioFileFormat.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -262,33 +262,25 @@ public Object getProperty(String key) { } /** - * Provides a string representation of the file format. + * Returns a string representation of the audio file format. * - * @return the file format as a string + * @return a string representation of the audio file format */ @Override public String toString() { - - StringBuffer buf = new StringBuffer(); - + String str = "Unknown file format"; //$$fb2002-11-01: fix for 4672864: AudioFileFormat.toString() throws unexpected NullPointerException - if (type != null) { - buf.append(type.toString() + " (." + type.getExtension() + ") file"); - } else { - buf.append("unknown file format"); + if (getType() != null) { + str = getType() + " (." + getType().getExtension() + ") file"; } - - if (byteLength != AudioSystem.NOT_SPECIFIED) { - buf.append(", byte length: " + byteLength); + if (getByteLength() != AudioSystem.NOT_SPECIFIED) { + str += ", byte length: " + getByteLength(); } - - buf.append(", data format: " + format); - - if (frameLength != AudioSystem.NOT_SPECIFIED) { - buf.append(", frame length: " + frameLength); + str += ", data format: " + getFormat(); + if (getFrameLength() != AudioSystem.NOT_SPECIFIED) { + str += ", frame length: " + getFrameLength(); } - - return new String(buf); + return str; } /** @@ -376,10 +368,9 @@ public final int hashCode() { } /** - * Provides the file type's name as the {@code String} representation of - * the file type. + * Returns type's name as the string representation of the file type. * - * @return the file type's name + * @return a string representation of the file type */ @Override public final String toString() { diff --git a/src/java.desktop/share/classes/javax/sound/sampled/AudioFormat.java b/src/java.desktop/share/classes/javax/sound/sampled/AudioFormat.java index 720c13be633..9331861dede 100644 --- a/src/java.desktop/share/classes/javax/sound/sampled/AudioFormat.java +++ b/src/java.desktop/share/classes/javax/sound/sampled/AudioFormat.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -448,83 +448,47 @@ public boolean matches(AudioFormat format) { } /** - * Returns a string that describes the format, such as: "PCM SIGNED 22050 Hz - * 16 bit mono big-endian". The contents of the string may vary between - * implementations of Java Sound. + * Returns a string that describes the audio format, such as: "PCM SIGNED + * 22050 Hz 16 bit mono big-endian". The contents of the string may vary + * between implementations of Java Sound. * - * @return a string that describes the format parameters + * @return a string representation of the audio format */ @Override public String toString() { - String sEncoding = ""; - if (getEncoding() != null) { - sEncoding = getEncoding().toString() + " "; - } - - String sSampleRate; - if (getSampleRate() == (float) AudioSystem.NOT_SPECIFIED) { - sSampleRate = "unknown sample rate, "; - } else { - sSampleRate = "" + getSampleRate() + " Hz, "; - } + String sampleRate = getSampleRate() == AudioSystem.NOT_SPECIFIED ? + "unknown sample rate" : getSampleRate() + " Hz"; - String sSampleSizeInBits; - if (getSampleSizeInBits() == (float) AudioSystem.NOT_SPECIFIED) { - sSampleSizeInBits = "unknown bits per sample, "; - } else { - sSampleSizeInBits = "" + getSampleSizeInBits() + " bit, "; - } + String sampleSize = getSampleSizeInBits() == AudioSystem.NOT_SPECIFIED ? + "unknown bits per sample" : getSampleSizeInBits() + " bit"; - String sChannels; - if (getChannels() == 1) { - sChannels = "mono, "; - } else - if (getChannels() == 2) { - sChannels = "stereo, "; - } else { - if (getChannels() == AudioSystem.NOT_SPECIFIED) { - sChannels = " unknown number of channels, "; - } else { - sChannels = ""+getChannels()+" channels, "; - } - } + String channels = switch (getChannels()) { + case 1 -> "mono"; + case 2 -> "stereo"; + case AudioSystem.NOT_SPECIFIED -> "unknown number of channels"; + default -> getChannels() + " channels"; + }; - String sFrameSize; - if (getFrameSize() == (float) AudioSystem.NOT_SPECIFIED) { - sFrameSize = "unknown frame size, "; - } else { - sFrameSize = "" + getFrameSize()+ " bytes/frame, "; - } + String frameSize = getFrameSize() == AudioSystem.NOT_SPECIFIED ? + "unknown frame size" : getFrameSize() + " bytes/frame"; - String sFrameRate = ""; + String frameRate = ""; if (Math.abs(getSampleRate() - getFrameRate()) > 0.00001) { - if (getFrameRate() == (float) AudioSystem.NOT_SPECIFIED) { - sFrameRate = "unknown frame rate, "; - } else { - sFrameRate = getFrameRate() + " frames/second, "; - } + frameRate = getFrameRate() == AudioSystem.NOT_SPECIFIED ? + ", unknown frame rate":", " + getFrameRate() + " frames/second"; } - String sEndian = ""; + String bigEndian = ""; if ((getEncoding().equals(Encoding.PCM_SIGNED) || getEncoding().equals(Encoding.PCM_UNSIGNED)) && ((getSampleSizeInBits() > 8) || (getSampleSizeInBits() == AudioSystem.NOT_SPECIFIED))) { - if (isBigEndian()) { - sEndian = "big-endian"; - } else { - sEndian = "little-endian"; - } + bigEndian = isBigEndian() ? ", big-endian" : ", little-endian"; } - return sEncoding - + sSampleRate - + sSampleSizeInBits - + sChannels - + sFrameSize - + sFrameRate - + sEndian; - + return String.format("%s %s, %s, %s, %s%s%s", getEncoding(), + sampleRate, sampleSize, channels, frameSize, + frameRate, bigEndian); } /** @@ -630,13 +594,12 @@ public final int hashCode() { } /** - * Provides the {@code String} representation of the encoding. This - * {@code String} is the same name that was passed to the constructor. + * Returns encoding's name as the string representation of the encoding. * For the predefined encodings, the name is similar to the encoding's * variable (field) name. For example, {@code PCM_SIGNED.toString()} * returns the name "PCM_SIGNED". * - * @return the encoding name + * @return a string representation of the encoding */ @Override public final String toString() { diff --git a/src/java.desktop/share/classes/javax/sound/sampled/BooleanControl.java b/src/java.desktop/share/classes/javax/sound/sampled/BooleanControl.java index ede71800428..816fd8bd7f7 100644 --- a/src/java.desktop/share/classes/javax/sound/sampled/BooleanControl.java +++ b/src/java.desktop/share/classes/javax/sound/sampled/BooleanControl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -119,13 +119,14 @@ public String getStateLabel(boolean state) { } /** - * Provides a string representation of the control. + * Returns a string representation of the boolean control. * - * @return a string representation of the control + * @return a string representation of the boolean control */ @Override public String toString() { - return new String(super.toString() + " with current value: " + getStateLabel(getValue())); + return String.format("%s with current value: %s", super.toString(), + getStateLabel(getValue())); } /** diff --git a/src/java.desktop/share/classes/javax/sound/sampled/CompoundControl.java b/src/java.desktop/share/classes/javax/sound/sampled/CompoundControl.java index 1f460c56106..33e80998137 100644 --- a/src/java.desktop/share/classes/javax/sound/sampled/CompoundControl.java +++ b/src/java.desktop/share/classes/javax/sound/sampled/CompoundControl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,6 +25,8 @@ package javax.sound.sampled; +import java.util.StringJoiner; + /** * A {@code CompoundControl}, such as a graphic equalizer, provides control over * two or more related properties, each of which is itself represented as a @@ -61,25 +63,18 @@ public Control[] getMemberControls() { } /** - * Provides a string representation of the control. + * Returns a string representation of the compound control. * - * @return a string description + * @return a string representation of the compound control */ @Override public String toString() { - - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < controls.length; i++) { - if (i != 0) { - sb.append(", "); - if ((i + 1) == controls.length) { - sb.append("and "); - } - } - sb.append(controls[i].getType()); + StringJoiner controls = new StringJoiner(", ", "[", "]"); + for (Control control : getMemberControls()) { + controls.add(control.getType().toString()); } - - return new String(getType() + " Control containing " + sb + " Controls."); + return String.format("%s containing %s controls", super.toString(), + controls); } /** diff --git a/src/java.desktop/share/classes/javax/sound/sampled/Control.java b/src/java.desktop/share/classes/javax/sound/sampled/Control.java index bdf3da5c73d..a1c5e894618 100644 --- a/src/java.desktop/share/classes/javax/sound/sampled/Control.java +++ b/src/java.desktop/share/classes/javax/sound/sampled/Control.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -66,13 +66,13 @@ public Type getType() { } /** - * Obtains a string describing the control type and its current state. + * Returns a string representation of the control. * * @return a string representation of the control */ @Override public String toString() { - return new String(getType() + " Control"); + return String.format("%s control", getType()); } /** @@ -120,10 +120,9 @@ public final int hashCode() { } /** - * Provides the {@code String} representation of the control type. This - * {@code String} is the same name that was passed to the constructor. + * Returns type's name as the string representation of the control type. * - * @return the control type name + * @return a string representation of the control type */ @Override public final String toString() { diff --git a/src/java.desktop/share/classes/javax/sound/sampled/DataLine.java b/src/java.desktop/share/classes/javax/sound/sampled/DataLine.java index c036f7a51c5..2dba30f5080 100644 --- a/src/java.desktop/share/classes/javax/sound/sampled/DataLine.java +++ b/src/java.desktop/share/classes/javax/sound/sampled/DataLine.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -475,30 +475,33 @@ public boolean matches(Line.Info info) { } /** - * Obtains a textual description of the data line info. + * Returns a string representation of the info object. * - * @return a string description + * @return a string representation of the info object */ @Override public String toString() { - - StringBuilder sb = new StringBuilder(); - - if ( (formats.length == 1) && (formats[0] != null) ) { - sb.append(" supporting format " + formats[0]); - } else if (getFormats().length > 1) { - sb.append(" supporting " + getFormats().length + " audio formats"); + String format = ""; + AudioFormat[] formats = getFormats(); + if (formats.length == 1 && formats[0] != null) { + format = " supporting format " + formats[0]; + } else if (formats.length > 1) { + format = " supporting " + formats.length + " audio formats"; } - if ( (minBufferSize != AudioSystem.NOT_SPECIFIED) && (maxBufferSize != AudioSystem.NOT_SPECIFIED) ) { - sb.append(", and buffers of " + minBufferSize + " to " + maxBufferSize + " bytes"); - } else if ( (minBufferSize != AudioSystem.NOT_SPECIFIED) && (minBufferSize > 0) ) { - sb.append(", and buffers of at least " + minBufferSize + " bytes"); - } else if (maxBufferSize != AudioSystem.NOT_SPECIFIED) { - sb.append(", and buffers of up to " + minBufferSize + " bytes"); + String buffers = ""; + int min = getMinBufferSize(); + int max = getMaxBufferSize(); + if (min != AudioSystem.NOT_SPECIFIED + && max != AudioSystem.NOT_SPECIFIED) { + buffers = ", and buffers of " + min + " to " + max + " bytes"; + } else if (min > 0) { + buffers = ", and buffers of at least " + min + " bytes"; + } else if (max != AudioSystem.NOT_SPECIFIED) { + buffers = ", and buffers of up to " + max + " bytes"; } - return new String(super.toString() + sb); + return String.format("%s%s%s", super.toString(), format, buffers); } } } diff --git a/src/java.desktop/share/classes/javax/sound/sampled/EnumControl.java b/src/java.desktop/share/classes/javax/sound/sampled/EnumControl.java index 6e553962f0a..d01aa2fa316 100644 --- a/src/java.desktop/share/classes/javax/sound/sampled/EnumControl.java +++ b/src/java.desktop/share/classes/javax/sound/sampled/EnumControl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -130,13 +130,14 @@ private boolean isValueSupported(Object value) { } /** - * Provides a string representation of the control. + * Returns a string representation of the enumerated control. * - * @return a string description + * @return a string representation of the enumerated control */ @Override public String toString() { - return new String(getType() + " with current value: " + getValue()); + return String.format("%s with current value: %s", super.toString(), + getValue()); } /** diff --git a/src/java.desktop/share/classes/javax/sound/sampled/FloatControl.java b/src/java.desktop/share/classes/javax/sound/sampled/FloatControl.java index af310211d5f..dbbf8bf1ee5 100644 --- a/src/java.desktop/share/classes/javax/sound/sampled/FloatControl.java +++ b/src/java.desktop/share/classes/javax/sound/sampled/FloatControl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -330,14 +330,15 @@ public void shift(float from, float to, int microseconds) { } /** - * Provides a string representation of the control. + * Returns a string representation of the float control. * - * @return a string description + * @return a string representation of the float control */ @Override public String toString() { - return new String(getType() + " with current value: " + getValue() + " " + units + - " (range: " + minimum + " - " + maximum + ")"); + return String.format("%s with current value: %s %s (range: %s - %s)", + super.toString(), getValue(), getUnits(), + getMinimum(), getMaximum()); } /** diff --git a/src/java.desktop/share/classes/javax/sound/sampled/Line.java b/src/java.desktop/share/classes/javax/sound/sampled/Line.java index 80ce5e44d97..e6b9452d5ca 100644 --- a/src/java.desktop/share/classes/javax/sound/sampled/Line.java +++ b/src/java.desktop/share/classes/javax/sound/sampled/Line.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -318,9 +318,9 @@ public boolean matches(Info info) { } /** - * Obtains a textual description of the line info. + * Returns a string representation of the info object. * - * @return a string description + * @return a string representation of the info object */ @Override public String toString() { diff --git a/src/java.desktop/share/classes/javax/sound/sampled/LineEvent.java b/src/java.desktop/share/classes/javax/sound/sampled/LineEvent.java index 5228e6ee2f9..a02b93b73e3 100644 --- a/src/java.desktop/share/classes/javax/sound/sampled/LineEvent.java +++ b/src/java.desktop/share/classes/javax/sound/sampled/LineEvent.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -140,22 +140,13 @@ public final long getFramePosition() { } /** - * Obtains a string representation of the event. The contents of the string - * may vary between implementations of Java Sound. + * Returns a string representation of the event. * - * @return a string describing the event + * @return a string representation of the event */ @Override public String toString() { - String sType = ""; - if (type != null) sType = type.toString()+" "; - String sLine; - if (getLine() == null) { - sLine = "null"; - } else { - sLine = getLine().toString(); - } - return new String(sType + "event from line " + sLine); + return String.format("%s event from line %s", type, getLine()); } /** @@ -207,9 +198,9 @@ public final int hashCode() { } /** - * Returns the type name as the string representation. + * Returns type's name as the string representation of the event type. * - * @return the type name as the string representation + * @return a string representation of the event type */ @Override public String toString() { diff --git a/src/java.desktop/share/classes/javax/sound/sampled/Mixer.java b/src/java.desktop/share/classes/javax/sound/sampled/Mixer.java index 4890bab5690..fb09f7e2c3d 100644 --- a/src/java.desktop/share/classes/javax/sound/sampled/Mixer.java +++ b/src/java.desktop/share/classes/javax/sound/sampled/Mixer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -349,13 +349,13 @@ public final String getVersion() { } /** - * Provides a string representation of the mixer info. + * Returns a string representation of the info object. * - * @return a string describing the info object + * @return a string representation of the info object */ @Override public final String toString() { - return (name + ", version " + version); + return String.format("%s, version %s", name, version); } } } diff --git a/src/java.desktop/share/classes/javax/sound/sampled/Port.java b/src/java.desktop/share/classes/javax/sound/sampled/Port.java index b717fc1dd55..3615db8761e 100644 --- a/src/java.desktop/share/classes/javax/sound/sampled/Port.java +++ b/src/java.desktop/share/classes/javax/sound/sampled/Port.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -199,13 +199,14 @@ public final int hashCode() { } /** - * Provides a {@code String} representation of the port. + * Returns a string representation of the info object. * - * @return a string that describes the port + * @return a string representation of the info object */ @Override public final String toString() { - return (name + ((isSource == true) ? " source" : " target") + " port"); + return String.format("%s %s port", getName(), + isSource() ? "source" : "target"); } } } diff --git a/src/java.desktop/share/classes/javax/sound/sampled/ReverbType.java b/src/java.desktop/share/classes/javax/sound/sampled/ReverbType.java index 24cf21c90a3..f0bd5b8ccba 100644 --- a/src/java.desktop/share/classes/javax/sound/sampled/ReverbType.java +++ b/src/java.desktop/share/classes/javax/sound/sampled/ReverbType.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -269,21 +269,21 @@ public final int hashCode() { } /** - * Provides a {@code String} representation of the reverb type, including - * its name and its parameter settings. The exact contents of the string may - * vary between implementations of Java Sound. + * Returns a string representation of the reverb type, including its name + * and its parameter settings. The exact contents of the string may vary + * between implementations of Java Sound. * - * @return reverberation type name and description + * @return a string representation of the reverb type */ @Override public final String toString() { - - //$$fb2001-07-20: fix for bug 4385060: The "name" attribute of class "ReverbType" is not accessible. - //return (super.toString() + ", early reflection delay " + earlyReflectionDelay + - return (name + ", early reflection delay " + earlyReflectionDelay + - " ns, early reflection intensity " + earlyReflectionIntensity + - " dB, late deflection delay " + lateReflectionDelay + - " ns, late reflection intensity " + lateReflectionIntensity + - " dB, decay time " + decayTime); + return String.format("%s, early reflection delay %d ns, " + + "early reflection intensity %s dB, " + + "late deflection delay %d ns, " + + "late reflection intensity %s dB, decay time %d", + getName(), earlyReflectionDelay, + earlyReflectionIntensity, + lateReflectionDelay, lateReflectionIntensity, + decayTime); } } diff --git a/test/jdk/javax/sound/midi/Devices/ToString.java b/test/jdk/javax/sound/midi/Devices/ToString.java new file mode 100644 index 00000000000..c6668ed9890 --- /dev/null +++ b/test/jdk/javax/sound/midi/Devices/ToString.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import javax.sound.midi.MidiDevice; + +/** + * @test + * @bug 8236980 + */ +public final class ToString { + + public static void main(String[] args) { + MidiDevice.Info info = new MidiDevice.Info("nameToTest", "", "", ""){}; + if (!info.toString().contains("nameToTest")) { + throw new RuntimeException("wrong string: " + info); + } + } +} diff --git a/test/jdk/javax/sound/midi/Sequencer/ToString.java b/test/jdk/javax/sound/midi/Sequencer/ToString.java new file mode 100644 index 00000000000..fb881d7a600 --- /dev/null +++ b/test/jdk/javax/sound/midi/Sequencer/ToString.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import javax.sound.midi.Sequencer; + +/** + * @test + * @bug 8236980 + */ +public final class ToString { + + public static void main(String[] args) { + Sequencer.SyncMode syncMode = new Sequencer.SyncMode("nameToTest"){}; + if (!syncMode.toString().equals("nameToTest")) { + throw new RuntimeException("wrong string: " + syncMode); + } + } +} diff --git a/test/jdk/javax/sound/sampled/AudioFileFormat/ToString.java b/test/jdk/javax/sound/sampled/AudioFileFormat/ToString.java new file mode 100644 index 00000000000..f025ebf77dd --- /dev/null +++ b/test/jdk/javax/sound/sampled/AudioFileFormat/ToString.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import javax.sound.sampled.AudioFileFormat; + +/** + * @test + * @bug 8236980 + */ +public final class ToString { + + public static void main(String[] args) { + AudioFileFormat.Type type = new AudioFileFormat.Type("nameToTest", + "ext") {}; + if (!type.toString().equals("nameToTest")) { + throw new RuntimeException("wrong string: " + type); + } + } +} diff --git a/test/jdk/javax/sound/sampled/AudioFormat/ToString.java b/test/jdk/javax/sound/sampled/AudioFormat/ToString.java new file mode 100644 index 00000000000..cf0c3c8e3a2 --- /dev/null +++ b/test/jdk/javax/sound/sampled/AudioFormat/ToString.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import javax.sound.sampled.AudioFormat; + +/** + * @test + * @bug 8236980 + */ +public final class ToString { + + public static void main(String[] args) { + AudioFormat.Encoding enc = new AudioFormat.Encoding("nameToTest") {}; + if (!enc.toString().equals("nameToTest")) { + throw new RuntimeException("wrong string: " + enc); + } + } +} diff --git a/test/jdk/javax/sound/sampled/Controls/ToString.java b/test/jdk/javax/sound/sampled/Controls/ToString.java new file mode 100644 index 00000000000..29984594479 --- /dev/null +++ b/test/jdk/javax/sound/sampled/Controls/ToString.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import javax.sound.sampled.Control; + +/** + * @test + * @bug 8236980 + */ +public final class ToString { + + public static void main(String[] args) { + Control.Type type = new Control.Type("nameToTest") {}; + if (!type.toString().equals("nameToTest")) { + throw new RuntimeException("wrong string: " + type); + } + } +} diff --git a/test/jdk/javax/sound/sampled/LineEvent/ToString.java b/test/jdk/javax/sound/sampled/LineEvent/ToString.java new file mode 100644 index 00000000000..9c68facf4e9 --- /dev/null +++ b/test/jdk/javax/sound/sampled/LineEvent/ToString.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import javax.sound.sampled.LineEvent; + +/** + * @test + * @bug 8236980 + */ +public final class ToString { + + public static void main(String[] args) { + LineEvent.Type type = new LineEvent.Type("nameToTest") {}; + if (!type.toString().equals("nameToTest")) { + throw new RuntimeException("wrong string: " + type); + } + } +} diff --git a/test/jdk/javax/sound/sampled/ReverbType/ToString.java b/test/jdk/javax/sound/sampled/ReverbType/ToString.java new file mode 100644 index 00000000000..8f1d4ef2047 --- /dev/null +++ b/test/jdk/javax/sound/sampled/ReverbType/ToString.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import javax.sound.sampled.ReverbType; + +/** + * @test + * @bug 4385060 8236980 + */ +public final class ToString { + + public static void main(String[] args) { + ReverbType type = new ReverbType("nameToTest", 0, 0, 0, 0, 0) {}; + if (!type.toString().startsWith("nameToTest")) { + throw new RuntimeException("wrong string: " + type); + } + } +} From b36738a55aa9dff2948b8a63ad291dc6cf084df2 Mon Sep 17 00:00:00 2001 From: Sergey Bylokhov Date: Sun, 26 Apr 2020 19:36:04 -0700 Subject: [PATCH 2/3] 8238575: DragSourceEvent.getLocation() returns wrong value on HiDPI screens (Windows) Reviewed-by: prr --- .../native/libawt/windows/awt_DnDDS.cpp | 55 +++++++++------ .../windows/native/libawt/windows/awt_DnDDS.h | 18 ++--- .../native/libawt/windows/awt_DnDDT.cpp | 2 +- .../dnd/Button2DragTest/Button2DragTest.java | 70 +++++++++++++------ test/jdk/sun/awt/dnd/8024061/bug8024061.java | 34 ++++----- 5 files changed, 112 insertions(+), 67 deletions(-) diff --git a/src/java.desktop/windows/native/libawt/windows/awt_DnDDS.cpp b/src/java.desktop/windows/native/libawt/windows/awt_DnDDS.cpp index c1057b5cc38..390b9250aba 100644 --- a/src/java.desktop/windows/native/libawt/windows/awt_DnDDS.cpp +++ b/src/java.desktop/windows/native/libawt/windows/awt_DnDDS.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -282,7 +282,7 @@ void AwtDragSource::_DoDragDrop(void* param) { call_dSCddfinished(env, peer, res == DRAGDROP_S_DROP && effects != DROPEFFECT_NONE, convertDROPEFFECTToActions(effects), - dragSource->m_dragPoint.x, dragSource->m_dragPoint.y); + dragSource->m_dragPoint); env->DeleteLocalRef(peer); @@ -649,8 +649,7 @@ HRESULT __stdcall AwtDragSource::QueryContinueDrag(BOOL fEscapeKeyPressed, DWOR if ( (dragPoint.x != m_dragPoint.x || dragPoint.y != m_dragPoint.y) && m_lastmods == modifiers) {//cannot move before cursor change - call_dSCmouseMoved(env, m_peer, - m_actions, modifiers, dragPoint.x, dragPoint.y); + call_dSCmouseMoved(env, m_peer, m_actions, modifiers, dragPoint); JNU_CHECK_EXCEPTION_RETURN(env, E_UNEXPECTED); m_dragPoint = dragPoint; } @@ -662,8 +661,7 @@ HRESULT __stdcall AwtDragSource::QueryContinueDrag(BOOL fEscapeKeyPressed, DWOR } else if ((modifiers & JAVA_BUTTON_MASK) != (m_initmods & JAVA_BUTTON_MASK)) { return DRAGDROP_S_CANCEL; } else if (m_lastmods != modifiers) { - call_dSCchanged(env, m_peer, - m_actions, modifiers, dragPoint.x, dragPoint.y); + call_dSCchanged(env, m_peer, m_actions, modifiers, dragPoint); m_bRestoreNodropCustomCursor = TRUE; } @@ -726,13 +724,13 @@ HRESULT __stdcall AwtDragSource::GiveFeedback(DWORD dwEffect) { if (invalid) { // Don't call dragExit if dragEnter and dragOver haven't been called. if (!m_enterpending) { - call_dSCexit(env, m_peer, curs.x, curs.y); + call_dSCexit(env, m_peer, curs); } m_droptarget = (HWND)NULL; m_enterpending = TRUE; } else if (m_droptarget != NULL) { (*(m_enterpending ? call_dSCenter : call_dSCmotion)) - (env, m_peer, m_actions, modifiers, curs.x, curs.y); + (env, m_peer, m_actions, modifiers, curs); m_enterpending = FALSE; } @@ -1176,14 +1174,26 @@ HRESULT __stdcall AwtDragSource::GetProcessId(FORMATETC __RPC_FAR *pFormatEtc, S return S_OK; } +static void ScaleDown(POINT &pt) { + HMONITOR monitor = MonitorFromPoint(pt, MONITOR_DEFAULTTOPRIMARY); + int screen = AwtWin32GraphicsDevice::GetScreenFromHMONITOR(monitor); + Devices::InstanceAccess devices; + AwtWin32GraphicsDevice *device = devices->GetDevice(screen); + if (device) { + pt.x = device->ScaleDownX(pt.x); + pt.y = device->ScaleDownY(pt.y); + } +} + DECLARE_JAVA_CLASS(dSCClazz, "sun/awt/windows/WDragSourceContextPeer") void AwtDragSource::call_dSCenter(JNIEnv* env, jobject self, jint targetActions, - jint modifiers, jint x, jint y) { + jint modifiers, POINT pt) { + ScaleDown(pt); DECLARE_VOID_JAVA_METHOD(dSCenter, dSCClazz, "dragEnter", "(IIII)V"); DASSERT(!JNU_IsNull(env, self)); - env->CallVoidMethod(self, dSCenter, targetActions, modifiers, x, y); + env->CallVoidMethod(self, dSCenter, targetActions, modifiers, pt.x, pt.y); if (!JNU_IsNull(env, safe_ExceptionOccurred(env))) { env->ExceptionDescribe(); env->ExceptionClear(); @@ -1192,10 +1202,11 @@ AwtDragSource::call_dSCenter(JNIEnv* env, jobject self, jint targetActions, void AwtDragSource::call_dSCmotion(JNIEnv* env, jobject self, jint targetActions, - jint modifiers, jint x, jint y) { + jint modifiers, POINT pt) { + ScaleDown(pt); DECLARE_VOID_JAVA_METHOD(dSCmotion, dSCClazz, "dragMotion", "(IIII)V"); DASSERT(!JNU_IsNull(env, self)); - env->CallVoidMethod(self, dSCmotion, targetActions, modifiers, x, y); + env->CallVoidMethod(self, dSCmotion, targetActions, modifiers, pt.x, pt.y); if (!JNU_IsNull(env, safe_ExceptionOccurred(env))) { env->ExceptionDescribe(); env->ExceptionClear(); @@ -1204,11 +1215,12 @@ AwtDragSource::call_dSCmotion(JNIEnv* env, jobject self, jint targetActions, void AwtDragSource::call_dSCchanged(JNIEnv* env, jobject self, jint targetActions, - jint modifiers, jint x, jint y) { + jint modifiers, POINT pt) { + ScaleDown(pt); DECLARE_VOID_JAVA_METHOD(dSCchanged, dSCClazz, "operationChanged", "(IIII)V"); DASSERT(!JNU_IsNull(env, self)); - env->CallVoidMethod(self, dSCchanged, targetActions, modifiers, x, y); + env->CallVoidMethod(self, dSCchanged, targetActions, modifiers, pt.x, pt.y); if (!JNU_IsNull(env, safe_ExceptionOccurred(env))) { env->ExceptionDescribe(); env->ExceptionClear(); @@ -1216,10 +1228,11 @@ AwtDragSource::call_dSCchanged(JNIEnv* env, jobject self, jint targetActions, } void -AwtDragSource::call_dSCexit(JNIEnv* env, jobject self, jint x, jint y) { +AwtDragSource::call_dSCexit(JNIEnv* env, jobject self, POINT pt) { + ScaleDown(pt); DECLARE_VOID_JAVA_METHOD(dSCexit, dSCClazz, "dragExit", "(II)V"); DASSERT(!JNU_IsNull(env, self)); - env->CallVoidMethod(self, dSCexit, x, y); + env->CallVoidMethod(self, dSCexit, pt.x, pt.y); if (!JNU_IsNull(env, safe_ExceptionOccurred(env))) { env->ExceptionDescribe(); env->ExceptionClear(); @@ -1228,10 +1241,11 @@ AwtDragSource::call_dSCexit(JNIEnv* env, jobject self, jint x, jint y) { void AwtDragSource::call_dSCddfinished(JNIEnv* env, jobject self, jboolean success, - jint operations, jint x, jint y) { + jint operations, POINT pt) { + ScaleDown(pt); DECLARE_VOID_JAVA_METHOD(dSCddfinished, dSCClazz, "dragDropFinished", "(ZIII)V"); DASSERT(!JNU_IsNull(env, self)); - env->CallVoidMethod(self, dSCddfinished, success, operations, x, y); + env->CallVoidMethod(self, dSCddfinished, success, operations, pt.x, pt.y); if (!JNU_IsNull(env, safe_ExceptionOccurred(env))) { env->ExceptionDescribe(); env->ExceptionClear(); @@ -1240,11 +1254,12 @@ AwtDragSource::call_dSCddfinished(JNIEnv* env, jobject self, jboolean success, void AwtDragSource::call_dSCmouseMoved(JNIEnv* env, jobject self, jint targetActions, - jint modifiers, jint x, jint y) { + jint modifiers, POINT pt) { + ScaleDown(pt); DECLARE_VOID_JAVA_METHOD(dSCmouseMoved, dSCClazz, "dragMouseMoved", "(IIII)V"); DASSERT(!JNU_IsNull(env, self)); - env->CallVoidMethod(self, dSCmouseMoved, targetActions, modifiers, x, y); + env->CallVoidMethod(self, dSCmouseMoved, targetActions, modifiers, pt.x, pt.y); if (!JNU_IsNull(env, safe_ExceptionOccurred(env))) { env->ExceptionDescribe(); env->ExceptionClear(); diff --git a/src/java.desktop/windows/native/libawt/windows/awt_DnDDS.h b/src/java.desktop/windows/native/libawt/windows/awt_DnDDS.h index 0abdc2d6760..1549ca64210 100644 --- a/src/java.desktop/windows/native/libawt/windows/awt_DnDDS.h +++ b/src/java.desktop/windows/native/libawt/windows/awt_DnDDS.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -127,20 +127,20 @@ class AwtDragSource : virtual public IDropSource, virtual public IDataObject { static int __cdecl _compar(const void *, const void *); static void call_dSCenter(JNIEnv* env, jobject self, jint targetActions, - jint modifiers, jint x, jint y); + jint modifiers, POINT pt); static void call_dSCmotion(JNIEnv* env, jobject self, jint targetActions, jint modifiers, - jint x, jint y); + POINT pt); static void call_dSCchanged(JNIEnv* env, jobject self, jint targetActions, jint modifiers, - jint x, jint y); + POINT pt); static void call_dSCmouseMoved(JNIEnv* env, jobject self, jint targetActions, jint modifiers, - jint x, jint y); - static void call_dSCexit(JNIEnv* env, jobject self, jint x, jint y); + POINT pt); + static void call_dSCexit(JNIEnv* env, jobject self, POINT pt); static void call_dSCddfinished(JNIEnv* env, jobject self, jboolean success, jint operations, - jint x, jint y); + POINT pt); protected: class ADSIEnumFormatEtc : public virtual IEnumFORMATETC { @@ -254,8 +254,8 @@ class AwtDragSource : virtual public IDropSource, virtual public IDataObject { jobject m_transferable; jobject m_formatMap; - POINT m_dragPoint; - POINT m_dropPoint; + POINT m_dragPoint; // device space (pixels) + POINT m_dropPoint; // device space (pixels) BOOL m_fNC; BOOL m_bRestoreNodropCustomCursor;//CR 6480706 - MS Bug on hold diff --git a/src/java.desktop/windows/native/libawt/windows/awt_DnDDT.cpp b/src/java.desktop/windows/native/libawt/windows/awt_DnDDT.cpp index bdf7dafbe49..aa7036004b9 100644 --- a/src/java.desktop/windows/native/libawt/windows/awt_DnDDT.cpp +++ b/src/java.desktop/windows/native/libawt/windows/awt_DnDDT.cpp @@ -125,7 +125,7 @@ ULONG __stdcall AwtDropTarget::Release() { return (ULONG)refs; } -void ScaleDown(POINT &cp, HWND m_window) { +static void ScaleDown(POINT &cp, HWND m_window) { int screen = AwtWin32GraphicsDevice::DeviceIndexForWindow(m_window); Devices::InstanceAccess devices; AwtWin32GraphicsDevice* device = devices->GetDevice(screen); diff --git a/test/jdk/java/awt/dnd/Button2DragTest/Button2DragTest.java b/test/jdk/java/awt/dnd/Button2DragTest/Button2DragTest.java index df5041d3818..9400f854da1 100644 --- a/test/jdk/java/awt/dnd/Button2DragTest/Button2DragTest.java +++ b/test/jdk/java/awt/dnd/Button2DragTest/Button2DragTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2006, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -21,7 +21,11 @@ * questions. */ +import java.awt.Color; import java.awt.Frame; +import java.awt.GraphicsConfiguration; +import java.awt.GraphicsDevice; +import java.awt.GraphicsEnvironment; import java.awt.Point; import java.awt.Robot; import java.awt.datatransfer.StringSelection; @@ -29,8 +33,9 @@ import java.awt.dnd.DragGestureEvent; import java.awt.dnd.DragGestureListener; import java.awt.dnd.DragSource; -import java.awt.dnd.DragSourceAdapter; +import java.awt.dnd.DragSourceDragEvent; import java.awt.dnd.DragSourceDropEvent; +import java.awt.dnd.DragSourceEvent; import java.awt.dnd.DragSourceListener; import java.awt.dnd.DropTarget; import java.awt.dnd.DropTargetAdapter; @@ -42,7 +47,7 @@ /** * @test * @key headful - * @bug 4955110 + * @bug 4955110 8238575 * @summary tests that DragSourceDragEvent.getDropAction() accords to its new * spec (does not depend on the user drop action) * @library ../../regtesthelpers @@ -53,26 +58,56 @@ public final class Button2DragTest { private volatile boolean dropSuccess; + private volatile boolean locationValid = true; private static Frame frame; public static void main(final String[] args) { - Button2DragTest test = new Button2DragTest(); - try { - test.run(); - } finally { - if (frame != null) { + var lge = GraphicsEnvironment.getLocalGraphicsEnvironment(); + for (GraphicsDevice device : lge.getScreenDevices()) { + Button2DragTest test = new Button2DragTest(); + frame = new Frame(device.getDefaultConfiguration()); + try { + test.run(); + } finally { frame.dispose(); } } } public void run() { - frame = new Frame(); + final DragSourceListener dragSourceListener = new DragSourceListener() { + private void checkLocation(DragSourceEvent dsde) { + if (!frame.getBounds().contains(dsde.getLocation())) { + System.err.println("Expected in" + frame.getBounds()); + System.err.println("Actual" + dsde.getLocation()); + locationValid = false; + } + } + + @Override + public void dragEnter(DragSourceDragEvent dsde) { + checkLocation(dsde); + } + + @Override + public void dragOver(DragSourceDragEvent dsde) { + checkLocation(dsde); + } - final DragSourceListener dragSourceListener = new DragSourceAdapter() { - public void dragDropEnd(DragSourceDropEvent e) { - dropSuccess = e.getDropSuccess(); + @Override + public void dropActionChanged(DragSourceDragEvent dsde) { + checkLocation(dsde); + } + + @Override + public void dragExit(DragSourceEvent dse) { + checkLocation(dse); + } + + public void dragDropEnd(DragSourceDropEvent dsde) { + checkLocation(dsde); + dropSuccess = dsde.getDropSuccess(); System.err.println("Drop was successful: " + dropSuccess); } }; @@ -93,11 +128,9 @@ public void drop(DropTargetDropEvent dtde) { }; new DropTarget(frame, dropTargetListener); - //What would normally go into main() will probably go here. - //Use System.out.println for diagnostic messages that you want - //to read after the test is done. + frame.setBackground(Color.GREEN); frame.setUndecorated(true); - frame.setBounds(100, 100, 200, 200); + frame.setSize(200, 200); frame.setLocationRelativeTo(null); frame.setVisible(true); @@ -115,11 +148,8 @@ public void drop(DropTargetDropEvent dtde) { Util.waitForIdle(robot); robot.delay(500); - if (dropSuccess) { - System.err.println("test passed"); - } else { + if (!dropSuccess || !locationValid) { throw new RuntimeException("test failed: drop was not successful"); } } - } diff --git a/test/jdk/sun/awt/dnd/8024061/bug8024061.java b/test/jdk/sun/awt/dnd/8024061/bug8024061.java index 4ffc6cc8096..742c5afc8b1 100644 --- a/test/jdk/sun/awt/dnd/8024061/bug8024061.java +++ b/test/jdk/sun/awt/dnd/8024061/bug8024061.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -27,11 +27,17 @@ * @bug 8024061 * @summary Checks that no exception is thrown if dragGestureRecognized * takes a while to complete. - * @library /test/lib - * @build jdk.test.lib.Platform - * @run main bug8024061 */ -import java.awt.*; + +import java.awt.AWTException; +import java.awt.Color; +import java.awt.Container; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.GridLayout; +import java.awt.Point; +import java.awt.Robot; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; @@ -49,15 +55,15 @@ import java.awt.dnd.DropTargetEvent; import java.awt.dnd.DropTargetListener; import java.awt.event.InputEvent; - import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import javax.swing.*; - -import jdk.test.lib.Platform; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.SwingUtilities; +import javax.swing.WindowConstants; /** * If dragGestureRecognized() takes a while to complete and if user performs a drag quickly, @@ -107,19 +113,13 @@ public class bug8024061 { content.add(panel2); frame.pack(); - + frame.setLocationRelativeTo(null); DropObject drop = new DropObject(); drop.place(panel1, new Point(10, 10)); frame.setVisible(true); } public static void main(String[] args) throws AWTException, InvocationTargetException, InterruptedException { - if (!Platform.isLinux() && !Platform.isSolaris()) { - System.out.println("This test is for Linux and Solaris only... " + - "skipping!"); - return; - } - final bug8024061[] dnd = {null}; SwingUtilities.invokeAndWait(new Runnable() { @Override @@ -166,7 +166,7 @@ public void run() { throw new RuntimeException("Timed out waiting for dragEnter()"); } } finally { - SwingUtilities.invokeLater(frame::dispose); + SwingUtilities.invokeAndWait(frame::dispose); } } From 14b7dd40905f649f68bbe0c1e0faadd1dfabea84 Mon Sep 17 00:00:00 2001 From: Sergey Bylokhov Date: Sun, 26 Apr 2020 19:42:04 -0700 Subject: [PATCH 3/3] 7185258: [macosx] Deadlock in SunToolKit.realSync() Reviewed-by: prr --- .../classes/sun/lwawt/macosx/LWCToolkit.java | 8 ++ .../native/libawt_lwawt/awt/CDragSource.m | 11 ++- .../native/libawt_lwawt/awt/LWCToolkit.h | 2 + .../native/libawt_lwawt/awt/LWCToolkit.m | 33 ++++++-- .../sun/awt/dnd/SunDragSourceContextPeer.java | 10 ++- .../dnd/DragWaitForIdle/DragWaitForIdle.java | 83 +++++++++++++++++++ 6 files changed, 135 insertions(+), 12 deletions(-) create mode 100644 test/jdk/java/awt/dnd/DragWaitForIdle/DragWaitForIdle.java diff --git a/src/java.desktop/macosx/classes/sun/lwawt/macosx/LWCToolkit.java b/src/java.desktop/macosx/classes/sun/lwawt/macosx/LWCToolkit.java index 76923d645f3..c0a95e7b624 100644 --- a/src/java.desktop/macosx/classes/sun/lwawt/macosx/LWCToolkit.java +++ b/src/java.desktop/macosx/classes/sun/lwawt/macosx/LWCToolkit.java @@ -107,6 +107,7 @@ import sun.awt.PlatformGraphicsInfo; import sun.awt.SunToolkit; import sun.awt.datatransfer.DataTransferer; +import sun.awt.dnd.SunDragSourceContextPeer; import sun.awt.util.ThreadGroupUtils; import sun.java2d.opengl.OGLRenderQueue; import sun.lwawt.LWComponentPeer; @@ -463,6 +464,13 @@ protected void initializeDesktopProperties() { @Override protected boolean syncNativeQueue(long timeout) { + if (SunDragSourceContextPeer.isDragDropInProgress() + || EventQueue.isDispatchThread()) { + // The java code started the DnD, but the native drag may still not + // start, the last attempt to flush the native events, + // also do not block EDT for a long time + timeout = 50; + } return nativeSyncQueue(timeout); } diff --git a/src/java.desktop/macosx/native/libawt_lwawt/awt/CDragSource.m b/src/java.desktop/macosx/native/libawt_lwawt/awt/CDragSource.m index 2071544d0d3..06dbbf4670c 100644 --- a/src/java.desktop/macosx/native/libawt_lwawt/awt/CDragSource.m +++ b/src/java.desktop/macosx/native/libawt_lwawt/awt/CDragSource.m @@ -37,6 +37,7 @@ #import "CDragSource.h" #import "DnDUtilities.h" #import "ThreadUtilities.h" +#import "LWCToolkit.h" // When sIsJavaDragging is true Java drag gesture has been recognized and a drag is/has been initialized. @@ -511,9 +512,9 @@ - (void)doDrag fDragKeyModifiers = [DnDUtilities extractJavaExtKeyModifiersFromJavaExtModifiers:fModifiers]; fDragMouseModifiers = [DnDUtilities extractJavaExtMouseModifiersFromJavaExtModifiers:fModifiers]; - sNeedsEnter = YES; - @try { + sNeedsEnter = YES; + AWTToolkit.inDoDragDropLoop = YES; // Data dragging: if (isFileDrag == FALSE) { [view dragImage:dragImage at:dragOrigin offset:dragOffset event:dragEvent pasteboard:pb source:view slideBack:YES]; @@ -561,6 +562,7 @@ - (void)doDrag JNFCallVoidMethod(env, fDragSourceContextPeer, resetHoveringMethod); // Hust reset static variable } @finally { sNeedsEnter = NO; + AWTToolkit.inDoDragDropLoop = NO; } // We have to do this, otherwise AppKit doesn't know we're finished dragging. Yup, it's that bad. @@ -607,7 +609,7 @@ - (NSArray *)namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination { - (void)draggedImage:(NSImage *)image beganAt:(NSPoint)screenPoint { DLog4(@"[CDragSource draggedImage beganAt]: (%f, %f) %@\n", screenPoint.x, screenPoint.y, self); - + [AWTToolkit eventCountPlusPlus]; // Initialize static variables: sDragOperation = NSDragOperationNone; sDraggingLocation = screenPoint; @@ -615,7 +617,7 @@ - (void)draggedImage:(NSImage *)image beganAt:(NSPoint)screenPoint { - (void)draggedImage:(NSImage *)image endedAt:(NSPoint)screenPoint operation:(NSDragOperation)operation { DLog4(@"[CDragSource draggedImage endedAt:]: (%f, %f) %@\n", screenPoint.x, screenPoint.y, self); - + [AWTToolkit eventCountPlusPlus]; sDraggingLocation = screenPoint; sDragOperation = operation; } @@ -625,6 +627,7 @@ - (void)draggedImage:(NSImage *)image movedTo:(NSPoint)screenPoint { JNIEnv* env = [ThreadUtilities getJNIEnv]; JNF_COCOA_ENTER(env); + [AWTToolkit eventCountPlusPlus]; // There are two things we would be interested in: // a) mouse pointer has moved // b) drag actions (key modifiers) have changed diff --git a/src/java.desktop/macosx/native/libawt_lwawt/awt/LWCToolkit.h b/src/java.desktop/macosx/native/libawt_lwawt/awt/LWCToolkit.h index 40bc58362f8..313b49e9651 100644 --- a/src/java.desktop/macosx/native/libawt_lwawt/awt/LWCToolkit.h +++ b/src/java.desktop/macosx/native/libawt_lwawt/awt/LWCToolkit.h @@ -39,6 +39,8 @@ extern int gNumberOfButtons; extern jint* gButtonDownMasks; @interface AWTToolkit : NSObject { } ++ (BOOL) inDoDragDropLoop; ++ (void) setInDoDragDropLoop:(BOOL)val; + (long) getEventCount; + (void) eventCountPlusPlus; + (jint) scrollStateWithEvent: (NSEvent*) event; diff --git a/src/java.desktop/macosx/native/libawt_lwawt/awt/LWCToolkit.m b/src/java.desktop/macosx/native/libawt_lwawt/awt/LWCToolkit.m index a153c256e7f..e0c5586e247 100644 --- a/src/java.desktop/macosx/native/libawt_lwawt/awt/LWCToolkit.m +++ b/src/java.desktop/macosx/native/libawt_lwawt/awt/LWCToolkit.m @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -70,13 +70,30 @@ @implementation AWTToolkit static long eventCount; +static BOOL inDoDragDropLoop; + ++ (BOOL) inDoDragDropLoop { + @synchronized(self) { + return inDoDragDropLoop; + } +} + ++ (void) setInDoDragDropLoop:(BOOL)val { + @synchronized(self) { + inDoDragDropLoop = val; + } +} + (long) getEventCount{ + @synchronized(self) { return eventCount; + } } + (void) eventCountPlusPlus{ + @synchronized(self) { eventCount++; + } } + (jint) scrollStateWithEvent: (NSEvent*) event { @@ -420,10 +437,16 @@ + (void)starter:(BOOL)wasOnMainThread headless:(BOOL)headless { // immediately after this we will post the second event via // [NSApp postEvent] then sometimes the second event will be handled // first. The opposite isn't proved, but we use both here to be safer. - [theApp postDummyEvent:false]; - [theApp waitForDummyEvent:timeout / 2.0]; - [theApp postDummyEvent:true]; - [theApp waitForDummyEvent:timeout / 2.0]; + + // If the native drag is in progress, skip native sync. + if (!AWTToolkit.inDoDragDropLoop) { + [theApp postDummyEvent:false]; + [theApp waitForDummyEvent:timeout / 2.0]; + } + if (!AWTToolkit.inDoDragDropLoop) { + [theApp postDummyEvent:true]; + [theApp waitForDummyEvent:timeout / 2.0]; + } } else { // could happen if we are embedded inside SWT application, diff --git a/src/java.desktop/share/classes/sun/awt/dnd/SunDragSourceContextPeer.java b/src/java.desktop/share/classes/sun/awt/dnd/SunDragSourceContextPeer.java index d0e5c577b78..5aa3d8c1987 100644 --- a/src/java.desktop/share/classes/sun/awt/dnd/SunDragSourceContextPeer.java +++ b/src/java.desktop/share/classes/sun/awt/dnd/SunDragSourceContextPeer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -74,8 +74,8 @@ public abstract class SunDragSourceContextPeer implements DragSourceContextPeer private DragSourceContext dragSourceContext; private int sourceActions; - private static boolean dragDropInProgress = false; - private static boolean discardingMouseEvents = false; + private static volatile boolean dragDropInProgress = false; + private static boolean discardingMouseEvents = false; /* * dispatch constants @@ -381,6 +381,10 @@ public static void checkDragDropInProgress() } } + public static boolean isDragDropInProgress() { + return dragDropInProgress; + } + private static String getExceptionMessage(boolean b) { return b ? "Drag and drop in progress" : "No drag in progress"; } diff --git a/test/jdk/java/awt/dnd/DragWaitForIdle/DragWaitForIdle.java b/test/jdk/java/awt/dnd/DragWaitForIdle/DragWaitForIdle.java new file mode 100644 index 00000000000..cae8c4271cf --- /dev/null +++ b/test/jdk/java/awt/dnd/DragWaitForIdle/DragWaitForIdle.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.awt.Frame; +import java.awt.Point; +import java.awt.Robot; +import java.awt.datatransfer.StringSelection; +import java.awt.dnd.*; +import java.awt.event.InputEvent; + +import test.java.awt.regtesthelpers.Util; + +/** + * @test + * @key headful + * @bug 7185258 + * @summary Robot.waitForIdle() should not hang forever if dnd is in progress + * @library ../../regtesthelpers + * @build Util + * @run main/othervm DragWaitForIdle + */ +public final class DragWaitForIdle { + + public static void main(final String[] args) throws Exception { + Frame frame = new Frame(); + Robot robot = new Robot(); + robot.setAutoWaitForIdle(true); // key point of the test + + DragGestureListener dragGestureListener = dge -> { + dge.startDrag(null, new StringSelection("OK"), new DragSourceAdapter(){}); + }; + + new DragSource().createDefaultDragGestureRecognizer(frame, + DnDConstants.ACTION_MOVE, dragGestureListener); + + new DropTarget(frame, new DropTargetAdapter() { + public void drop(DropTargetDropEvent dtde) { + dtde.acceptDrop(DnDConstants.ACTION_MOVE); + dtde.dropComplete(true); + } + }); + + try { + frame.setUndecorated(true); + frame.setBounds(100, 100, 200, 200); + frame.setLocationRelativeTo(null); + frame.setVisible(true); + robot.waitForIdle(); + frame.toFront(); + + Point startPoint = frame.getLocationOnScreen(); + Point endPoint = new Point(startPoint); + startPoint.translate(50, 50); + endPoint.translate(150, 150); + + Util.drag(robot, startPoint, endPoint, InputEvent.BUTTON2_MASK); + + robot.delay(500); + } finally { + frame.dispose(); + } + } +}