Better Java with JDeveloper 10 g Training Days
Better Java with JDeveloper 10 g Training Days 2005 Presented by Dustin Marx 02/09/2005 Better Java with JDeveloper 10 g
Symbols Used in These Presentation Slides New or significantly altered slide Minor change or addition to slide Notes Changes since slides were printed Significant details in “Notes” section of slide (see electronic version to see these “Notes”) 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/. 02/09/2005 Better Java with JDeveloper 10 g 2 Training Days 2005
Two Versions of the Same Story • “JDeveloper 10 g” is label for several versions of the JDeveloper IDE – Some versions start with “ 9” – “g” is for “Grid” • This presentation primarily presumes JDeveloper 10 g as JDeveloper 9. 0. 5. x • Some related changes and enhancements of JDeveloper 10 g Release 2 (10. 1. x) are mentioned in this presentation • The associated paper exclusively treats JDeveloper 10 g as JDeveloper 9. 0. 5. x 02/09/2005 Better Java with JDeveloper 10 g 3 Training Days 2005
Recent JDeveloper Versions Version Numbers Name 10. 1. 3 JDeveloper 10 g Release 2* 9. 0. 5. 1, 9. 0. 5. 2, 10. 1. 2 JDeveloper 10 g 9. 0. 4. 1, 9. 0. 4. 0 JDeveloper 9 i 9. 0. 3. 3, 9. 0. 3. 2, 9. 0. 3. 1, 9. 0. 3 JDeveloper 9 i 9. 0. 2 JDeveloper 9 i *Developer Preview 02/09/2005 Better Java with JDeveloper 10 g 4 Training Days 2005
Better Java with JDeveloper 10 g: Agenda • JDeveloper 10 g Analysis Tools Overview • JDeveloper 10 g Profilers – Memory Profiler – Execution Profiler – Event Profiler • • • JDeveloper 10 g Code. Coach JDeveloper 10 g Code Audit JDeveloper 10 g Code Metrics Better Java with the JDeveloper 10 g Tools Optimization and Code Improvement Best Practices Conclusion 02/09/2005 Better Java with JDeveloper 10 g 5 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion JDeveloper 10 g: The ‘J’ is for Java • JDeveloper 10 g 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 02/09/2005 Better Java with JDeveloper 10 g 6 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion JDeveloper 10 g Code Analysis Tools • JDeveloper 10 g 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) 02/09/2005 Better Java with JDeveloper 10 g 7 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion More on JDeveloper 10 g 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 02/09/2005 Better Java with JDeveloper 10 g 8 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Increased Maintainability Graphical Survey of JDeveloper 10 g Tools KEY Increased Performance 02/09/2005 Better Java with JDeveloper 10 g 9 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion JDeveloper 10 g 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 02/09/2005 Better Java with JDeveloper 10 g 10 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion JDeveloper 10 g Tools: IDE and Command-line JDeveloper 10 g Analysis Tool IDE Command-line Memory Profiler Run Memory Profile N/A Execution Profiler Run Execution Profile N/A Event Profiler Run Event Profile N/A Code. Coach Run Code. Coach ojc -g and then java –ojvm –Xcodecoach Code Audit Run Audit ojaudit. exe Code Metrics Run Measure ojmetrics. exe 02/09/2005 Better Java with JDeveloper 10 g 11 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Command-line Oracle Java Compiler (OJC) Example ojc -sourcepath <<jdev_home>jdevmyworkBetter. JavaProjectsrc -classpath %CLASSPATH%; <<jdev_home>>jdevmyworkBetter. JavaProjectclas ses -d <<jdev_home>>jdevmyworkBetter. JavaProjectclasses -verbose -noquiet -g <<jdev_home>>jdevmyworkBetter. JavaProjectsrcorgrmougma rxbetterjava*. 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) 02/09/2005 Better Java with JDeveloper 10 g 12 Training Days 2005
JDeveloper 10 g Profilers Code. Coach 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 02/09/2005 Better Java with JDeveloper 10 g 13 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion OJC Response File Example ocj_args. txt -sourcepath <<jdev_home>>jdevmyworkBetter. JavaProjectsrc -classpath %CLASSPATH%; <<jdev_home>>jdevmyworkBetter. JavaProjectclasses -d <<jdev_home>>jdevmyworkBetter. JavaProjectclasses -verbose -noquiet -g <<jdev_home>>jdevBetter. JavaProjectsrcorgrmougmarxbetterjava*. java To use the above file, one would run the following command: ojc @ojc_args. txt 02/09/2005 Better Java with JDeveloper 10 g 14 Training Days 2005
JDeveloper 10 g Profilers Code. Coach 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 10 g automatically generates ANT build. xml file with OJC compilation • JDeveloper 10 g can read and incorporate existing ANT build. xml files • See next slide for example of building with OJC in ANT 02/09/2005 Better Java with JDeveloper 10 g 15 Training Days 2005
JDeveloper 10 g Profilers Code. Coach 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. Ojc. Adapter"/> <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="Cp 1252"> <classpath refid="classpath"/> <src refid="srcpath"/> <include name="org/rmoug/marx/betterjava/Test. Bad. Example. java"/> <include name="org/rmoug/marx/betterjava/Simple. Logger. java"/> <include name="org/rmoug/marx/betterjava/Simple. Data. Class. java"/> <include name="org/rmoug/marx/betterjava/Profiler. Example. java"/> <include name="org/rmoug/marx/betterjava/Parent. Item. java"/> <include name="org/rmoug/marx/betterjava/Child. Item. java"/> <include name="org/rmoug/marx/betterjava/Bad. Example. Parent. java"/> <include name="org/rmoug/marx/betterjava/Bad. Example. java"/> </javac> 02/09/2005 Better Java with JDeveloper 10 g 16 Training Days 2005
JDeveloper 10 g Profilers Code. Coach 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) 02/09/2005 Better Java with JDeveloper 10 g 17 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion JDeveloper 10 g Profilers • JDeveloper 10 g 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 02/09/2005 Better Java with JDeveloper 10 g 18 Training Days 2005
JDeveloper 10 g Profilers Code. Coach 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 02/09/2005 Better Java with JDeveloper 10 g 19 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Example of Memory Profiler Output 02/09/2005 Better Java with JDeveloper 10 g Conclusion Notes 20 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Another Example of Memory Profiler Output 02/09/2005 Better Java with JDeveloper 10 g 21 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Still Another Example of Memory Profiler Output 02/09/2005 Better Java with JDeveloper 10 g Notes 22 Training Days 2005
JDeveloper 10 g Profilers Code. Coach 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 – Profiler. API. start. Sampling() – Profiler. API. stop. Sampling() 02/09/2005 Better Java with JDeveloper 10 g 23 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Memory Profiler Configuration Window 02/09/2005 Better Java with JDeveloper 10 g 24 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Execution Profiler • Perform statistical analysis of application performance • Identify methods that top the call stack 02/09/2005 Better Java with JDeveloper 10 g 25 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Example of Execution Profiler Output 02/09/2005 Better Java with JDeveloper 10 g 26 Training Days 2005
JDeveloper 10 g Profilers Code. Coach 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 • Profiler. API 02/09/2005 Better Java with JDeveloper 10 g 27 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Execution Profiler Configuration Window 02/09/2005 Better Java with JDeveloper 10 g 28 Training Days 2005
JDeveloper 10 g Profilers Code. Coach 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 BC 4 J events • Most importantly – can monitor own custom events 02/09/2005 Better Java with JDeveloper 10 g 29 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Example of Event Profiler 02/09/2005 Better Java with JDeveloper 10 g 30 Training Days 2005
JDeveloper 10 g Profilers Code. Coach 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 • Profiler. API 02/09/2005 Better Java with JDeveloper 10 g 31 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Event Profiler Customization Window 02/09/2005 Better Java with JDeveloper 10 g Conclusion Notes 32 Training Days 2005
JDeveloper 10 g Profilers Code. Coach 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 Hot. Spot Java 5. 0 (AKA 1. 5) JVM provides jvmstat and other JVM monitoring tools – See “Backup” slide for additional details 02/09/2005 Better Java with JDeveloper 10 g 33 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Code. Coach • 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 02/09/2005 Better Java with JDeveloper 10 g 34 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion What Code. Coach Tells You: Results • Code. Coach 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 Code. Coach results in the IDE 02/09/2005 Better Java with JDeveloper 10 g 35 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Code. Coach Results (IDE Example) 02/09/2005 Better Java with JDeveloper 10 g 36 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Code. Coach Results Options • “Hide Messages of This Type” – Hides all messages with matching advice type – “Show Hidden Messages” brings them all back • “Ignore Message” – Adds Code. Coach 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 Code. Coach has flagged • “Save to HTML” – Saves Code. Coach results to HTML page 02/09/2005 Better Java with JDeveloper 10 g 37 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Code. Coach Results & Options (IDE Example) 02/09/2005 Better Java with JDeveloper 10 g 38 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Properly Using Code. Coach Advice • Quality of Code. Coach advice depends on how you run Code. Coach! – Should run Code. Coach through all significant threads of code – Any code not executed during a Code. Coach run is unknown to Code. Coach – Code. Coach 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 Code. Coach -recommended changes 02/09/2005 Better Java with JDeveloper 10 g 39 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Code. Coach Customization • Customize Code. Coach Advice Types, Covered Packages, and Level of Advice – Project-level settings § Via command-line or IDE § Customize which advice types to identify o Specific advice types individually or via setting of level § Customize which classes/packages to analyze o For classes, all analyzed same or not at all – File, Class, or Method Level settings § Via in-code (comments) pragmas for more granular scope o Can more specifically tailor each class’s Code. Coach analysis 02/09/2005 Better Java with JDeveloper 10 g 40 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Code. Coach Customization: Advice Types • Choose advice types to focus on or ignore • Advice types allow associated with major pieces of Code. Coach 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 Code. Coach advice for all advice types – Can use “Level” to select one of ten predetermined advice types mixes § Increasing level indicates increased advice 02/09/2005 Better Java with JDeveloper 10 g 41 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Code. Coach 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 02/09/2005 Better Java with JDeveloper 10 g 42 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Code. Coach Customization: IDE • Check boxes for advice types (or groups of advice types) you want Code. Coach 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 Code. Coach analysis • Refer to next slide (“Code. Coach Customization Window”) for example of IDE customization 02/09/2005 Better Java with JDeveloper 10 g 43 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Code. Coach Customization Window 02/09/2005 Better Java with JDeveloper 10 g Conclusion Notes 44 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Code. Coach Customization: Command-line • Instruct Code. Coach 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 Code. Coach 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 Code. Coach analysis • Don’t forget the –ojvm option to run Code. Coach on source code in the Oracle Java Virtual Machine • Application must be have been built with OJC and –g option to have Code. Coach analyze it 02/09/2005 Better Java with JDeveloper 10 g 45 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Code. Coach Command-line Customization Example: Running Code. Coach with OJVM java –ojvm –Xcodecoach –Xcc: level: 9 –Xcc: enable: ALL –Xcc: incl: org. rmoug. marx. betterjava -Xcc: excl: com; javax; sunw; oracle –classpath %CLASSPATH% My. Bad. Class. Test The above assumes that My. Bad. Class. Test was built with Oracle Java Compiler and the –g debugger option. 02/09/2005 Better Java with JDeveloper 10 g 46 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion For More on Code. Coach • See my article on Code. Coach 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 Code. Coach 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 02/09/2005 Better Java with JDeveloper 10 g 47 Training Days 2005
JDeveloper 10 g Profilers Code. Coach 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 10 g 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 02/09/2005 Better Java with JDeveloper 10 g 48 Training Days 2005
JDeveloper 10 g Profilers Code. Coach 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 02/09/2005 Better Java with JDeveloper 10 g 49 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Code Audit Results/Output Options (IDE Example) 02/09/2005 Better Java with JDeveloper 10 g 50 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Code Audit Priorities / Severities IDE (“Priority”) “Medium” HTML (“Severity”) “Convention” “Suggestion” “Violation” “High” “Warning” “Critical” “Error” “Low” Conclusion Notes The IDE associates a “priority” with Code Audit identified violations while the HTML output lists a “severity” for each identified violation. 02/09/2005 Better Java with JDeveloper 10 g 51 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Code Audit Customization • Code Audit reports can be customized to not display certain categories/groups of violations – Cannot suppress individual violation types • Use Audit Profile to specify which audit rules to use (or not use) in analysis • Code Audit can be extended and customized with oracle. jdeveloper. audit package – Also used for Code Metrics 02/09/2005 Better Java with JDeveloper 10 g 52 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Code Audit Customization Example 02/09/2005 Better Java with JDeveloper 10 g Conclusion Notes 53 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Command-Line Code Audit • ojaudit. exe (Windows) • Generates XML output containing same data available in IDE/HTML output – Including line number of relevant source code – Use –quiet option to remove most non-XML output normally included (Oracle header and summary footer information) – Use –style to specify XSLT style sheet that could be applied to Code Audit resultant HTML to produce own HTML similar to IDE’s HTML “Export” • Use ojaudit (without parameters) or ojaudit –help to display usage • Use –fix to have Code Audit address violations 02/09/2005 Better Java with JDeveloper 10 g 54 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Code Metrics • Static tool – analyzes static code – No OJC compiling or OJVM execution required • JDeveloper 10 g Code Metrics measures three metrics: – Depth of Inheritance Tree [DIT] § Your classes will have DIT >= 2 – Number of Statements [NOS] § Number of Java statements in code block – Cyclomatic Complexity [V(G)] § Branching complexity of code § Generally strive for V(G) < 10 o Rough rule of thumb 02/09/2005 Better Java with JDeveloper 10 g 55 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Code Metrics Results/Output Options • “Go to Source” highlights class or method right-clicked on • “Export” saves Code Metrics results as HTML – Same information as in IDE Code Metrics output window – Can use default Cascading Style Sheets (to HTML or to very basic text) or select own CSS to apply in creation of HTML output • “Show Over Threshold Only” only displays Code Metrics findings that have a metric exceeding Threshold set in Metrics Preferences 02/09/2005 Better Java with JDeveloper 10 g 56 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Code Metrics Output Example (IDE) 02/09/2005 Better Java with JDeveloper 10 g 57 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Code Metrics Customization • Code Metrics reports can be customized to focus on “problem level” results • Use Metrics Profile to specify which metrics to evaluate • Threshold can be set for each metric type to control which measurements or metrics are displayed • Code Metrics can be customized and extended with oracle. jdeveloper. audit package – Same as used with Code Audit 02/09/2005 Better Java with JDeveloper 10 g 58 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Code Metrics Customization Example 02/09/2005 Better Java with JDeveloper 10 g 59 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Command-line Code Metrics • ojmetrics. exe (Windows) • Generates XML output containing same data available in IDE/HTML output – Adds line numbers for easier tracing of output to source code – Adds nice descriptions of meanings of metrics [NOS, V(G), DIT] at top of produced output file – XML results file has audit as root tag and other tag names matching the Code Audit output – Use –quiet option to remove most non-XML output normally included (Oracle header and summary footer information) • Use –style to specify XSLT style sheet that could be applied to Code Audit resultant HTML to produce own HTML similar to IDE’s HTML “Export” • Use ojmetrics (without parameters) or ojmetrics –help to display usage 02/09/2005 Better Java with JDeveloper 10 g 60 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Release 2 New Features / Changes for JDeveloper 10 g Release 2 Notes • Combining of similar Code Audit and Code Metrics tool into single Code Audit tool – “Code Audit Rules” similar to Code Audit of 9. 0. 5. x – “Code Audit Metrics” similar to Code Metrics of 9. 0. 5. x – These tools shared many traits even in 9. 0. 5. x • Introduction of Code Assist – Can be executed from “Run Audit …” option – Automatically displays light bulb icon when affected line is highlighted § Versus explicit execution of other Code Audit family of tools – See following slide on Code Assist 02/09/2005 Better Java with JDeveloper 10 g 61 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Release 2 The Multi-Function Code Audit in Release 2 • Code Audit (R 2) contains many audit options (profiles) – “All Metrics” § Java source errors (rules) § Metrics from 9. 0. 5 – “All Rules” § All “Rules” but no “Metrics” or “Code Assists” – “Code Assist Rules” § Most “Rules”, All “Code Assists”, No “Metrics” – “Default Rules” § No “Code Assists” § No “Metrics” § Most “Rules” – “Javadoc Rules” § Only “Javadoc Comments” rules – Your Own Auditing Profile § Custom selected rules 02/09/2005 Better Java with JDeveloper 10 g 62 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Code Assist A Quick Peek at Code Assist • New Feature of JDeveloper 10 g R 2 (10. 1. 3) • Similar functionality to Code. Coach – But do not need to explicitly run tool separately • Light bulb icon highlights available advice – Use Ctrl+Alt+Enter to manually display Code Assist advice – Change/disable Code Assist settings via Tools Preferences (Audit) • Click on light bulb icon to view advice • Click on advice to implement advice – Potential side effects displayed § Opportunity to cancel recommendation rather than accepting it 02/09/2005 Better Java with JDeveloper 10 g 63 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Code Assist 02/09/2005 A Code Assist Example – IDE Notes Better Java with JDeveloper 10 g 64 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Code Assist Current Limitations of Code. Assist • Spacing of accepted Code. Assist recommendations does not always match well with source code indentation • Code. Assist recommendations are currently limited – Not a whole lot of recommendations … yet § Add Javadoc Comment § Separate Declaration and Assignment § Remove Unused Local Variable § Invert If Statement § Negate Expression § Others? – May disagree with some of the recommendations – Some recommendations made regardless of context 02/09/2005 Better Java with JDeveloper 10 g 65 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Better Java Code with JDeveloper 10 g Tools • Improve your Java Code with the JDeveloper Analysis Tools – Use tools together for most comprehensive and accurate results – Take advantage of different purposes of tools to identify different types of problems – Use results of individual tools as input for other JDeveloper tools § For example, identify most significant problems with Profilers and use other tools to improve problem areas 02/09/2005 Better Java with JDeveloper 10 g 66 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion JDeveloper 10 g Tools Improving Java Code. Coach • More efficient classes • More efficient & maintainable methods • More efficient/maintainable fields • More efficient local variables • More efficient Collections memory usage • Minimizing unnecessary instanceof usage • Minimizing constant object propagation problems Memory Profiler Execution Profiler Event Profiler Identify potential memory leaks in code Identify performance problems in application Identify problems or potential problems associated with particular events in the application. • Garbage Collection Events • BC 4 J Events • Customized Events Code Audit Code Metrics Identifies potential coding standard violations including: naming and coding conventions, unused constructs, source code errors, Javadoc problems and omissions, appropriate API usage, and more. Summarizes complexity and size of application including: number of statements (NOS), Depth of Inheritance Tree (DIT), and Cyclomatic/Branching Complexity [V(G)]. 02/09/2005 Better Java with JDeveloper 10 g 67 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Example: Better Java Code with Code. Coach Parent. Item item = new Child. Item(); if ( item instanceof Child. Item ) { name = item. get. Name(); } While instanceof is very useful at times, it is expensive and should not be used unless truly needed. 02/09/2005 Better Java with JDeveloper 10 g 68 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Example: Better Java Code with Code Audit String abused. String = new String(); for ( int i=0; i<10000; ++i ) { abused. String = abused. String + '1'; } Strings are immutable and so a new String must be instantiated each time that “ 1” character is added above. This repeated String construction is expensive and unnecessary. 02/09/2005 Better Java with JDeveloper 10 g 69 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Example: Better Java Code with Code Metrics if () { if () if () } else if () {} 02/09/2005 Cyclomatic complexity here is shown with a red “ 11” because the recommended complexity (10) has been exceeded. Better Java with JDeveloper 10 g 70 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Optimization and Code Improvement Best Practices • Know When and Where to Optimize – Prefer architecture and design optimization over code optimization § Biggest bang for the buck – Perform architecture and design optimization early and often § Prototype risky pieces – Perform code optimization late and rarely § Code optimization effort rarely justifies cost 02/09/2005 Better Java with JDeveloper 10 g 71 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Optimization and Code Improvement Best Practices • Design code for modularity, maintainability, and functionality first – Only optimize and tune code if necessary § Pareto Principle (“ 80/20 rule”) o Eighty percent of problems in twenty percent of code o Numbers not exact, but real meaning is that a few problems often cause most of the negative consequences – Tuning code for one area of improvement may negatively impact other areas of code (new bugs) – Do not allow code optimization to trump good design and maintainability unless justified by business case 02/09/2005 Better Java with JDeveloper 10 g 72 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Optimization and Code Improvement Best Practices • Experienced Developer Involved in Process – JDeveloper tools are very useful for identifying potential issues, but use expert review § No automatic tool is foolproof § Analysis only as good as configuration of tools and executable tests § Not all recommended results may apply to your situation o Results can be adjusted through various IDE and command-line options and via in-code pragmas o Need to discern which results are valid for organization and which do not apply or are not really an issue 02/09/2005 Better Java with JDeveloper 10 g 73 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Optimization and Code Improvement Best Practices • Review Your Code, Only Your Code, and Nothing But Your Code – With JDeveloper 10 g dynamic tools, accuracy of results depends on thoroughness of code coverage § Exercise all methods/threads of your own code in Profilers and Code. Coach tests – Do not analyze third-party code with tools § Typically cannot do anything about this code anyway § Adds significant delay to analysis execution time § Distracts reviewers from own code issues § Example: don’t analyze java, javax, and similar packages 02/09/2005 Better Java with JDeveloper 10 g 74 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Optimization and Code Improvement Best Practices • Train/Educate with JDeveloper Tools – Ultimately want personnel to learn and follow software development best practices – Developers can examine and discuss JDeveloper results and why they do or do not apply – Java “Experts” are developed by learning common issues with Java code – JDeveloper “Experts” are developed by learning how to use and customize the JDeveloper tools – Read “Explanations” and other JDeveloper documentation on rules and metrics to better understand why certain practices should be followed 02/09/2005 Better Java with JDeveloper 10 g 75 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Optimization and Code Improvement Best Practices • Use JDeveloper 10 g Tools Together – JDeveloper tools best used together because of differing areas of focus • Favor non-invasive analysis over invasive analysis techniques – For example, in-code changes are more invasive § Comments (Code. Coach Pragmas) are less troubling than executable source code statements § In-code instrumentation (Profilers) is designed to be “transparent” outside OJVM in Profile Mode – Aspect-Oriented Programming (AOP) concepts? 02/09/2005 Better Java with JDeveloper 10 g 76 Training Days 2005
JDeveloper 10 g Profilers Code. Coach Code Audit Code Metrics Better Java Best Practices Conclusion Summary • JDeveloper 10 g enables better Java code and better Java applications – – Identifies memory problems/issues Identifies execution/event performance/issues Identifies areas for code improvement Identifies non-compliance with industry and organizational conventions and standards – Identifies code that may be overly complex – Provides teaching and training opportunities for developers – Recognize that not all recommendations should be followed § Expert judgment should be used in applying recommendations 02/09/2005 Better Java with JDeveloper 10 g 77 Training Days 2005
Related RMOUG Training Days 2005 Presentations • Already completed: – “The Leap From PL/SQL to Java: More Than Just Syntax” – Session 1 – “The Object-Oriented SDLC and JDeveloper” – Session 1 – “Visual and Declarative J 2 EE Development with Oracle JDeveloper 10 g and Oracle ADF” – Session 4 02/09/2005 Better Java with JDeveloper 10 g 78 Training Days 2005
Related RMOUG Training Days 2005 Presentations • Still to Come: – “Avoiding Land Mines As You ‘Struts Your Stuff’ Through J 2 EE Development Using JDeveloper” – Session 7 – “Introduction to Java – PL/SQL Developers Take Heart!” – Session 8 – “Applied Testing Architectures to Prove Correctness of Enterprise J 2 EE Applications” – Session 9 – “Oracle JDeveloper for Database Developers and DBAs” – Session 10 – “Quick Web Development Using JDeveloper 10 g” – Session 12 – “I Love the Java Jive: J 2 EE Overview for Oracle Technologists” – Session 12 02/09/2005 Better Java with JDeveloper 10 g 79 Training Days 2005
Trademarks and Disclaimer • While significant effort was invested in an attempt to ensure accuracy, correctness, and completeness of this presentation, no guarantees of accuracy, correctness, or completeness are made by any individual or organization • Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. • Oracle and Oracle-based marks (including JDeveloper) are registered trademarks of Oracle Corporation and/or its affiliates. • Dustin Marx is independent of both Sun Microsystems, Inc. and Oracle Corporation. 02/09/2005 Better Java with JDeveloper 10 g 80 Training Days 2005
Better Java with JDeveloper 10 g Training Days 2005 Backup Slides 02/09/2005 Better Java with JDeveloper 10 g
JDeveloper 10 g Release 2 Tools Code. Coach • More efficient and maintainable code. Memory Profiler Execution Profiler Event Profiler Identify potential memory leaks in code Identify performance problems in application Identify problems or potential problems associated with events. Code Audit Rules (formerly Code Audit) Metrics (formerly Code Metrics) Identifies potential coding standard violations including: naming and coding conventions, unused constructs, source code errors, Javadoc problems and omissions, appropriate API usage, and more. Summarizes complexity and size of application including: number of statements (NOS), Depth of Inheritance Tree (DIT), and Cyclomatic/Branching Complexity [V(G)]. Code Assist 02/09/2005 Better Java with JDeveloper 10 g 82 Training Days 2005
Notes Transforming Code Audit and Code Metrics XML Output • Code/Code Metrics results are in XML format – Select XSLT style sheet in IDE to transform to HTML, text, or other format – Specify XSLT style sheet with –style option on command line for transformed formats • JDeveloper 10 g provides default HTML and text style sheets for both Code Metrics and Code Audit – – – audit-html. xsl, audit-text. xsl metrics-html. xsl, metrics-text. xsl Useful for transforming command-line results directly Useful in IDE as default style sheet selections Useful as starting point for adapting own custom style sheets 02/09/2005 Better Java with JDeveloper 10 g 83 Training Days 2005
jvmstat • Suite of tools compatible with Java 1. 4. 2, but with more complete support for Java 5 (AKA 1. 5) • Subset of jvmstat tools bundled with Java 5 – jps (Java Virtual Machine Process Status Tool) – jstat (Java Virtual Machine Statistics Monitoring Tool) – jstatd (Java Virtual Machine jstat Daemon) • visualgc available, but not bundled with Java 5 – Visual Garbage Collection Monitoring Tool 02/09/2005 Better Java with JDeveloper 10 g 84 Training Days 2005
-Xlink javac Option • Non-standard javac option • Reports legal but potentially problematic syntax – – – – -Xlink (all recommended warnings) -Xlink: none (display only mandatory warnings) -Xlink: unchecked (detailed unchecked conversion warnings) -Xlink: path (non-existent path directories) -Xlink: finally (problem with finally clause) -Xlink: fallthrough (switch-case without break) -Xlink: serial (missing serial. Version. UID) § http: //www. javapractices. com/Topic 45. cjp 02/09/2005 Better Java with JDeveloper 10 g 85 Training Days 2005
Java Virtual Machine Tool Interface (JVMTI) • Programming interface native to JVM intended for use by tools such as JDeveloper IDE and tools – – – Profiling Debugging Monitoring Coverage http: //java. sun. com/j 2 se/1. 5. 0/docs/guide/jvmti/ • Replaces JVMPI and JVMDI – Java Virtual Machine Profiler Interface (JVMPI) is now deprecated § http: //java. sun. com/j 2 se/1. 4. 2/docs/guide/jvmpi. html – Java Virtual Machine Debug Interface (JVMDI) is now deprecated § http: //java. sun. com/j 2 se/1. 4. 2/docs/guide/jpda/jvmdi-spec. html 02/09/2005 Better Java with JDeveloper 10 g 86 Training Days 2005
Java Object Toolkit (JOT) • Similar to Java’s Reflection API – But does not require loading of classes into JVM • Interfaces and classes used to parse and generate other Java code • Used in oracle. jdeveloper. audit package for customizing and extending Code Audit and Code Metrics • Available with Oracle JDeveloper (and Borland JBuilder) 02/09/2005 Better Java with JDeveloper 10 g 87 Training Days 2005
Better Java with JDeveloper 10 g Training Days 2005 Resources and References 02/09/2005 Better Java with JDeveloper 10 g
Other Papers/Presentations by the Author • “An Introduction to Aspect-Oriented Programming (AOP)” – RMOUG TD 2005 – Session 11! • “To Oracle XML and Beyond: PDF and Pictures” – RMOUG TD 2004 • “Beyond the Buzzwords: Walking the Talk, III” – RMOUG TD 2003 • “JSP Best Practices” and “More JSP Best Practices” – http: //www. javaworld. com/javaworld/jw-11 -2001/jw-1130 jsp. html – http: //www. javaworld. com/javaworld/jw-07 -2003/jw-0725 morejsp. html 02/09/2005 Better Java with JDeveloper 10 g 89 Training Days 2005
References – Oracle JDeveloper • Oracle JDeveloper – http: //www. oracle. com/technology/products/jdev/index. html • Oracle JDeveloper 10 g Overview – http: //www. oracle. com/technology/products/jdev/events/owsf 2004 /jdevoverview_oow 2004. pdf • Oracle JDeveloper Tips and Tricks – http: //www. oracle. com/technology/products/jdev/tips/index. html • Oracle Technology Forum – JDeveloper – http: //forums. oracle. com/forums/forum. jsp? forum=83 02/09/2005 Better Java with JDeveloper 10 g 90 Training Days 2005
References – Oracle JDeveloper Tools • Oracle 9 i JDeveloper – Performance Tuning J 2 EE Applications – http: //www. oracle. com/technology/products/ids/daily/jun 25. html • How to Build Your Own Audit Rules – http: //www. oracle. com/technology/products/jdev/howtos/10 g/Audi t. Tutorial/index. html • Code Auditing as Part of the Build Process – http: //www. oracle. com/technology/products/jdev/tips/lediouris/aud it/index. html • Oracle JDeveloper Extension SDK – http: //www. oracle. com/technology/products/jdev/htdocs/partners/a ddins/index. html 02/09/2005 Better Java with JDeveloper 10 g 91 Training Days 2005
References – Java Best Practices • Java Practices – http: //www. javapractices. com/index. cjp • JSP Best Practices – http: //www. javaworld. com/javaworld/jw-11 -2001/jw-1130 jsp. html – http: //www. javaworld. com/javaworld/jw-07 -2003/jw-0725 morejsp. html • Effective Java – http: //java. sun. com/developer/Books/effectivejava/ – http: //www. javaworld. com/javaworld/jw-06 -2001/j 1 -01 sintes 2. html • Effective Java Programming with Tiger – http: //www. developer. com/java/other/article. php/3112301 02/09/2005 Better Java with JDeveloper 10 g 92 Training Days 2005
References – JVM Monitoring Tools and Interfaces • jvmstat – http: //java. sun. com/performance/jvmstat/ • Java Virtual Machine Tool Interface (JVMTI) – http: //java. sun. com/j 2 se/1. 5. 0/docs/guide/jvmti/ • Java Virtual Machine Profiler Interface (JVMPI) – http: //java. sun. com/j 2 se/1. 4. 2/docs/guide/jvmpi. html • Java Virtual Machine Debug Interface (JVMDI) – http: //java. sun. com/j 2 se/1. 4. 2/docs/guide/jpda/jvmdi-spec. html 02/09/2005 Better Java with JDeveloper 10 g 93 Training Days 2005
References – Java Tuning • Java Performance Tuning – http: //www. javaperformancetuning. com/tips/index. shtml • Java Performance – http: //java. sun. com/performance/ • Java Performance Documentation – http: //java. sun. com/docs/performance/ • Java Platform Performance: Strategies and Tips – http: //java. sun. com/docs/books/performance/ 02/09/2005 Better Java with JDeveloper 10 g 94 Training Days 2005
- Slides: 94