Better Java with JDeveloper 10g - PowerPoint PPT Presentation

About This Presentation
Title:

Better Java with JDeveloper 10g

Description:

Better Java with JDeveloper 10g Presented by Dustin Marx Symbols Used in These Presentation Slides Two Versions of the Same Story JDeveloper 10g is label for ... – PowerPoint PPT presentation

Number of Views:77
Avg rating:3.0/5.0
Slides: 95
Provided by: Dusti88
Category:

less

Transcript and Presenter's Notes

Title: Better Java with JDeveloper 10g


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

2
Symbols Used in These Presentation Slides
Changes since slides were printed
Because the slides are submitted for printing
over one week in advance and in attempt to reduce
paper used to print out the handouts, some
changes and additions were made to the slides
between printing and the presentation. Some
slides were taken out for printing and added back
in for this presentation. Latest version will be
made available at RMOUG site and at
http//www.geocities.com/dustinmarx/.
3
Two Versions of the Same Story
  • JDeveloper 10g is label for several versions of
    the JDeveloper IDE
  • Some versions start with 9
  • g is for Grid
  • This presentation primarily presumes JDeveloper
    10g as JDeveloper 9.0.5.x
  • Some related changes and enhancements of
    JDeveloper 10g Release 2 (10.1.x) are mentioned
    in this presentation
  • The associated paper exclusively treats
    JDeveloper 10g as JDeveloper 9.0.5.x

4
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
5
Better Java with JDeveloper 10g Agenda
  • JDeveloper 10g Analysis Tools Overview
  • JDeveloper 10g Profilers
  • Memory Profiler
  • Execution Profiler
  • Event Profiler
  • JDeveloper 10g CodeCoach
  • JDeveloper 10g Code Audit
  • JDeveloper 10g Code Metrics
  • Better Java with the JDeveloper 10g Tools
  • Optimization and Code Improvement Best Practices
  • Conclusion

6
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

7
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)

8
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

9
Graphical Survey of JDeveloper 10g Tools
Increased Maintainability
Increased Performance
10
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

11
JDeveloper 10g Tools IDE and Command-line
JDeveloper 10g 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
CodeCoach Run?CodeCoach ojc -g and then java ojvm Xcodecoach
Code Audit Run?Audit ojaudit.exe
Code Metrics Run?Measure ojmetrics.exe
12
Command-line Oracle Java Compiler (OJC) Example
ojc -sourcepath ltltjdev_homegt\jdev\mywork\BetterJav
a\Project\src -classpath CLASSPATHltltjdev_homegtgt
\jdev\mywork\BetterJava\Project\classes -d
ltltjdev_homegtgt\jdev\mywork\BetterJava\Project\class
es -verbose -noquiet -g ltltjdev_homegtgt\jdev\mywork\
BetterJava\Project\src\org\rmoug\marx\betterjava\
.java
An even easier way to perform the above is to
place all the options in a text file known as a
Response File (see later slide)
13
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 _at_cc_args.txt
  • Compare to verbosity on previous slide

14
OJC Response File Example
ocj_args.txt
-sourcepath ltltjdev_homegtgt\jdev\mywork\BetterJava\P
roject\src -classpath CLASSPATHltltjdev_homegtgt\jd
ev\mywork\BetterJava\Project\classes -d
ltltjdev_homegtgt\jdev\mywork\BetterJava\Project\class
es -verbose -noquiet -g ltltjdev_homegtgt\jdev\BetterJ
ava\Project\src\org\rmoug\marx\betterjava\.java
To use the above file, one would run the
following command ojc _at_ojc_args.txt
15
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

16
Compiling with OJC and ANT An Example
lt!--Configure Oracle Java Compiler--gt ltproperty
name"build.compiler value"oracle.jdeveloper
.compiler.ant.taskdefs.OjcAdapter"/gt ltproperty
name"ojc.assertions.enabled" value"true"/gt
ltproperty name"ojc.update.imports"
value"none"/gt lt!--Compile Java source
files--gt ltjavac destdir"compile.outdir"
debug"on" encoding"Cp1252"gt ltclasspath
refid"classpath"/gt ltsrc refid"srcpath"/gt
ltinclude name"org/rmoug/marx/betterjava/TestBadEx
ample.java"/gt ltinclude name"org/rmoug/marx/bett
erjava/SimpleLogger.java"/gt ltinclude
name"org/rmoug/marx/betterjava/SimpleDataClass.ja
va"/gt ltinclude name"org/rmoug/marx/betterjava/P
rofilerExample.java"/gt ltinclude
name"org/rmoug/marx/betterjava/ParentItem.java"/gt
ltinclude name"org/rmoug/marx/betterjava/ChildI
tem.java"/gt ltinclude name"org/rmoug/marx/better
java/BadExampleParent.java"/gt ltinclude
name"org/rmoug/marx/betterjava/BadExample.java"/gt
lt/javacgt
17
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 IDEs
    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)

