2010-10-02 5 views
1

J'utilise Google Window Builder Pro pour SWT et nous utilisons beaucoup de composants personnalisés ici. Les composants reposent sur l'utilisation dans notre framework, mais cela les rend inutilisables dans Window Builder (les exeptions sont lancées lorsqu'elles sont utilisées en dehors de notre framework, comme dans Window Builder). Comment puis-je détecter que Window Builder utilise nos composants pour ignorer le code qui repose sur notre infrastructure?Detect Window Builder

Répondre

2

J'ai implémenté une fonction utilitaire qui vide un StackTrace et recherche des éléments dans les instanciations qu'il contient. Cela fonctionne parfaitement:

/** 
* 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; 
} 
1

Vous pouvez imprimer la Stacktrace sous forme de chaîne de cette façon (la méthode getStackTraceAsString() est absent de la réponse de Daniel):

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 
}