In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you the content of the sample analysis of upgrading from JBuilder2005 implementation refactoring to JDK5.0. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
I want to convert the jdk under Jbuilder X to 1.6. the result shows an error. The following writing method is not supported.
ArrayListlist=new ArrayList ()
After checking on the Internet, it is found that you need to fix it to Project- > Project Properties...- > Build- > on the Java settings page, set the Target VM to Java 2 SDK,v 5.0 and later.
But this option is not set in my Jbuilder X, and the upgrade of jdk under Jbuilder is not successful.
Now upgrade the JBuilder2005 implementation refactoring to JDK5.0--reprint here
JDK 5.0makes many significant syntax improvements, although the source code developed in earlier versions of JDK can be run directly in JDK 5.0without any modification. However, there are still refactorings that must be upgraded, so that improved features such as for and while loops, automatic loading / unpacking, generics, etc. of JDK 5.0can be applied to make the code more compact while optimizing code performance. So JBuilder provides three refactoring features specifically for low-version programs upgraded to JDK 5.0, making it easy for you to upgrade seamlessly.
1. Make the project based on JDK 5.0,
Although JBuilder 2005 supports JDK 5.0, the default JDK version is 1.4.2. to make the project's JDK version 5.0, you must install JDK 5.0and set it under JBuilder. We don't describe too much about the detailed setup of JDK 5.0. in short, it mainly includes the following steps:
1) install JDK 5.0 (download from http://java.sun.com/j2se/1.5.0/download.jsp).
2) Tools- > Configure- > JDKs... Generally specify the installation path of JDK5.0 to set JDK.
3) Project- > Project Properties...- > on the Paths settings page, set JDK to JDK 5.0.
4) Project- > Project Properties...- > Build- > on the Java settings page, set Language features: to Java 2 SDK,V 5.0 (generics enabled) and Target VM to Java 2 SDK,v 5.0 and later.
Code refactoring related to JDK 5.0 can only be carried out if the JDK version of the project is set to JDK 5.0.
2. Optimize the cycle
JDK 5.0introduces a more efficient loop, called a JDK 5.0style loop, which includes:
. Array traversal
.list traversal
For loop of .Iterator
While loop of .Iterator
JBuilder provides a refactoring method that transforms the loop code corresponding to the lower version of JDK, which is a JDK5.0 loop style. We illustrate this through a refactoring of array traversal. See the following low version JDK loop code:
Listing 11 low-version loop style
1. Public static void arrayLoopRefactoring ()
2. {
3. Int [] myArray = {1,2,3,4}
4. For (int x = 0; x
< myArray.length ; x++) { 5. System.out.println(myArray[x]) ; 6. } 7. } 将光标移到第4行的for(int x = 0 ; x < myArray.length ; x++) 中间:Ctrl+Shift+R->Introduce Foreach calls up the loop refactoring dialog:
Figure 22 Loop refactoring dialog box
Specify a variable name for the array loop temporary variable in Loop variable name. Here, we set it to item, press OK to complete the refactoring, and JBuilder generates JDK 5.0style loop code, as shown below:
Listing 12 JDK 5.0style loop
1. Public static void arrayLoopRefactoring ()
2. {
3. Int [] myArray = {1,2,3,4}
4. For (int item: myArray) {
5. System.out.println (item)
6.}
7.}
3. Automatic loading / unpacking
Each basic data type such as double and int has corresponding wrapper classes Double and Integer. In the lower version of JDK, some boring code is needed to complete the conversion from the basic data type to the corresponding wrapper class. The automatic packing / unpacking feature of JDK 5.0can automatically complete this conversion, making the code more concise. Take a look at the following code that does not use automatic packing / unboxing:
Listing 13 does not use automatic loading / unboxing code
1. Package myrefactor
2. Public class Jdk5
3. {
4....
5. Public static void autoBoxingPreliminary (Integer intObject)
6. {
7. System.out.println (intObject)
8.}
9.
10. Public static void autoBoxingRefactoring ()
11. {
12. AutoBoxingPreliminary (new Integer (8))
13.}
14....
15.}
The autoBoxingPreliminary () method defined in lines 5-8 requires an input parameter to Integer, and autoBoxingRefactoring () honestly satisfies this requirement in line 12. JBuilder provides a very convenient method for automatic refactoring / unboxing, open the classes that need to do this refactoring, anywhere in the editor: Ctrl+Shift+R- > Introduce Auto (un) boxing,JBuilder search where the current Class file needs to be automatically loaded / unboxed, and refactoring. For example, after the automatic loading / unboxing refactoring of the above code snippet, it will be called as follows:
Code listing 14 code after automatic loading / unboxing and refactoring
1. Package myrefactor
2. Public class Jdk5
3. {
4....
5. Public static void autoBoxingPreliminary (Integer intObject)
6. {
7. System.out.println (intObject)
8.}
9.
10. Public static void autoBoxingRefactoring ()
11. {
12. AutoBoxingPreliminary (8)
13.}
14....
15.}
Line 12 is passed directly to 8 instead of new Integer (8), and the code is much cleaner.
4. Conversion from non-generic to generic
In the lower version, objects in the collection need to be displayed for type conversion before use, such as String s = (String) iter.next (). JDK 5.0 introduces the concept of generics, adds compile-time type safety checks, cancels mandatory type conversions, and saves code. The following is the familiar traditional List operation code:
Listing 15 non-generic code
1. Public static void genericsArrayList ()
2. {
3. List list = new ArrayList ()
4. List.add (0, new Integer (23))
5. Int total = (Integer) list.get (0) .intValue ()
6. System.out.println (total)
7.}
Move the cursor to line 3 (that is, the line of code defined by the collection): Ctrl+Shift+R- > Introduce Generics calls up the generic refactoring dialog box:
Figure 23 generic refactoring dialog box
Specify a data type in Type Argument, and JBuilder has defaulted to the most appropriate data type through the parsing code, which generally does not need to be adjusted. Complete the code refactoring of the generic type after pressing OK:
Code for code listing generics
1. Public static void genericsArrayList ()
2. {
3. List list = new ArrayList ()
4. List.add (0, new Integer (23))
5. Int total = (list.get (0)) .intValue ()
6. System.out.println (total)
7.}
Notice the change in lines 3 and 5. Some people may point out that line 4 should be adjusted to list.add (23). Yes, you only need to call the automatic loading / unpacking refactoring in the previous section to do this.
Thank you for reading! This is the end of this article on "sample analysis of upgrading from JBuilder2005 implementation refactoring to JDK5.0". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.