18
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

19
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 JDevelopers Debugger to identify
    memory leaks!
  • Heap Window is especially useful

20
Example of Memory Profiler Output
Notes
21
Another Example of Memory Profiler Output
22
Still Another Example of Memory Profiler Output
Notes
23
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()

24
Memory Profiler Configuration Window
25
Execution Profiler
  • Perform statistical analysis of application
    performance
  • Identify methods that top the call stack

26
Example of Execution Profiler Output
27
Execution Profiler Customization
  • Sample Interval
  • More samples means better statistical results
  • Columns displayed in Execution Profiler output
  • ProfilerAPI

28
Execution Profiler Configuration Window
29
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

30
Example of Event Profiler
31
Event Profiler Customization
  • Types/Groups of events to be monitored
  • Including custom Discovered Events
  • Columns displayed in Event Profiler output
  • ProfilerAPI

32
Event Profiler Customization Window
Notes
33
Event Profiler Events Monitoring
  • Real power is ability to create and monitor own
    events
  • Suns HotSpot Java 5.0 (AKA 1.5) JVM provides
    jvmstat and other JVM monitoring tools
  • See Backup slide for additional details

34
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

35
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

36
CodeCoach Results (IDE Example)
37
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

38
CodeCoach Results Options (IDE Example)
39
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

40
CodeCoach Customization
  • Customize CodeCoach Advice Types, Covered
    Packages, and Level of Advice
  • Project-level settings
  • Via command-line or IDE
  • Customize which advice types to identify
  • Specific advice types individually or via setting
    of level
  • Customize which classes/packages to analyze
  • For classes, all analyzed same or not at all
  • File, Class, or Method Level settings
  • Via in-code (comments) pragmas for more granular
    scope
  • Can more specifically tailor each classs
    CodeCoach analysis

41
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

42
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

43
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

44
CodeCoach Customization Window
Notes
45
CodeCoach Customization Command-line
  • Instruct CodeCoach via command-line using Xcc
    flags and options
  • -Xcclevelltltygtgt where y is integer 0 through 9
  • Corresponds to IDEs Level bar (1 through 10)
  • -Xccenableltlttypegtgt and -Xccdisableltlttypegtgt
  • 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
  • -Xccinclltltclassesgtgt and -Xccexclltltclassesgtgt
  • where classes is semicolon-separated list of
    classes and/or packages to analyze (incl) or
    ignore (excl) during CodeCoach analysis
  • Dont 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

46
CodeCoach Command-line Customization
ExampleRunning CodeCoach with OJVM
java ojvm Xcodecoach Xcclevel9
XccenableALL Xccinclorg.rmoug.marx.bet
terjava -Xccexclcomjavajavaxsunsunwora
cle classpath CLASSPATH
MyBadClassTest
The above assumes that MyBadClassTest was built
with Oracle Java Compiler and the g debugger
option.
47
For More on CodeCoach
  • See my article on CodeCoach in the next edition
    of the Rocky Mountain Oracle Users 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

48
Code Audit
  • Static tool analyzes static code
  • No OJC compiling or OJVM execution required
  • Can be run on code that doesnt 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

49
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

50
Code Audit Results/Output Options (IDE Example)
51
Code Audit Priorities / Severities
Notes
IDE (Priority) HTML (Severity)
Low Convention
Low 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.
52
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

53
Code Audit Customization Example
Notes
54
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 IDEs HTML Export
  • Use ojaudit (without parameters) or ojaudit help
    to display usage
  • Use fix to have Code Audit address violations

55
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 gt 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) lt 10
  • Rough rule of thumb

56
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

57
Code Metrics Output Example (IDE)
58
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

59
Code Metrics Customization Example
60
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 IDEs HTML Export
  • Use ojmetrics (without parameters) or ojmetrics
    help to display usage

61
New Features / Changes for JDeveloper 10g 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

62
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

63
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 CtrlAltEnter 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

64
A Code Assist Example IDE
Notes
65
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

66
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

67
JDeveloper 10g Tools Improving Java Code
  • 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

