Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检测窗口生成器_Java_Swt - Fatal编程技术网

Java 检测窗口生成器

Java 检测窗口生成器,java,swt,Java,Swt,我正在为SWT使用Google Window Builder Pro,我们在这里使用了许多自定义组件。组件依赖于在我们的框架内使用,但这使得它们在Window Builder中不可用(在我们的框架外使用时,例如在Window Builder中使用时,会抛出例外) 如何检测Window Builder正在使用我们的组件跳过依赖于我们框架的代码?我实现了一个实用函数,该函数转储StackTrace并从其中的实例化中查找内容。这非常有效: /** * Designer mode. This is u

我正在为SWT使用Google Window Builder Pro,我们在这里使用了许多自定义组件。组件依赖于在我们的框架内使用,但这使得它们在Window Builder中不可用(在我们的框架外使用时,例如在Window Builder中使用时,会抛出例外)


如何检测Window Builder正在使用我们的组件跳过依赖于我们框架的代码?

我实现了一个实用函数,该函数转储StackTrace并从其中的实例化中查找内容。这非常有效:

/**
 * Designer mode. This is used to detect if the widgets are running
 * from SWT designer, because in this case we have to skip some
 * initialization code.
 */
private static Boolean designerMode;

/**
 * This is used to detect if the widgets are running
 * from SWT designer, because in this case we have to skip some
 * initialization code.
 */
public static boolean isDesignerMode() {
    if( designerMode == null ) {
        String s = StacktraceUtils.getStackTraceAsString(
                new RuntimeException("Just to get the Stacktrace."));
        designerMode = s.contains("com.instantiations.designer");  
    }
    return designerMode;
}

可以通过这种方式将Stacktrace打印为字符串(Daniel的答案中缺少getStackTraceAsString()方法):

public static String getStackTraceAsString(Throwable t) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    t.printStackTrace(pw);
    return sw.toString(); // stack trace as a string
}