Search code examples
oracle-adfjdeveloper

ADF: What is a quick and easy way to display a debug/log message on the UI?


So I am debugging some code in my application (dev and test environment) and I am not allowed to use appsloggers or System.out.print or any such logging.

I would like to see the value some of the variables hold at a given time and put messages like "Entered first If loop" and "Enter x function". The code usually exists in ManagedBean, VOImpl and AMImpl and some java handlers.

What is the best way to display something on the UI of my application, like a comment box or a warning message for debugging purposes? It would be great if I can close the message box and code flow continues.


Solution

  • It would be great if I can close the message box and code flow continues.

    What you are describing is debugging. In Java and ADF debugging isn't done in the UI and should not. You can use JDeveloper debugger functionnality on your local instance or by plugging the remote debug functionnality to your target server.

    https://download.oracle.com/otn_hosted_doc/jdeveloper/101301/java_app_dev/debug/deb_p_local.html Debugging a Project in JDeveloper Make sure that your code is compiled with debugging information in Tools then choose ProjectProperties - Compiler before you can make use of some of the debugger features such as viewing arguments and local variables in the Data window.

    See About Debugger Icons to determine the purpose and functions of the various debugger icons displayed on the toolbar or in the debugger windows. Each of these commands is also available from the Debug main menu.
    
    To set breakpoints and step through your code:
    
    In a source editor, set a breakpoint on an executable statement by clicking in the margin to the left of the statement.
    The unverified breakpoints icon red_dot appears in the left margin.
    Select D ebug then choose Debug [filename.java] or click Debug Button on the toolbar.
    The class runs and stops at the first breakpoint.
    From the toolbar, click step_into_toolbar_icon Step Into to trace into a method call or click step_over_toolbar_icon Step Over to step over a method call.
    Look in the Stack window to examine the sequence of method calls that brought your program to its current state. Double-click a method to display the associated source code in the source editor.
    In the Smart Data and Data windows, examine the arguments and variables.
    Display the Threads window to see the status of other threads in your program.
    

    Remote debugging on your target server : https://aboutsoa.wordpress.com/2009/03/10/remote-debugging-in-jdeveloper/

    Open Enterprise Manager (http://{hostname}:{port}/em) and click on the oc4j_soa instance.
    Click the Administration tab.
    Click on Server Properties.
    Under command-line options add the following line: -Xrunjdwp:transport=dt_socket,server=y,address=9901,suspend=n.
    Click Apply and then be sure to restart the OC4J server.
    Next you need to configure your JDeveloper project for remote debugging:
    
    Right-click on the project and choose Project Properties.
    Click the Run/Debug option.
    Click on the default run configuration and click Edit…
    Under the Launch Settings option make sure the Remote Debugging and Profiling checkbox is checked.
    Choose the Debug > Remote option.
    

    If you still want to display and alert on the UI you can trigger a javascript alert function programmatically.
    https://cedricleruth.com/how-to-execute-client-javascript-in-an-adf-java-bean-action/

      this.executeClientJavascript("alert('HERE IS MY VARIABLE VALUE, i should really use the debugger for this :)" + YOURJAVAVARTOSTRING + " ')");
    
    
    //You should put this function in a util java class if you want to use it in multiple bean
    public static void executeClientJavascript(String script) {
     FacesContext facesContext = FacesContext.getCurrentInstance();
     ExtendedRenderKitService service = Service.getRenderKitService(facesContext, ExtendedRenderKitService.class);
     service.addScript(facesContext, script);
    }