Memory Profiler Identify potential memory leaks
in code
Execution Profiler Identify performance problems
in application
  • Event
  • Profiler
  • Identify problems or potential problems
    associated with particular events in the
    application.
  • Garbage Collection Events
  • BC4J Events
  • Customized Events

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.
Code Metrics Summarizes complexity and size of
application including number of statements
(NOS), Depth of Inheritance Tree (DIT), and
Cyclomatic/Branching Complexity V(G).
68
Example Better Java Code with CodeCoach
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.
69
Example Better Java Code with Code Audit
String abusedString new String() for ( int
i0 ilt10000 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.
70
Example Better Java Code with Code Metrics
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.
71
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

72
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)
  • Eighty percent of problems in twenty percent of
    code
  • 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

73
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
  • Results can be adjusted through various IDE and
    command-line options and via in-code pragmas
  • Need to discern which results are valid for
    organization and which do not apply or are not
    really an issue

74
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 dont analyze java, javax, and similar
    packages

75
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

76
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?

77
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

78
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

79
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

80
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.

81
Better Java with JDeveloper 10g
  • Backup Slides

82
JDeveloper 10g Release 2 Tools
  • CodeCoach
  • More efficient and maintainable code.

Memory Profiler Identify potential memory leaks
in code
Execution Profiler Identify performance problems
in application
Event Profiler Identify problems or potential
problems associated with events.
Code Audit
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
83
Transforming Code Audit and Code Metrics XML
Output
Notes
  • 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

84
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

85
-Xlink javac Option
  • Non-standard javac option
  • Reports legal but potentially problematic syntax
  • -Xlink (all recommended warnings)
  • -Xlinknone (display only mandatory warnings)
  • -Xlinkunchecked (detailed unchecked conversion
    warnings)
  • -Xlinkpath (non-existent path directories)
  • -Xlinkfinally (problem with finally clause)
  • -Xlinkfallthrough (switch-case without break)
  • -Xlinkserial (missing serialVersionUID)
  • http//www.javapractices.com/Topic45.cjp

86
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/jv
    mpi.html
  • Java Virtual Machine Debug Interface (JVMDI) is
    now deprecated
  • http//java.sun.com/j2se/1.4.2/docs/guide/jpda/jvm
    di-spec.html

87
Java Object Toolkit (JOT)
  • Similar to Javas 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)

88
Better Java with JDeveloper 10g
  • Resources and References

89
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-1
    130-jsp.html
  • http//www.javaworld.com/javaworld/jw-07-2003/jw-0
    725-morejsp.html

90
References Oracle JDeveloper
  • Oracle JDeveloper
  • http//www.oracle.com/technology/products/jdev/ind
    ex.html
  • Oracle JDeveloper 10g Overview
  • http//www.oracle.com/technology/products/jdev/eve
    nts/owsf2004/jdevoverview_oow2004.pdf
  • Oracle JDeveloper Tips and Tricks
  • http//www.oracle.com/technology/products/jdev/tip
    s/index.html
  • Oracle Technology Forum JDeveloper
  • http//forums.oracle.com/forums/forum.jsp?forum83

91
References Oracle JDeveloper Tools
  • Oracle 9i JDeveloper Performance Tuning J2EE
    Applications
  • http//www.oracle.com/technology/products/ids/dail
    y/jun25.html
  • How to Build Your Own Audit Rules
  • http//www.oracle.com/technology/products/jdev/how
    tos/10g/AuditTutorial/index.html
  • Code Auditing as Part of the Build Process
  • http//www.oracle.com/technology/products/jdev/tip
    s/lediouris/audit/index.html
  • Oracle JDeveloper Extension SDK
  • http//www.oracle.com/technology/products/jdev/htd
    ocs/partners/addins/index.html

92
References Java Best Practices
  • Java Practices
  • http//www.javapractices.com/index.cjp
  • JSP Best Practices
  • http//www.javaworld.com/javaworld/jw-11-2001/jw-1
    130-jsp.html
  • http//www.javaworld.com/javaworld/jw-07-2003/jw-0
    725-morejsp.html
  • Effective Java
  • http//java.sun.com/developer/Books/effectivejava/
  • http//www.javaworld.com/javaworld/jw-06-2001/j1-0
    1-sintes2.html
  • Effective Java Programming with Tiger
  • http//www.developer.com/java/other/article.php/31
    12301

93
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/jv
    mpi.html
  • Java Virtual Machine Debug Interface (JVMDI)
  • http//java.sun.com/j2se/1.4.2/docs/guide/jpda/jvm
    di-spec.html

94
References Java Tuning
  • Java Performance Tuning
  • http//www.javaperformancetuning.com/tips/index.sh
    tml
  • 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/
Write a Comment
User Comments (0)
About PowerShow.com