由于无法将html.disable
每个 created 的属性全局设置为 true JLabel
,因此一种 hacky 方式(我说 hacky 是因为我不确定对性能的影响,或者这种解决方案是否可以用于生产)是做一些每个创建的JLabel
实例的字节码拦截。像 ByteBuddy 这样的库可以做到这一点。我已经对 ByteBuddy 进行了一些试验,并找到了一种设置Java 代理setText()
的方法,该代理拦截对JLabel
. JLabel
在使用提供的文本创建一个时调用此方法。
代理人
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.agent.builder.AgentBuilder.InitializationStrategy;
import net.bytebuddy.agent.builder.AgentBuilder.Listener;
import net.bytebuddy.agent.builder.AgentBuilder.RedefinitionStrategy;
import net.bytebuddy.agent.builder.AgentBuilder.TypeStrategy;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.dynamic.loading.ClassInjector;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.SuperMethodCall;
import net.bytebuddy.matcher.ElementMatchers;
import net.bytebuddy.matcher.StringMatcher;
import java.io.File;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.nio.file.Files;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.description.type.TypeDescription.ForLoadedType;
import static net.bytebuddy.dynamic.ClassFileLocator.ForClassLoader.read;
import static net.bytebuddy.dynamic.loading.ClassInjector.UsingInstrumentation.Target.BOOTSTRAP;
import static net.bytebuddy.matcher.ElementMatchers.*;
public class JLabelAgent {
private static final Class<?> INTERCEPTOR_CLASS = JLabelInterceptor.class;
private JLabelAgent() {
}
public static void premain(String arg, Instrumentation instrumentation) throws Exception {
injectBootstrapClasses(instrumentation);
new AgentBuilder.Default()
.with(RedefinitionStrategy.RETRANSFORMATION)
.with(InitializationStrategy.NoOp.INSTANCE)
.with(TypeStrategy.Default.REDEFINE)
.ignore(new AgentBuilder.RawMatcher.ForElementMatchers(nameStartsWith("net.bytebuddy.").or(isSynthetic()), any(), any()))
.with(new Listener.Filtering(
new StringMatcher("javax.swing.JLabel", StringMatcher.Mode.EQUALS_FULLY),
Listener.StreamWriting.toSystemOut()))
.type(named("javax.swing.JLabel"))
.transform((builder, type, classLoader, module) ->
builder.visit(Advice.to(INTERCEPTOR_CLASS).on(named("setText")))
)
.installOn(instrumentation);
}
private static void injectBootstrapClasses(Instrumentation instrumentation) throws IOException {
File temp = Files.createTempDirectory("tmp").toFile();
temp.deleteOnExit();
ClassInjector.UsingInstrumentation.of(temp, BOOTSTRAP, instrumentation)
.inject(singletonMap(new ForLoadedType(INTERCEPTOR_CLASS), read(INTERCEPTOR_CLASS)));
}
}
拦截器
import javax.swing.JComponent;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.asm.Advice.Argument;
import net.bytebuddy.asm.Advice.This;
public class JLabelInterceptor {
@Advice.OnMethodEnter()
public static void setText(@This Object label, @Argument(0) String text) {
((JComponent) label).putClientProperty("html.disable", Boolean.TRUE);
System.out.println("Label text is " + text);
}
}
例子
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame("JList Test");
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] selections = {"<html><img src='http:\\\\invalid\\url'>", "<html><H1>Hello</h1></html>", "orange", "dark blue"};
JList list = new JList(selections);
list.setSelectedIndex(1);
System.out.println(list.getSelectedValue());
JLabel jLabel = new JLabel("<html><h2>standard Label</h2></html>");
frame.add(new JScrollPane(list));
frame.add(jLabel);
frame.pack();
frame.setVisible(true);
}

运行示例
编译 Java 代理,然后运行示例:
java -javaagent:agent.jar -jar example.jar
注意:当使用 Maven 构建代理 Jar 时,我必须在 POM 中放入以下配置来设置清单:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Can-Redefine-Classes>true</Can-Redefine-Classes>
<Can-Retransform-Classes>true</Can-Retransform-Classes>
<Agent-Class>example.JLabelAgent</Agent-Class>
<Premain-Class>example.JLabelAgent</Premain-Class>
<Boot-Class-Path>byte-buddy-1.10.14.jar</Boot-Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>