1 / 94

Better Java with JDeveloper 10g

Better Java with JDeveloper 10g. Presented by Dustin Marx. New or significantly altered slide. Minor change or addition to slide. Significant details in “Notes” section of slide (see electronic version to see these “Notes”). Notes. Symbols Used in These Presentation Slides. Changes

kaipo
Télécharger la présentation

Better Java with JDeveloper 10g

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Better Java with JDeveloper 10g Presented by Dustin Marx

  2. New or significantly altered slide Minor change or addition to slide Significant details in “Notes” section of slide (see electronic version to see these “Notes”) Notes Symbols Used in These Presentation Slides Changes since slides were printed Because the slides are submitted for printing over one week in advance and in attempt to reduce paper used to print out the handouts, some changes and additions were made to the slides between printing and the presentation. Some slides were taken out for printing and added back in for this presentation. Latest version will be made available at RMOUG site and at http://www.geocities.com/dustinmarx/. Better Java with JDeveloper 10g

  3. Two Versions of the Same Story • “JDeveloper 10g” is label for several versions of the JDeveloper IDE • Some versions start with “9” • “g” is for “Grid” • This presentation primarily presumes JDeveloper 10g as JDeveloper 9.0.5.x • Some related changes and enhancements of JDeveloper 10g Release 2 (10.1.x) are mentioned in this presentation • The associated paper exclusively treats JDeveloper 10g as JDeveloper 9.0.5.x Better Java with JDeveloper 10g

  4. Recent JDeveloper Versions *Developer Preview Better Java with JDeveloper 10g

  5. Better Java with JDeveloper 10g: Agenda • JDeveloper 10g Analysis Tools Overview • JDeveloper 10g Profilers • Memory Profiler • Execution Profiler • Event Profiler • JDeveloper 10g CodeCoach • JDeveloper 10g Code Audit • JDeveloper 10g Code Metrics • Better Java with the JDeveloper 10g Tools • Optimization and Code Improvement Best Practices • Conclusion Better Java with JDeveloper 10g

  6. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion JDeveloper 10g: The ‘J’ is for Java • JDeveloper 10g provides tools for the Java Development Lifecycle (SDLC) • Modeling/Design • JDeveloper models as activity, class, or use case • Implementation • Code Insight, color syntax, automatic code generation, much more • Debugging and Testing • Built-in debugger, JUnit Extension • Optimization / Performance Tuning • Analysis Tools covered in this presentation Better Java with JDeveloper 10g

  7. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion JDeveloper 10g Code Analysis Tools • JDeveloper 10g provides suite of tools for comprehensive code analysis and optimization • Some tools are static • Examine code without executing it • Identify common coding standards problems and complexity problems • Some tools are dynamic • Execute code to identify potential problems and areas for improvement • Require Oracle Java Compiler (ojc) and execution in the Oracle Java Virtual Machine (ojvm) Better Java with JDeveloper 10g

  8. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion More on JDeveloper 10g Code Analysis Tools • JDeveloper Analysis Tools Share Many Traits • Can be run easily within JDeveloper IDE • Output can be viewed in Log Window or in standalone output file (text, HTML, .opr, etc. – different among tools) • Can be run from the command line (all but Profilers) • Output displayed on screen or redirected to external file • Can be customized to best fit your development environment • Provide guidance to potential problems and issues • Knowledgeable human review should often be included in process and before making suggested changes • Multiple output/report formats • IDE log window, HTML, XML, and more Better Java with JDeveloper 10g

  9. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion KEY Graphical Survey of JDeveloper 10g Tools Increased Maintainability Increased Performance Better Java with JDeveloper 10g

  10. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion JDeveloper 10g Code Analysis Tools: IDE or Command-line? • Advantages of IDE • Easier to use in many cases • Automatic code highlighting for problem areas • Availability of Profiler tools • Advantages of Command-line • Can use as part of batch / nightly builds • Easy integration with ANT, FOP, other scripted tools • Expanded non-IDE output options • Can be used with IDEs other than JDeveloper Better Java with JDeveloper 10g

  11. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion JDeveloper 10g Tools: IDE and Command-line Better Java with JDeveloper 10g

  12. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Command-line Oracle Java Compiler (OJC) Example ojc -sourcepath <<jdev_home>\jdev\mywork\BetterJava\Project\src -classpath %CLASSPATH%;<<jdev_home>>\jdev\mywork\BetterJava\Project\classes -d <<jdev_home>>\jdev\mywork\BetterJava\Project\classes -verbose -noquiet -g <<jdev_home>>\jdev\mywork\BetterJava\Project\src\org\rmoug\marx\betterjava\*.java An even easier way to perform the above is to place all the options in a text file known as a Response File (see later slide) Better Java with JDeveloper 10g

  13. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion OJC: Command-line Response Files • Place all command-line tool options in a text file (named something like cc_args.txt) • Can have each ojc option on a different line • Can include comments in file with ; delimiter • Compile with Oracle Java Compiler • ojc @cc_args.txt • Compare to verbosity on previous slide Better Java with JDeveloper 10g

  14. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion OJC Response File Example ocj_args.txt -sourcepath <<jdev_home>>\jdev\mywork\BetterJava\Project\src -classpath %CLASSPATH%;<<jdev_home>>\jdev\mywork\BetterJava\Project\classes -d <<jdev_home>>\jdev\mywork\BetterJava\Project\classes -verbose -noquiet -g <<jdev_home>>\jdev\BetterJava\Project\src\org\rmoug\marx\betterjava\*.java To use the above file, one would run the following command: ojc @ojc_args.txt Better Java with JDeveloper 10g

  15. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Oracle Java Compiler: Command-line and ANT • The easiest way to repeatedly use Oracle Java Compiler (OJC) from the Command-line may be to employ ANT • JDeveloper 10g automatically generates ANT build.xml file with OJC compilation • JDeveloper 10g can read and incorporate existing ANT build.xml files • See next slide for example of building with OJC in ANT Better Java with JDeveloper 10g

  16. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Compiling with OJC and ANT: An Example <!--Configure Oracle Java Compiler--> <property name="build.compiler“ value="oracle.jdeveloper.compiler.ant.taskdefs.OjcAdapter"/> <property name="ojc.assertions.enabled" value="true"/> <property name="ojc.update.imports" value="none"/> <!--Compile Java source files--> <javac destdir="${compile.outdir}" debug="on" encoding="Cp1252"> <classpath refid="classpath"/> <src refid="srcpath"/> <include name="org/rmoug/marx/betterjava/TestBadExample.java"/> <include name="org/rmoug/marx/betterjava/SimpleLogger.java"/> <include name="org/rmoug/marx/betterjava/SimpleDataClass.java"/> <include name="org/rmoug/marx/betterjava/ProfilerExample.java"/> <include name="org/rmoug/marx/betterjava/ParentItem.java"/> <include name="org/rmoug/marx/betterjava/ChildItem.java"/> <include name="org/rmoug/marx/betterjava/BadExampleParent.java"/> <include name="org/rmoug/marx/betterjava/BadExample.java"/> </javac> Better Java with JDeveloper 10g

  17. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion setvars.bat (Windows Only) • Sets up PATH, CLASSPATH, and other significant environmental variables for JDeveloper command-line tool use • Set path to directory with ojc, java, ojmetrics.exe, ojaudit.exe, and JDeveloper IDE’s executable • Use setvars –go to use default environmental variable settings • Use setvars –show to see most significant environmental variables settings • Use setvars –show all to see all related environmental variables settings • Use setvars without options to see usage (help) Better Java with JDeveloper 10g

  18. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion JDeveloper 10g Profilers • JDeveloper 10g Features Three Profilers • Memory Profiler • Execution Profiler • Event Profiler • All Three Profilers are Dynamic • Must compile code with Oracle Java Compiler (ojc) • Must execute code in Oracle Java Virtual Machine (ojvm) • Must execute all code for which profiling is sought • Not running pieces of code impacts profile results Better Java with JDeveloper 10g

  19. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Memory Profiler • Java does have memory leaks! • Long-lived references • JDeveloper Memory Profiler identifies memory leaks • Monitors memory usage on Java heap at periodic intervals • Watch for “Diff Sz” column in Memory Profiler output • Easily locate where Java objects are instantiated • Pause Memory Profiler  Get Allocation Details • Can also use JDeveloper’s Debugger to identify memory leaks! • Heap Window is especially useful Better Java with JDeveloper 10g

  20. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Notes Example of Memory Profiler Output Better Java with JDeveloper 10g

  21. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Another Example of Memory Profiler Output Better Java with JDeveloper 10g

  22. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Notes Still Another Example of Memory Profiler Output Better Java with JDeveloper 10g

  23. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Memory Profiler Customization • Type of Sampling: Automatic (time) vs. Manual • Sampling frequency for automatic sampling • Slider depth: Number of samples available for review • Include/Exclude instances (objects) or different classes • Columns displayed in Memory Profiler output • Use Profiler API (oracle.jdeveloper.profiler) to customize profiling from within source code • ProfilerAPI.startSampling() • ProfilerAPI.stopSampling() Better Java with JDeveloper 10g

  24. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Memory Profiler Configuration Window Better Java with JDeveloper 10g

  25. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Execution Profiler • Perform statistical analysis of application performance • Identify methods that top the call stack Better Java with JDeveloper 10g

  26. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Example of Execution Profiler Output Better Java with JDeveloper 10g

  27. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Execution Profiler Customization • Sample Interval • More samples means better statistical results • Columns displayed in Execution Profiler output • ProfilerAPI Better Java with JDeveloper 10g

  28. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Execution Profiler Configuration Window Better Java with JDeveloper 10g

  29. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Event Profiler • Identify time spent on different events • Focus on events that involve interaction between the application and the Java Virtual Machine • Garbage Collection • Wait • Prepare, Load, and Resolve Class • Synchronization Blocks • Can monitor BC4J events • Most importantly – can monitor own custom events Better Java with JDeveloper 10g

  30. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Example of Event Profiler Better Java with JDeveloper 10g

  31. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Event Profiler Customization • Types/Groups of events to be monitored • Including custom “Discovered Events” • Columns displayed in Event Profiler output • ProfilerAPI Better Java with JDeveloper 10g

  32. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Notes Event Profiler Customization Window Better Java with JDeveloper 10g

  33. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Event Profiler: Events Monitoring • Real power is ability to create and monitor own events • Sun’s HotSpot Java 5.0 (AKA 1.5) JVM provides jvmstat and other JVM monitoring tools • See “Backup” slide for additional details Better Java with JDeveloper 10g

  34. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion CodeCoach • Your own Java code guru • Help improve code by flagging omissions and problems common to routine Java code • Dynamic Analysis Tool • Code must be compiled with Oracle Java Compiler (OJC) • Code must be executed in Oracle Java Virtual Machine (OJVM) • All relevant threads of controlled code should be exercised (executed) to improve result accuracy Better Java with JDeveloper 10g

  35. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion What CodeCoach Tells You: Results • CodeCoach results include the following: • Textual description of recommended change to code • This is referred to as “advice” • As with any advice you might hear, consider it carefully before implementing recommendations • Line number of source code that is subject of advice • Advice Type (four letter uppercase keyword) • See next slide for example of CodeCoach results in the IDE Better Java with JDeveloper 10g

  36. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion CodeCoach Results (IDE Example) Better Java with JDeveloper 10g

  37. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion CodeCoach Results Options • “Hide Messages of This Type” • Hides all messages with matching advice type • “Show Hidden Messages” brings them all back • “Ignore Message” • Adds CodeCoach pragma to source code to prevent that particular message (not all with same advice type) from appearing again • “Apply Fix to Source” • If available, fixes source code according to recommendation • “Go to Source” • Highlights line of source code that CodeCoach has flagged • “Save to HTML” • Saves CodeCoach results to HTML page Better Java with JDeveloper 10g

  38. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion CodeCoach Results & Options (IDE Example) Better Java with JDeveloper 10g

  39. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Properly Using CodeCoach Advice • Quality of CodeCoach advice depends on how you run CodeCoach! • Should run CodeCoach through all significant threads of code • Any code not executed during a CodeCoach run is unknown to CodeCoach • CodeCoach results can be correct for the code it saw, but incorrect when the larger picture is taken into account • Generally, choose to “Go to Source” and analyze recommendation manually rather than choosing “Apply Fix to Source” • Always unit and regression test your code after making CodeCoach-recommended changes Better Java with JDeveloper 10g

  40. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion CodeCoach Customization • Customize CodeCoach Advice Types, Covered Packages, and Level of Advice • Project-level settings • Via command-line or IDE • Customize which advice types to identify • Specific advice types individually or via setting of level • Customize which classes/packages to analyze • For classes, all analyzed same or not at all • File, Class, or Method Level settings • Via in-code (comments) pragmas for more granular scope • Can more specifically tailor each class’s CodeCoach analysis Better Java with JDeveloper 10g

  41. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion CodeCoach Customization: Advice Types • Choose advice types to focus on or ignore • Advice types allow associated with major pieces of CodeCoach advice • Representative keywords are typically four uppercase letters • “ALL” is only exception (three uppercase letters) • Groups of advice types can be selected or ignored • “ALL” displays CodeCoach advice for all advice types • Can use “Level” to select one of ten predetermined advice types mixes • Increasing level indicates increased advice Better Java with JDeveloper 10g

  42. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion CodeCoach Customization: Advice Type Groupings • Class Advice Types (CALL) • CFIN, CSTAT • Method Advice Types (MALL) • MFIN, MSTAT, MPRI • Field Advice Types (FALL) • FFIN, FSTA, FPRI, FUNU, FLOC • Local Variable Advice Types (LALL) • LFIN, LUNU • Memory Improvement Advice Types (NALL) • NALS, NBST, NHTB, NHMP, NSBU, NVCT, NWHT • Miscellaneous Advice Types (OALL) • CDEA, ACST, INST Better Java with JDeveloper 10g

  43. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion CodeCoach Customization: IDE • Check boxes for advice types (or groups of advice types) you want CodeCoach to report by checking on them under “Advice to Report” • Use “Level” bar to select integer between 1 and 10 to specify rough percentages of advice types to be monitored and flagged • Specifically include and/or exclude packages and classes from CodeCoach analysis • Refer to next slide (“CodeCoach Customization Window”) for example of IDE customization Better Java with JDeveloper 10g

  44. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Notes CodeCoach Customization Window Better Java with JDeveloper 10g

  45. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion CodeCoach Customization: Command-line • Instruct CodeCoach via command-line using –Xcc flags and options • -Xcc:level:<<y>> where y is integer 0 through 9 • Corresponds to IDE’s “Level” bar (1 through 10) • -Xcc:enable:<<type>> and -Xcc:disable:<<type>> • where type is “ALL” or one of the more specific four-letter advice types to be specifically identified (enable) or specifically not identified (disable) in CodeCoach run • -Xcc:incl:<<classes>> and -Xcc:excl:<<classes>> • where classes is semicolon-separated list of classes and/or packages to analyze (incl) or ignore (excl) during CodeCoach analysis • Don’t forget the –ojvm option to run CodeCoach on source code in the Oracle Java Virtual Machine • Application must be have been built with OJC and –g option to have CodeCoach analyze it Better Java with JDeveloper 10g

  46. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion CodeCoach Command-line Customization Example:Running CodeCoach with OJVM java –ojvm –Xcodecoach –Xcc:level:9 –Xcc:enable:ALL –Xcc:incl:org.rmoug.marx.betterjava -Xcc:excl:com;java;javax;sun;sunw;oracle –classpath %CLASSPATH% MyBadClassTest The above assumes that MyBadClassTest was built with Oracle Java Compiler and the –g debugger option. Better Java with JDeveloper 10g

  47. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion For More on CodeCoach • See my article on CodeCoach in the next edition of the Rocky Mountain Oracle User’s Group (RMOUG) newsletter • Expected late February/early March • Effective Java by Joshua Block • Much of CodeCoach advice is espoused in this book • Other tools also make use of principles outlined in this book • Opinion: Must-read for the serious Java developer Better Java with JDeveloper 10g

  48. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Code Audit • Static tool – analyzes static code • No OJC compiling or OJVM execution required • Can be run on code that doesn’t compile! • JDeveloper 10g Code Audit audits code for compliance with coding standards • Can have JDeveloper automatically fix flagged coding standards (audit) violations • Typically less risky to code execution than automatic fixes done for dynamic tools Better Java with JDeveloper 10g

  49. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Code Audit Results/Output Options – IDE • Each identified violation listed in IDE with descriptive text and a “priority” • Some violations allow for automatic fix (“Apply …”) with right-click • Violations of same type can be hidden or described with right-click • “Go to Source” (after right-click) highlights line of code associated with identified violation • “Export” to HTML (or text) • Can select default (HTML or simple text) or customized Cascading Style Sheet • Default HTML Cascading Style Sheet • Associates violation with “severity” rather than priority (see later slide) • Adds line number to output to enable quick lookup of associated line in source code Better Java with JDeveloper 10g

  50. JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion Code Audit Results/Output Options (IDE Example) Better Java with JDeveloper 10g

More Related