avoid NoSuchMethodError under jdk9

This commit is contained in:
Alexey Kudravtsev
2017-05-17 14:40:26 +03:00
parent 30b6b67af1
commit a16b875656
3 changed files with 31 additions and 7 deletions
@@ -28,7 +28,7 @@ import com.intellij.codeInspection.ui.InspectionResultsView;
import com.intellij.codeInspection.ui.InspectionTree;
import com.intellij.icons.AllIcons;
import com.intellij.ide.DataManager;
import com.intellij.internal.ReviseWhenPortedToJDK;
import com.intellij.ReviseWhenPortedToJDK;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.actionSystem.ex.CustomComponentAction;
import com.intellij.openapi.application.ApplicationManager;
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.internal;
package com.intellij;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
@@ -26,5 +26,5 @@ import java.lang.annotation.Target;
@Target({ElementType.METHOD,ElementType.TYPE,ElementType.TYPE_USE,ElementType.ANNOTATION_TYPE,ElementType.CONSTRUCTOR,ElementType.FIELD,ElementType.PARAMETER,ElementType.LOCAL_VARIABLE,ElementType.PACKAGE,ElementType.TYPE_PARAMETER})
@Retention(RetentionPolicy.SOURCE)
public @interface ReviseWhenPortedToJDK {
String value();
String value(); // JDK major version
}
@@ -15,13 +15,16 @@
*/
package com.intellij.util.io;
import com.intellij.ReviseWhenPortedToJDK;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.SystemInfo;
import org.jetbrains.annotations.Nullable;
import sun.misc.Cleaner;
import sun.nio.ch.DirectBuffer;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.security.AccessController;
import java.security.PrivilegedAction;
@@ -58,15 +61,37 @@ public abstract class DirectBufferWrapper extends ByteBufferWrapper {
myBuffer = null;
}
static boolean disposeDirectBuffer(final ByteBuffer buffer) {
@ReviseWhenPortedToJDK("9")
static boolean disposeDirectBuffer(final ByteBuffer buffer) {
return AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
@Nullable
public Object run() {
try {
if (buffer instanceof DirectBuffer) {
Cleaner cleaner = ((DirectBuffer)buffer).cleaner();
if (cleaner != null) cleaner.clean(); // Already cleaned otherwise
if (SystemInfo.IS_AT_LEAST_JAVA9) {
// in JDK9 DirectBuffer.cleaner() returns jdk.internal.ref.Cleaner instead of sun.misc.Cleaner
// since we have to target both jdk 8 and 9 we have to use reflection
try {
Method cleanerMethod = buffer.getClass().getMethod("cleaner");
cleanerMethod.setAccessible(true);
Object cleaner = cleanerMethod.invoke(buffer);
if (cleaner != null) {
Method cleanMethod = cleaner.getClass().getMethod("clean");
cleanMethod.setAccessible(true);
cleanMethod.invoke(cleaner);
}
}
catch (Exception e) {
// something serious, needs to be logged
LOG.error(e);
throw e;
}
}
else {
Cleaner cleaner = ((DirectBuffer)buffer).cleaner();
if (cleaner != null) cleaner.clean(); // Already cleaned otherwise
}
}
return null;
}
@@ -76,5 +101,4 @@ public abstract class DirectBufferWrapper extends ByteBufferWrapper {
}
}) == null;
}
}