{"id":232,"date":"2008-09-02T09:49:09","date_gmt":"2008-09-02T08:49:09","guid":{"rendered":"http:\/\/blog.sahits.ch\/?p=232"},"modified":"2017-12-24T21:42:28","modified_gmt":"2017-12-24T20:42:28","slug":"how-to-debug-a-java-emitter-template","status":"publish","type":"post","link":"http:\/\/sahits.ch\/blog\/blog\/2008\/09\/02\/how-to-debug-a-java-emitter-template\/","title":{"rendered":"How to debug a Java Emitter template"},"content":{"rendered":"<p>This is a tricky task. The generation of a piece of code &#8211; let&#8217;s assume that it is a Java class &#8211; is based on a java emitter template that defines with literal constants and JSP like syntax the code of the resulting class. To create a source file a Java generator class is generated from the template. This generator class produces dour output Java source file.<br \/>\n<!--more--><br \/>\nSetting a break point in the template and start the JET generator in debug will not work, since the actual errors occur in the class that is generated from the template and there are no breakpoints set.<br \/>\nSo let us assume your call of the JET generator looks something like this:<br \/>\n<code><br \/>\n      monitor = createIfNull(_monitor);<br \/>\n      Config config = getConfig();<br \/>\n      String templateURI=config.getTemplateFullUri();<br \/>\n      JETEmitter emitter = new JETEmitter(templateURI, getClass().getClassLoader());<br \/>\n      Monitor sub = new BasicMonitor.EclipseSubProgress(monitor, 1);<br \/>\n      emitter.addVariable(\"model\", ModelPlugin.getPluginID());<br \/>\n      jetEmiterProjectName=emitter.getProjectName();<br \/>\n      String result = emitter.generate(sub, new Object []{ config.getModel() });<br \/>\n      return result;<br \/>\n<\/code><br \/>\nListing from the class ch.sahits.codegen.java.generator.JETGatway from the Eclipse plug-in ch.sahits.codegen.java<br \/>\nIf you have the sources of the EMF available you can put a breakpoint at the second to last line of the above listing and step into the emitter. Work your way through until you reach this code fragment:<br \/>\n<code><br \/>\n        IMarker [] markers = targetFile.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE);<br \/>\n        boolean errors = false;<br \/>\n        for (int i = 0; i < markers.length; ++i)\n        {\n          IMarker marker = markers[i];\n          if (marker.getAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO) == IMarker.SEVERITY_ERROR)\n          {\n            errors = true;\n            subProgressMonitor.subTask\n              (marker.getAttribute(IMarker.MESSAGE) + \" : \" + \n                 (CodeGenPlugin.getPlugin().getString\n                   (\"jet.mark.file.line\", \n                    new Object []\n                    {\n                      targetFile.getLocation(), \n                      marker.getAttribute(IMarker.LINE_NUMBER)\n                    })));\n          }\n        }<\/code><\/p>\n<p>Belongs to org.eclipse.emf.codegen.jet.JETEmitter.EclipseHelper.initialize(Monitor,JETEmitter)<br \/>\nThere you see a concrete error message is sent to the monitor, but since use the set up you do you won't see it.<br \/>\nThe above method may work fine if you have access to the source whenever you need it and the debugging is no regular occurrence (since this is a nuisance).<br \/>\nThe initialize method is called from the JETEmitter.generate(Monitor,Object[]) method if the initialisation did not already occur. So you can provide your own implementation of the initialize method and explicitly call it before generating the code.<br \/>\nI did this by coping the EclipseHelper class and changing the above code:<\/p>\n<p><\/code><code>\t\t\tIMarker [] markers = targetFile.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE);<br \/>\n\t        boolean errors = false;<br \/>\n\t        for (int i = 0; i < markers.length; ++i)\n\t        {\n\t          IMarker marker = markers[i];\n\t          if (marker.getAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO) == IMarker.SEVERITY_ERROR)\n\t          {\n\t            errors = true;\n\t            String errorMessage = marker.getAttribute(IMarker.MESSAGE) + \" : \" + \n                (CodeGenPlugin.getPlugin().getString\n \t                   (\"jet.mark.file.line\", \n \t                    new Object []\n \t                    {\n \t                      targetFile.getLocation(), \n \t                      marker.getAttribute(IMarker.LINE_NUMBER)\n \t                    }));\n\t            Logging.addMessage(getClass(), errorMessage);\n\t            Logging.log(new RuntimeException(errorMessage));\n\t            subProgressMonitor.subTask(errorMessage);\n\t          }\n\t        }<\/code><\/p>\n<p>Listening from ch.sahits.codegen.java.internal.generator.jet.JETEmitterInitializer<br \/>\nThe above code results in logging an error. Alternatively you can just put it out on the console.<br \/>\nBefore you call the generate method you have to call the initialize method.<\/p>\n<p><\/code><code>      JETEmitter emitter = new JETEmitter(templateURI, getClass().getClassLoader());<br \/>\n      Monitor sub = new BasicMonitor.EclipseSubProgress(monitor, 1);<br \/>\n      emitter.addVariable(\"model\", ModelPlugin.getPluginID());<br \/>\n      jetEmiterProjectName=emitter.getProjectName();<br \/>\n      new JETEmitterInitializer(emitter).initialize(monitor);<br \/>\n      String result = emitter.generate(sub, new Object []{ config.getModel() });<br \/>\n<\/code><br \/>\nMentioned Resources:<br \/>\n<a href=\"http:\/\/codegenjava.svn.sourceforge.net\/viewvc\/codegenjava\/plugin\/src\/ch\/sahits\/codegen\/java\/generator\/JETGateway.java\">ch.sahits.codegen.java.generator.JETGateway<\/a><br \/>\n<a href=\"http:\/\/dev.eclipse.org\/viewcvs\/index.cgi\/org.eclipse.emf\/org.eclipse.emf\/plugins\/org.eclipse.emf.codegen\/src\/org\/eclipse\/emf\/codegen\/jet\/JETEmitter.java?root=Modeling_Project&#038;view=co\">org.eclipse.emf.codegen.jet.JETEmitter<\/a><br \/>\n<a href=\"http:\/\/codegenjava.svn.sourceforge.net\/viewvc\/codegenjava\/plugin\/src\/ch\/sahits\/codegen\/java\/internal\/generator\/jet\/JETEmitterInitializer.java\">ch.sahits.codegen.java.internal.generator.jet.JETEmitterInitializer<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a tricky task. The generation of a piece of code &#8211; let&#8217;s assume that it is a Java class &#8211; is based on a java emitter template that defines with literal constants and JSP like syntax the code of the resulting class. To create a source file a Java generator class is generated &hellip; <a href=\"http:\/\/sahits.ch\/blog\/blog\/2008\/09\/02\/how-to-debug-a-java-emitter-template\/\" class=\"more-link\"><span class=\"screen-reader-text\">\u201eHow to debug a Java Emitter template\u201c <\/span>weiterlesen<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,6],"tags":[304,301,93,300],"class_list":["post-232","post","type-post","status-publish","format-standard","hentry","category-java","category-programmieren","tag-eclipse","tag-java","tag-jet","tag-programmieren"],"_links":{"self":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/comments?post=232"}],"version-history":[{"count":5,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/232\/revisions"}],"predecessor-version":[{"id":237,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/232\/revisions\/237"}],"wp:attachment":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/media?parent=232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/categories?post=232"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/tags?post=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}