better java with jdeveloper 10g
Post on 12-Jan-2016
37 Views
Preview:
DESCRIPTION
TRANSCRIPT
Training Days 2005
Better Java with JDeveloper 10g
Presented byDustin Marx
02/09/2005 Better Java with JDeveloper 10g 2
Training Days 2005
Symbols Used in These Presentation Slides
New or significantly altered slide
Minor change or addition to slide
Changessince slides
were printed
Significant details in “Notes” section of slide(see electronic version to see these “Notes”)
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 10g 3
Training Days 2005
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
02/09/2005 Better Java with JDeveloper 10g 4
Training Days 2005
Recent JDeveloper Versions
Version Numbers Name
10.1.3 JDeveloper 10g Release 2*
9.0.5.1, 9.0.5.2, 10.1.2 JDeveloper 10g
9.0.4.1, 9.0.4.0 JDeveloper 9i
9.0.3.3, 9.0.3.2, 9.0.3.1, 9.0.3 JDeveloper 9i
9.0.2 JDeveloper 9i
*Developer Preview
02/09/2005 Better Java with JDeveloper 10g 5
Training Days 2005
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
02/09/2005 Better Java with JDeveloper 10g 6
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 7
Training Days 2005
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)
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 8
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 9
Training Days 2005
Graphical Survey of JDeveloper 10g Tools
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
Incr
ease
d M
aint
aina
bili
ty
Increased Performance
KEY
02/09/2005 Better Java with JDeveloper 10g 10
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 11
Training Days 2005
JDeveloper 10g Tools: IDE and Command-line
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
JDeveloper 10g
Analysis ToolIDE Command-line
Memory Profiler RunMemory Profile N/A
Execution Profiler RunExecution Profile N/A
Event Profiler RunEvent Profile N/A
CodeCoach RunCodeCoach
ojc -g and then
java –ojvm
–Xcodecoach
Code Audit RunAudit ojaudit.exe
Code Metrics RunMeasure ojmetrics.exe
02/09/2005 Better Java with JDeveloper 10g 12
Training Days 2005
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 optionsin a text file known as a Response File (see later slide)
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 13
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 14
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 15
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 16
Training Days 2005
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>
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 17
Training Days 2005
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)
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 18
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 19
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 20
Training Days 2005
Example of Memory Profiler Output
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
Notes
02/09/2005 Better Java with JDeveloper 10g 21
Training Days 2005
Another Example of Memory Profiler Output
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 22
Training Days 2005
Still Another Example of Memory Profiler Output
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
Notes
02/09/2005 Better Java with JDeveloper 10g 23
Training Days 2005
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()
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 24
Training Days 2005
Memory Profiler Configuration Window
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 25
Training Days 2005
Execution Profiler
• Perform statistical analysis of application performance
• Identify methods that top the call stack
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 26
Training Days 2005
Example of Execution Profiler Output
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 27
Training Days 2005
Execution Profiler Customization
• Sample Interval– More samples means better statistical results
• Columns displayed in Execution Profiler output• ProfilerAPI
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 28
Training Days 2005
Execution Profiler Configuration Window
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 29
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 30
Training Days 2005
Example of Event Profiler
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 31
Training Days 2005
Event Profiler Customization
• Types/Groups of events to be monitored– Including custom “Discovered Events”
• Columns displayed in Event Profiler output• ProfilerAPI
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 32
Training Days 2005
Event Profiler Customization Window
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
Notes
02/09/2005 Better Java with JDeveloper 10g 33
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 34
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 35
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 36
Training Days 2005
CodeCoach Results (IDE Example)
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 37
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 38
Training Days 2005
CodeCoach Results & Options (IDE Example)
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 39
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 40
Training Days 2005
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
o Specific advice types individually or via setting of level
Customize which classes/packages to analyzeo 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 CodeCoach analysis
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 41
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 42
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 43
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 44
Training Days 2005
CodeCoach Customization Window
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
Notes
02/09/2005 Better Java with JDeveloper 10g 45
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 46
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
The above assumes that MyBadClassTest was built with Oracle Java Compiler and the –g debugger option.
02/09/2005 Better Java with JDeveloper 10g 47
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 48
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 49
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 50
Training Days 2005
Code Audit Results/Output Options (IDE Example)
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 51
Training Days 2005
Code Audit Priorities / Severities
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
IDE (“Priority”) HTML (“Severity”)
“Low”“Convention”
“Suggestion”
“Medium” “Violation”
“High” “Warning”
“Critical” “Error”
The IDE associates a “priority” with Code Audit identified violations while the HTML output lists a “severity” for each identified violation.
Notes
02/09/2005 Better Java with JDeveloper 10g 52
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 53
Training Days 2005
Code Audit Customization Example
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
Notes
02/09/2005 Better Java with JDeveloper 10g 54
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 55
Training Days 2005
Code Metrics
• Static tool – analyzes static code– No OJC compiling or OJVM execution required
• JDeveloper 10g 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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 56
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 57
Training Days 2005
Code Metrics Output Example (IDE)
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 58
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 59
Training Days 2005
Code Metrics Customization Example
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 60
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 61
Training Days 2005
New Features / Changes for JDeveloper 10g Release 2
• 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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
Release 2
Notes
02/09/2005 Better Java with JDeveloper 10g 62
Training Days 2005
The Multi-Function Code Audit in Release 2
• Code Audit (R2) 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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
Release 2
02/09/2005 Better Java with JDeveloper 10g 63
Training Days 2005
A Quick Peek at Code Assist
• New Feature of JDeveloper 10g R2 (10.1.3)• Similar functionality to CodeCoach
– 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 ToolsPreferences
(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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
Code Assist
02/09/2005 Better Java with JDeveloper 10g 64
Training Days 2005
A Code Assist Example – IDE
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
Code Assist
Notes
02/09/2005 Better Java with JDeveloper 10g 65
Training Days 2005
Current Limitations of CodeAssist
• Spacing of accepted CodeAssist recommendations does not always match well with source code indentation
• CodeAssist 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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
Code Assist
02/09/2005 Better Java with JDeveloper 10g 66
Training Days 2005
Better Java Code with JDeveloper 10g 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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 67
Training Days 2005
JDeveloper 10g Tools Improving Java Code
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
CodeCoach•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
MemoryProfiler
Identify potential memory leaks in code
ExecutionProfiler
Identify performance problems in application
EventProfiler
Identify problems or potential problems associated with particular events in the application.•Garbage Collection Events•BC4J Events•Customized Events
Code AuditIdentifies potential coding standard violations including: naming and coding conventions, unused constructs, source code errors, Javadoc problems and omissions, appropriate API usage, and more.
Code MetricsSummarizes 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 10g 68
Training Days 2005
Example: Better Java Code with CodeCoach
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
ParentItem item = new ChildItem();if ( item instanceof ChildItem ){ name = item.getName();}
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 10g 69
Training Days 2005
Example: Better Java Code with Code Audit
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
String abusedString = new String();for ( int i=0; i<10000; ++i ){ abusedString = abusedString + '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 10g 70
Training Days 2005
Example: Better Java Code with Code Metrics
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
if (){ if () if () if () if () if ()}else if () {}else if () {}else if () {}else if () {}
Cyclomatic complexity here is shown with a red “11” because the recommended complexity (10) has been exceeded.
02/09/2005 Better Java with JDeveloper 10g 71
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 72
Training Days 2005
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 codeo 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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 73
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 74
Training Days 2005
Optimization and Code Improvement Best Practices
• Review Your Code, Only Your Code, and Nothing But Your Code– With JDeveloper 10g dynamic tools, accuracy of results depends
on thoroughness of code coverage Exercise all methods/threads of your own code in Profilers and
CodeCoach 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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 75
Training Days 2005
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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 76
Training Days 2005
Optimization and Code Improvement Best Practices
• Use JDeveloper 10g 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 (CodeCoach 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?
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 77
Training Days 2005
Summary
• JDeveloper 10g 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
JDeveloper10g Profilers CodeCoach Code Audit Code Metrics Better Java Best Practices Conclusion
02/09/2005 Better Java with JDeveloper 10g 78
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 J2EE Development with Oracle JDeveloper 10g and Oracle ADF” – Session 4
02/09/2005 Better Java with JDeveloper 10g 79
Training Days 2005
Related RMOUG Training Days 2005 Presentations
• Still to Come:– “Avoiding Land Mines As You ‘Struts Your Stuff’ Through J2EE
Development Using JDeveloper” – Session 7– “Introduction to Java – PL/SQL Developers Take Heart!” –
Session 8– “Applied Testing Architectures to Prove Correctness of Enterprise
J2EE Applications” – Session 9– “Oracle JDeveloper for Database Developers and DBAs” –
Session 10– “Quick Web Development Using JDeveloper 10g” – Session 12– “I Love the Java Jive: J2EE Overview for Oracle Technologists” –
Session 12
02/09/2005 Better Java with JDeveloper 10g 80
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.
Training Days 2005
Better Java with JDeveloper 10g
Backup Slides
02/09/2005 Better Java with JDeveloper 10g 82
Training Days 2005
Code Audit
JDeveloper 10g Release 2 Tools
CodeCoach•More efficient and maintainable code.
MemoryProfiler
Identify potential memory leaks in code
ExecutionProfiler
Identify performance problems in application
EventProfiler
Identify problems or potential problems associated with events.
Rules (formerly Code Audit)Identifies potential coding standard violations including: naming and coding conventions, unused constructs, source code errors, Javadoc problems and omissions, appropriate API usage, and more.
Metrics (formerly Code Metrics)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 10g 83
Training Days 2005
Transforming Code Audit and Code Metrics XML Output
• Code 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 10g 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
Notes
02/09/2005 Better Java with JDeveloper 10g 84
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 10g 85
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 serialVersionUID)
http://www.javapractices.com/Topic45.cjp
02/09/2005 Better Java with JDeveloper 10g 86
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/j2se/1.5.0/docs/guide/jvmti/
• Replaces JVMPI and JVMDI– Java Virtual Machine Profiler Interface (JVMPI) is now
deprecated http://java.sun.com/j2se/1.4.2/docs/guide/jvmpi/jvmpi.html
– Java Virtual Machine Debug Interface (JVMDI) is now deprecated http://java.sun.com/j2se/1.4.2/docs/guide/jpda/jvmdi-spec.html
02/09/2005 Better Java with JDeveloper 10g 87
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)
Training Days 2005
Better Java with JDeveloper 10g
Resources and References
02/09/2005 Better Java with JDeveloper 10g 89
Training Days 2005
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 10g 90
Training Days 2005
References – Oracle JDeveloper
• Oracle JDeveloper– http://www.oracle.com/technology/products/jdev/index.html
• Oracle JDeveloper 10g Overview– http://www.oracle.com/technology/products/jdev/events/
owsf2004/jdevoverview_oow2004.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 10g 91
Training Days 2005
References – Oracle JDeveloper Tools
• Oracle 9i JDeveloper – Performance Tuning J2EE Applications– http://www.oracle.com/technology/products/ids/daily/jun25.html
• How to Build Your Own Audit Rules– http://www.oracle.com/technology/products/jdev/howtos/10g/
AuditTutorial/index.html • Code Auditing as Part of the Build Process
– http://www.oracle.com/technology/products/jdev/tips/lediouris/audit/index.html
• Oracle JDeveloper Extension SDK– http://www.oracle.com/technology/products/jdev/htdocs/partners/
addins/index.html
02/09/2005 Better Java with JDeveloper 10g 92
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/j1-01-
sintes2.html• Effective Java Programming with Tiger
– http://www.developer.com/java/other/article.php/3112301
02/09/2005 Better Java with JDeveloper 10g 93
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/j2se/1.5.0/docs/guide/jvmti/
• Java Virtual Machine Profiler Interface (JVMPI)– http://java.sun.com/j2se/1.4.2/docs/guide/jvmpi/jvmpi.html
• Java Virtual Machine Debug Interface (JVMDI)– http://java.sun.com/j2se/1.4.2/docs/guide/jpda/jvmdi-spec.html
02/09/2005 Better Java with JDeveloper 10g 94
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/
top related