Accessing the impl classes via reflection only
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 05 Nov 2013 12:54:34 +0100
changeset 316b16cc01038b9
parent 315 b3a78c2e5c33
child 317 6af7542ab0f8
Accessing the impl classes via reflection only
boot-fx/src/main/java/org/apidesign/html/boot/fx/Dbgr.java
boot-fx/src/main/java/org/apidesign/html/boot/fx/FXInspect.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/boot-fx/src/main/java/org/apidesign/html/boot/fx/Dbgr.java	Tue Nov 05 12:54:34 2013 +0100
     1.3 @@ -0,0 +1,66 @@
     1.4 +/**
     1.5 + * HTML via Java(tm) Language Bindings
     1.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details. apidesign.org
    1.16 + * designates this particular file as subject to the
    1.17 + * "Classpath" exception as provided by apidesign.org
    1.18 + * in the License file that accompanied this code.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License
    1.21 + * along with this program. Look for COPYING file in the top folder.
    1.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    1.23 + */
    1.24 +
    1.25 +package org.apidesign.html.boot.fx;
    1.26 +
    1.27 +import java.lang.reflect.Method;
    1.28 +import java.util.logging.Level;
    1.29 +import javafx.scene.web.WebEngine;
    1.30 +import javafx.util.Callback;
    1.31 +import static org.apidesign.html.boot.fx.AbstractFXPresenter.LOG;
    1.32 +
    1.33 +/** Debugger bridge to shield us from propriatory impl APIs.
    1.34 + *
    1.35 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.36 + */
    1.37 +final class Dbgr {
    1.38 +    final Object dbg;
    1.39 +    final Method sendMsg;
    1.40 +    
    1.41 +    Dbgr(WebEngine eng, Callback<String,Void> callback) {
    1.42 +        Object d;
    1.43 +        Method m;
    1.44 +        try {
    1.45 +            d = eng.getClass().getMethod("impl_getDebugger").invoke(eng); // NOI18N
    1.46 +            Class<?> debugger = eng.getClass().getClassLoader().loadClass("com.sun.javafx.scene.web.Debugger"); // NOI18N
    1.47 +            debugger.getMethod("setEnabled", boolean.class).invoke(d, true); // NOI18N
    1.48 +            debugger.getMethod("setMessageCallback", Callback.class).invoke(d, callback); // NOI18N
    1.49 +            m = debugger.getMethod("sendMessage", String.class); // NOI18N
    1.50 +        } catch (Exception ex) {
    1.51 +            LOG.log(Level.INFO, null, ex);
    1.52 +            d = null;
    1.53 +            m = null;
    1.54 +        }
    1.55 +        dbg = d;
    1.56 +        sendMsg = m;
    1.57 +    }
    1.58 +
    1.59 +    void sendMessage(String msg) {
    1.60 +        try {
    1.61 +            if (dbg != null) {
    1.62 +                sendMsg.invoke(dbg, msg);
    1.63 +            }
    1.64 +        } catch (Exception ex) {
    1.65 +            LOG.log(Level.INFO, null, ex);
    1.66 +        }
    1.67 +    }
    1.68 +    
    1.69 +}
     2.1 --- a/boot-fx/src/main/java/org/apidesign/html/boot/fx/FXInspect.java	Tue Nov 05 09:04:29 2013 +0100
     2.2 +++ b/boot-fx/src/main/java/org/apidesign/html/boot/fx/FXInspect.java	Tue Nov 05 12:54:34 2013 +0100
     2.3 @@ -20,7 +20,6 @@
     2.4   */
     2.5  package org.apidesign.html.boot.fx;
     2.6  
     2.7 -import com.sun.javafx.scene.web.Debugger;
     2.8  import java.io.IOException;
     2.9  import java.io.ObjectInputStream;
    2.10  import java.io.ObjectOutputStream;
    2.11 @@ -32,7 +31,6 @@
    2.12  import javafx.application.Platform;
    2.13  import javafx.scene.web.WebEngine;
    2.14  import javafx.util.Callback;
    2.15 -import org.openide.util.Exceptions;
    2.16  
    2.17  /**
    2.18   *
    2.19 @@ -44,6 +42,7 @@
    2.20      
    2.21      private final WebEngine engine;
    2.22      private final ObjectInputStream input;
    2.23 +    private Dbgr dbg;
    2.24      
    2.25      private FXInspect(WebEngine engine, int port) throws IOException {
    2.26          this.engine = engine;
    2.27 @@ -73,9 +72,7 @@
    2.28          Platform.runLater(new Runnable() {
    2.29              @Override
    2.30              public void run() {
    2.31 -                Debugger debugger = engine.impl_getDebugger();
    2.32 -                debugger.setEnabled(true);
    2.33 -                debugger.setMessageCallback(new Callback<String,Void>() {
    2.34 +                dbg = new Dbgr(engine, new Callback<String,Void>() {
    2.35                      @Override
    2.36                      public Void call(String message) {
    2.37                          try {
    2.38 @@ -104,12 +101,12 @@
    2.39                  Platform.runLater(new Runnable() {
    2.40                      @Override
    2.41                      public void run() {
    2.42 -                        engine.impl_getDebugger().sendMessage(message);
    2.43 +                        dbg.sendMessage(message);
    2.44                      }
    2.45                  });
    2.46              }
    2.47          } catch (IOException ioex) {
    2.48 -            ioex.printStackTrace();
    2.49 +            LOG.log(Level.WARNING, null, ex);
    2.50          }
    2.51      }
    2.52  }