Upload
dinhtu
View
214
Download
0
Embed Size (px)
Citation preview
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Enhanced Depreca@on
Stuart Marks aka Dr Deprecator JDK Core Libraries Java PlaJorm Group, Oracle
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
Enhanced Depreca@on • Concepts & History • Depreca@on work in JDK 9 • No@fica@on & warnings – compile-‐@me warnings – sta@c analysis tool – jdeprscan • Work in progress • Future work
4
Twi[er hashtag for ques@ons and comments
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
Systems Development Life Cycle • Depreca@on is concerned with the final phase: Disposi@on
5
source: wikipedia h[ps://en.wikipedia.org/wiki/Systems_development_life_cycle
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
API Life Cycle • Different from socware product life cycle – nearing end, updates (bugfixes) ramped down – security vulnerabili@es only – end of support life • API life cycle – addi@on, depreca@on, and removal of feature rela@ve to a specifica,on – example: API X deprecated in Java SE 9, removed from Java SE 10 – dis@nct from Oracle JDK 9 reaching end of public update, end of support life
6
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
Depreca@on Key Concepts • Depreca@on is – no@fica@on to developers that they should migrate their code away from the deprecated API
• Possible reasons – the deprecated API has something wrong with it – there’s a newer, be[er API that can be used instead – the deprecated API is going to be removed
7
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
History (1) • Depreca@on introduced in JDK 1.1 as a javadoc tag @deprecated • Early on, massive wave of depreca@ons for widely varying reasons – dangerous APIs (Thread.destroy) – simple renames (AWT Component.show/hide => setVisible) – caused disrup@on, compiler warnings, fear, confusion – result: depreca@ons slowed dras@cally, no APIs removed
• Annota@on @Deprecated introduced in Java 5 – note capitaliza@on – no change/clarifica@on of seman@cs
8
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
History (2) • Has anything ever been un-‐deprecated? – Yes! The System.getenv() API was in JDK 1.0 – first sentence of its javadoc: “Obsolete.” – implementa@on: threw an excep@on uncondi@onally – officially deprecated in 1.1 (when depreca@on was created) – fully implemented and un-‐deprecated in Java SE 5
• Has anything been newly introduced as deprecated? – Yes! Three overloads of javax.management.MBeanServer.deserialize() – introduced in Java SE 5, deprecated in Java SE 5! – JMX was a standalone JSR, with deprecated stuff, before integra@on into SE 5
9
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
History (3) • Confusion con@nued through Java 8 – “Don’t use deprecated APIs, since they might be removed” – “Sun/Oracle have never removed anything, and they never will”
• Wrong! We will actually remove stuff in Java 9 – [email protected]/removePropertyChangeListener – [email protected]/Unpacker.add/removePropertyChangeListener – main driver was modularity, to break dependencies on Java Beans, part of the java.desktop module – also: java.awt.Component.getPeer() removed • flawed API, referred to types outside of Java SE, e.g., ComponentPeer
10
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
Depreca@on Work in Java 9 • Major clarifica@on of seman@cs • Enhancements to @Deprecated annota@on – since=<string> – forRemoval=<boolean> – example:
@Deprecated(since="9", forRemoval=true)
• Sta@c analysis tool: jdeprscan • Minor adjustments to JLS, javac, javadoc
11
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
New Depreca@on Elements • forRemoval=<boolean> – forRemoval=true • depreca@on is a warning that the API is subject to removal in a future spec release • called a “terminal depreca@on” • for Java SE, intent is that removal will occur in the next release
– forRemoval=false • merely a recommenda@on to migrate away from this API; no current intent to remove • called an “ordinary depreca@on” • pre-‐exis@ng depreca@ons are equivalent to forRemoval=false (the default)
• since=<string> – the version when the API was deprecated; mostly informa@onal
12
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
What Happened to CONDEMNED? • Previous proposals had a reason code (enum) – SUPERSEDED, OBSOLETE, DANGEROUS, CONDEMNED, etc. – reason is a useful concept, in prac@ce hard to assign for any par@cular depreca@on – CONDEMNED is now forRemoval=true – "condemned" is easily misinterpreted, tended to generate unproduc@ve discussions
13
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
Java 9 Depreca@ons So Far: forRemoval=false • Boxed primi@ve constructors: new Integer(), new Boolean(), etc. – use Integer.valueOf(), Boolean.valueOf(), etc. • java.applet – applets and browser plugins are (slowly) on their way out • [email protected]/Observable – anybody ever use these?
14
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
Java 9 Depreca@ons So Far: forRemoval=true • Thread.destroy() • Thread.stop(Throwable) – no-‐arg Thread.stop() s@ll deprecated, but not for removal
• System.runFinalizersOnExit(boolean) – JDK-‐4240589: if called with true, can cause unavoidable VM crash – Filed in 1999! The only fix is to remove it.
• Obsolete SecurityManager calls – inCheck, getInCheck(), currentClassLoader(), currentLoadedClass(), classDepth(), classLoaderDepth(), inClass(), inClassLoader(), checkTopLevelWindow(), ... – all ves@ges of the old security model, superseded in Java 1.2
15
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
No@fica@ons and Warnings • Purpose of no@fica@ons is to facilitate code migra@on • When an API is deprecated, developers need no@fica@on – code using obsolete API might have problems – the API might eventually go away, breaking code that uses it
• Goal: get depreca@on/removal informa@on to developers – avoid unpleasant surprises at compile @me – avoid unpleasant surprises at run @me
16
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
Kinds of No@fica@on • Compile @me – mostly compiler warnings – various severity – warnings management and suppression
• Sta@c analysis – useful for binaries that aren’t recompiled regularly – especially 3rd party dependent libraries • Dynamic analysis – detect usage at run@me – (not part of Java 9)
17
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
18
$ javac src/example/Deprecations.java Note: src/example/Deprecations.java uses or overrides a deprecated API. Note: Recompile with -‐Xlint:deprecation for details. $ javac -‐Xlint:deprecation src/example/Deprecations.java src/example/Deprecations.java:12: warning: [deprecation] getSelectedValues() in JList has been deprecated Object[] values = jlist.getSelectedValues(); ^ 1 warning
Compile-‐Time Warnings
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
Compile-‐Time Warnings: Problem • Compile-‐@me warnings only help if you recompile – in many cases, people don’t recompile with each release – we support binary compa@bility, acer all
• Problem is worse with third party libraries – consider binary ar@facts distributed widely, e.g., from maven central – for maximum compa@bility, ocen compiled for earliest JDK release supported – many people reuse such binaries on later JDK releases
• Your dependent libraries might use deprecated APIs – how can you tell?
19
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
Compile-‐Time Warnings: Problem Consider scenario: MyApp uses class SomeOldJdkClass, reuse binary
1. Use of SomeOldJdkClass introduced è MyApp compiles fine, without warnings
2. Next Java release: SomeOldJdkClass deprecated è MyApp con@nues to run, without warnings
3. Next Java release: SomeOldJdkClass deprecated for removal è MyApp con@nues to run, without warnings
4. Next Java release: SomeOldJdkClass is removed è MyApp suddenly fails at run@me with NoClassDefFoundError!
20
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
Solu@on: Sta@c Analysis (jdeprscan) • New tool introduced in Java 9 • Sta@cally analyzes class files and jar files against Java SE APIs • Looks for and reports usage of deprecated Java SE APIs
21
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
22
public class Deprecations { void method(JList jlist) { Object[] values = jlist.getSelectedValues(); // deprecated in 7 SecurityManager sm = new RMISecurityManager(); // deprecated in 8 Boolean b2 = new Boolean(true); // deprecated in 9 } }
Solu@on: Sta@c Analysis (jdeprscan)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
23
$ javac -‐version javac 1.6.0_65 $ javac -‐d classes src/example/Deprecations.java (no warnings) $ jdeprscan -‐-‐full-‐version 9-‐ea+134 $ jdeprscan -‐-‐class-‐path classes example.Deprecations class example/Deprecations uses type java/rmi/RMISecurityManager deprecated class example/Deprecations uses method javax/swing/JList getSelectedValues ()[Ljava/lang/Object; deprecated class example/Deprecations uses method in type java/rmi/RMISecurityManager deprecated class example/Deprecations uses method java/lang/Boolean <init> (Z)V deprecated
Solu@on: Sta@c Analysis (jdeprscan)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
24
$ jdeprscan -‐-‐class-‐path classes -‐-‐release 9 example.Deprecations class example/Deprecations uses type java/rmi/RMISecurityManager deprecated class example/Deprecations uses method javax/swing/JList getSelectedValues ()[Ljava/lang/Object; deprecated class example/Deprecations uses method in type java/rmi/RMISecurityManager deprecated class example/Deprecations uses method java/lang/Boolean <init> (Z)V deprecated $ jdeprscan -‐-‐class-‐path classes -‐-‐release 8 example.Deprecations class example/Deprecations uses type java/rmi/RMISecurityManager deprecated class example/Deprecations uses method javax/swing/JList getSelectedValues ()[Ljava/lang/Object; deprecated class example/Deprecations uses method in type java/rmi/RMISecurityManager deprecated
jdeprscan Analysis Can Be Version-‐specific
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
25
$ jdeprscan -‐-‐class-‐path classes -‐-‐release 8 example.Deprecations class example/Deprecations uses type java/rmi/RMISecurityManager deprecated class example/Deprecations uses method javax/swing/JList getSelectedValues ()[Ljava/lang/Object; deprecated class example/Deprecations uses method in type java/rmi/RMISecurityManager deprecated $ jdeprscan -‐-‐class-‐path classes -‐-‐release 7 example.Deprecations class example/Deprecations uses method javax/swing/JList getSelectedValues ()[Ljava/lang/Object; deprecated $ jdeprscan -‐-‐class-‐path classes -‐-‐release 6 example.Deprecations (no output)
jdeprscan Analysis Can Be Version-‐specific
API informa,on for past releases is built into Java 9. No need to keep old versions of the JDK around.
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
Terminal Depreca@on Warnings (forRemoval=true) • New state transi@on – from @Deprecated(forRemoval=false) to @Deprecated(forRemoval=true) – need an extra level of warning for this • Warnings can be suppressed with @SuppressWarnings("depreca@on") – currently suppresses both warnings, forRemoval false and true – leads to state transi@ons going undetected
26
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
Terminal Depreca@on Warnings (forRemoval=true) Consider scenario: MyApp uses class SomeOldJdkClass, compile each release 1. Use of SomeOldJdkClass introduced
è MyApp compiles fine, without warnings 2. Next Java release: SomeOldJdkClass deprecated
è MyApp compiles with warnings; @SuppressWarnings added 3. Next Java release: SomeOldJdkClass deprecated for removal
è MyApp compiles fine, without warnings; warning s@ll suppressed 4. Next Java release: SomeOldJdkClass is removed
è MyApp compila@on suddenly fails with error: cannot find symbol!
27
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
Terminal Depreca@on Warnings: Solu@on • Add new lint warning category “removal” dis@nct from “depreca@on” – @SuppressWarnings("depreca@on") • this suppresses only ordinary depreca@on warnings, not terminal depreca@on warnings
– @SuppressWarnings("removal") • this suppresses only terminal depreca@on warnings, not ordinary depreca@on warnings
– @SuppressWarnings({"depreca@on", "removal"}) • if for some reason you want to suppress both kinds
– javac op@on –Xlint:removal is enabled by default • terminal depreca@on warnings are shown without having to add op@ons • no change to –Xlint:depreca@on
28
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
Legacy @deprecated Javadoc Tag Behavior • Originally, the javadoc tag was the only way to deprecate something – now, the annota@on is the preferred way – preferably, both should be present – things s@ll work if the javadoc tag is present without the annota@on
/** * My method. * @deprecated This method is obsolete. Use a newer method instead. */ @Deprecated void doSomething() { ... }
29
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
Legacy @deprecated Javadoc Tag Behavior • Annota@on vs. class file a[ribute – the @deprecated javadoc tag causes javac to emit a class file that contains the “Deprecated” class file a[ribute (see JVMS 4.7.15) – the @Deprecated annota@on is stored in the class file as an annota@on – they are dis@nct! – when reading a class file, javac honors both • Time to re@re the “Deprecated” class file a[ribute? – it probably should first be... deprecated... – JDK 9: a javadoc tag without the annota@on generates a warning by default – future: the “Deprecated” class file a[ribute will no longer be generated or honored
30
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
Future Depreca@on Work • Develop run@me analyzer – sta@c analysis currently misses reflec@ve access to deprecated APIs
• More APIs to be deprecated – Vector, Hashtable, Enumera@on (legacy collec@ons) – [email protected] (error-‐prone, encourages poor style) – Timer, TimerTask (superseded by ScheduledThreadPoolExecutor) – Date, Calendar (superseded by java.@me) – java.corba module?
31
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
Future Depreca@on Work • Be[er warnings management – problem with depreca@ng oc-‐used APIs (Vector, Hashtable, Date, Calendar) – doing this generates lots of warnings – li[ering a code base with @SuppressWarnings is untenable – modifying all old usages is untenable – need be[er way of suppressing warnings – ??? maybe suppress depreca@on warnings only for specific APIs ???
32
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
Conclusions • JEP 277 nearly completed – major seman@c updates and clarifica@ons – new forRemoval annota@on element (terminal depreca@on) – we’re (slowly) cleaning up the JDK! • Depreca@on life cycle requires improved no@fica@on – run the sta@c analysis tool jdeprscan – @SuppressWarnings("removal") for terminal depreca@ons
• Links – JEP 277: h[p://openjdk.java.net/jeps/277 – JDK 9 builds: h[ps://jdk9.java.net
33
Twi[er: @DrDeprecator @StuartMarks
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
#EnhancedDepreca@on
Safe Harbor Statement The preceding is intended to outline our general product direc@on. It is intended for informa@on purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or func@onality, and should not be relied upon in making purchasing decisions. The development, release, and @ming of any features or func@onality described for Oracle’s products remains at the sole discre@on of Oracle.
34