matthiasblaesing commented on code in PR #8361: URL: https://github.com/apache/netbeans/pull/8361#discussion_r2016967565
########## platform/o.n.agent/src/org/netbeans/agent/TransferHandlerActionsNonFinalTransformer.java: ########## @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.agent; + +import java.io.IOException; +import java.lang.instrument.ClassFileTransformer; +import java.lang.instrument.IllegalClassFormatException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; +import java.security.ProtectionDomain; +import java.text.MessageFormat; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.FieldVisitor; +import org.objectweb.asm.Opcodes; + +/** + * Transform javax.swing.TransferHandler to enable replacing the copy, cut and + * paste actions and replace them with their NetBeans implementations. + * + * The transformer can be debugged: + * + * - The system property "org.netbeans.agent.TransferHandlerActionsNonFinalTransformer.disable" + * takes a boolean to disable the transformer. I.e. running NetBeans as + * + * NBBASEPATH/bin/netbeans64.exe -J-Dorg.netbeans.agent.TransferHandlerActionsNonFinalTransformer.disable=true + * + * will run without this. + * + * - The system property "org.netbeans.agent.TransferHandlerActionsNonFinalTransformer.dumpPath" + * takes a string. The string is expected to be a path and the transformed + * class data will be dumped at that location. It is expected, that a path + * including the filename is specified. + */ +public class TransferHandlerActionsNonFinalTransformer implements ClassFileTransformer { + + private static final String DEBUG_DUMP_TRANSFORMED_CLASS = System.getProperty("org.netbeans.agent.TransferHandlerActionsNonFinalTransformer.dumpPath"); + private static final boolean DEBUG_DISABLE_TRANSFORMER = Boolean.getBoolean("org.netbeans.agent.TransferHandlerActionsNonFinalTransformer.disable"); + + private static Logger getLog() { + return Logger.getLogger(TransferHandlerActionsNonFinalTransformer.class.getName()); + } + + @Override + public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { + if (DEBUG_DISABLE_TRANSFORMER) { + return null; + } + // We assume, that we are loaded early enough before AWT is initialzed, + // so we don't expect to need to retransform + if (classBeingRedefined != null) { + getLog().log(Level.WARNING, "Attempted retransform for: {0} (Ignored)", new Object[]{className}); + } + // Only transform TransferHandler class - ignore all other classes + if ("javax/swing/TransferHandler".equals(className)) { + getLog().log(Level.INFO, "Transforming: {0}", new Object[]{className}); + try { + ClassReader cr = new ClassReader(classfileBuffer); + ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); + ClassVisitor cv = new ClassVisitor(Opcodes.ASM9, cw) { + @Override + public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) { + switch(name) { + case "cutAction": + case "copyAction": + case "pasteAction": + return super.visitField(access & (~ Opcodes.ACC_FINAL), name, descriptor, signature, value); + default: + return super.visitField(access, name, descriptor, signature, value); + } + } + }; + cr.accept(cv, 0); + byte[] result = cw.toByteArray(); + if (DEBUG_DUMP_TRANSFORMED_CLASS != null && !DEBUG_DUMP_TRANSFORMED_CLASS.isBlank()) { + Files.write(Path.of(DEBUG_DUMP_TRANSFORMED_CLASS), result, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); + } + return result; + } catch (IOException | RuntimeException ex) { + getLog().log(Level.INFO, MessageFormat.format("Transforming: {0} failed", new Object[]{className}), ex); + } finally { + getLog().log(Level.INFO, "Transforming: {0} done", new Object[]{className}); Review Comment: JUL usage was removed here to not mess with its initialization by the IDE/unittesting infrastructure. Replacement call was moved as suggested. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@netbeans.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@netbeans.apache.org For additional commands, e-mail: notifications-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists