【问题标题】:How do I modify a class before it's been loaded?如何在加载类之前修改它?
【发布时间】:2018-06-05 04:22:56
【问题描述】:

我需要对 JavaFX WebView 类 to make it render even when not visible 进行手术。为了实现这一点,我找到了Javassist,但是当我尝试使用它时,我收到了这个错误:

java.lang.IllegalArgumentException: Can not set javafx.scene.web.WebView field sample.Controller.webView to javafx.scene.web.WebView

我认为这是因为该成员是在修改之前定义的?我不知道。目前,我没有修改类中的任何内容,只是使用代码加载、解冻(?)并保存它:

CtClass webViewClass = ClassPool.getDefault().get("javafx.scene.web.WebView");
webViewClass.defrost();
webViewClass.toClass();

这是我最小的可重现示例。一、Main.java

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.NotFoundException;

public class Main extends Application {
    public static void main(String[] args) {
        try {
            CtClass webViewClass = ClassPool.getDefault().get("javafx.scene.web.WebView");
            webViewClass.defrost();
            webViewClass.toClass();
        } catch (NotFoundException | CannotCompileException e) {
            e.printStackTrace();
        }
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setScene(new Scene(root, 800, 600));
        primaryStage.show();
    }
}

这些是Controller.java 文件的内容:

package sample;

import javafx.fxml.FXML;
import javafx.scene.web.WebView;

public class Controller {
    @FXML
    private WebView webView;

    @FXML
    private void initialize() {
        webView.getEngine().load("http://stackoverflow.com");
    }
}

这是视图,sample.fxml:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.web.WebView?>
<AnchorPane xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="sample.Controller">
    <WebView fx:id="webView" minHeight="-Infinity" minWidth="-Infinity"/>
</AnchorPane>

完整的例外是:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException: 
/C:/Users/pupeno/Documents/Dashman/code/experiments/webviewwoes/out/production/webviewwoes/sample/sample.fxml:8

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at sample.Main.start(Main.java:27)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    ... 1 more
Caused by: java.lang.IllegalArgumentException: Can not set javafx.scene.web.WebView field sample.Controller.webView to javafx.scene.web.WebView
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
    at java.lang.reflect.Field.set(Field.java:764)
    at javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1163)
    at javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:103)
    at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:857)
    at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:765)
    at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
    ... 17 more
Exception running application sample.Main

Process finished with exit code 1

【问题讨论】:

  • 抛出哪一行?
  • @GhostCat:我添加了完整的异常,包括堆栈跟踪、行号、文件等。
  • 从未使用过 javassist,但是,如果在您的代码中将 webViewClass.toClass();webViewClass.writeFile(); 交换,则错误消失了。
  • @DVarga:但这会修改​​内存类吗? wirteFile() 似乎另有建议。
  • 这里的目标是什么?即使 WebView 不可见也能打印或截屏?

标签: java javafx javassist


【解决方案1】:

很可能有两个版本的 Webview 由两个不同的 ClassLoader 加载,至少我希望只有在这种情况下才会出现这样的异常。

请注意 javassist tutorial 中的这一段:

如果程序在一些应用服务器上运行,例如 JBoss 和 Tomcat,toClass() 使用的上下文类加载器可能是 不当。在这种情况下,您会看到意想不到的 类转换异常。为避免此异常,您必须明确给出 toClass() 的适当类加载器。例如,如果 bean 是您的 session bean 对象,然后是以下代码:

CtClass cc = ...; c类= cc.toClass(bean.getClass().getClassLoader());会工作。你应该 给 toClass() 加载了你的程序的类加载器(在 上面的例子,bean对象的类)。

调用 toClass() 使线程的上下文类加载器加载类。我假设当 FXMLLoader 加载视图时,它会使用不同的类加载器加载类,并且以某种方式混淆了,因此您最终会遇到此异常。另一个可能的结果是一切都加载正常,但是您的修改不起作用,因为 FXMLLoader 加载了未修改的类。

做这样的事情:

ClassPool classPool = ClassPool.getDefault();
CtClass webViewClass = classPool.get("javafx.scene.web.WebView");
webViewClass.defrost();
classPool.toClass(webViewClass, FXMLLoader.class.getClassLoader(), null);

【讨论】:

  • 我试过webViewClass.toClass(FXMLLoader.getDefaultClassLoader());,但我得到了同样的异常。
  • webViewClass.toClass(FXMLLoader.class.getClassLoader()) 没有崩溃,让我们看看它是否有效。
猜你喜欢
  • 1970-01-01
  • 2023-03-11
  • 2013-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多