IDEA中的重构部分

对于初学者,可以将本文视为在IDEA中重构Java文件的快速gif

小心,有很多沉重的GIF。

“任何傻瓜都可以编写计算机可以理解的代码。优秀的程序员可以编写人类可以理解的代码。” -M. Fowler(1999)

内容

介绍

“重构”部分

-重构这个

- 改名

- 重新命名文件

-更改签名

-编辑属性值(无示例)

-类型迁移

-使静态

-转换为实例方法

-上课

-复制课程

- Safe Delete

- Extract/Introduce

- - Variable

- - Constant

- - Field

- - Parameter

- - Functional Parameter

- - Functional Variable

- - Parameter Object

- - Method

- - Type Parameter ( )

- - Interface

- - Superclass

- - Subquery as CTE ( )

- - RSpec 'let' ( )

- Inline

- Find and Replace Code Duplicate

- Pull Member Up

- Pull Member Down

- Push ITds In

- Use Interface Where Possible

- Replace Inheritance with Delegation

- Remove Middleman

- Wrap Method Return Value

- Encapsulate Field

- Replace Temp with Query

- Replace Constructor with Factory Method

- Replace Constructor with Builder

- Generify

- Migrate

- Lombok Delombok ( )

- Internationalize

- Java- ( ). ( ).

, , IDEA, , . , , Refator:

数字: 1.节重构IDEA
. 1. Refactoring IDEA

, , idea

, . , , Refactoring: Improving the Design of Existing Code (Martin Fowler). gif-, . Windows/LInux .

«Refator»

.

«Refactor This» (Ctrl+Alt+Shift+T)

. , , . , .

数字: 2.函数名称的“ Refactor This”
. 2. «Refactor This»

数字: 3.函数参数的“重构”
. 3. «Refactor This»

«Rename» (Shift+F6)

, . , , . ( 2 - “Rename Field” “Rename Variable”)

数字: 4.重命名方法
. 4.

public class Main {
   public static void main(String[] args) {
       System.out.print("Hello");
       invo<caret/>ke(", World");
   }

   private static void invoke(String text) {
       //text
       System.out.println(text);
   }
}

public class Main {
   public static void main(String[] args) {
       System.out.print("Hello");
       newFunctionName(", World");
   }

   private static void newFunctionName(String text) {
       //text
       System.out.println(text);
   }
}

数字: 5.重命名变量
. 5.

public class Main {
   public static void main(String[] args) {
       System.out.print("Hello");
       invoke(", World");
   }

   private static void invoke(String te<caret>xt) {
       //text
       System.out.println(text);
   }
}

public class Main {
   public static void main(String[] args) {
       System.out.print("Hello");
       invoke(", World");
   }

   private static void invoke(String newText) {
       //newText
       System.out.println(newText);
   }
}

数字: 6.重命名嵌套类
. 6.

public class Main {
   public static void main(String[] args) {
       System.out.print("Hello");
       invoke(", World");
   }

   private static void invoke(String text) {
       //text
       System.out.println(text);
       throw new MyExc<caret>eption();
   }

   public static class MyException extends RuntimeException {
   }
}

public class Main {
   public static void main(String[] args) {
       System.out.print("Hello");
       invoke(", World");
   }

   private static void invoke(String text) {
       //text
       System.out.println(text);
       throw new NewMyException ();
   }

   public static class NewMyException extends RuntimeException {
   }
}

数字: 7.重命名课程
. 7.

public class Main {
   public static void main(String[] args) {
       MyS<caret>ervice service = new MyService();
       service.service();
   }
}

public class Main {
   public static void main(String[] args) {
       NewMyService myService = new NewMyService ();
       myService.service();
   }
}

数字: 8.重命名程序包
. 8.
package gen<caret>eral;

public class Main {
   public static void main(String[] args) {
       NewMyService service = new NewMyService();
       service.service();
   }
}

package org.test.java.src;

public class Main {
   public static void main(String[] args) {
       NewMyService service = new NewMyService();
       service.service();
   }
}

«Rename File»

. Shift+F6 . (Scope),

数字: 9.使用“重命名文件”的示例
. 9. «Rename File»

public class Main {
   public static void main(String[] args) throws IOException {
       Path path = Paths.get("src/general/TestFile.txt");

       String read = Files.readAllLines(path).get(0);
       System.out.println(read);
   }
}

public class Main {
   public static void main(String[] args) throws IOException {
       Path path = Paths.get("src/general/TestFile2.txt");

       String read = Files.readAllLines(path).get(0);
       System.out.println(read);
   }
}

«Change Signature» (Ctrl+F6)

“Change Function Declaration”. IDEA «Change Signature» . :

  • - - ,

  • - .

( )

. "R" "Update usages to reflect signature change", .

数字: 10.使用“更新用法以反映签名更改”的示例
. 10. «Update usages to reflect signature change»

public class ChangeSignature {
   public static void main(String[] args) {
       invokeMethod("Hello");
       invokeMethod("World");
   }

   private static void invokeMethod(String text) {
       System.out.println(text);
   }
}

public class ChangeSignature {
   public static void main(String[] args) {
       invokeMethod("Hello", null);
       invokeMethod("World", null);
   }

   private static void invokeMethod(String text, String newType) {
       System.out.println(text);
   }
}

( )

, exception, .

数字: 11.使用“更改签名”的示例
. 11. «Change Signature»

public class ChangeSignature {
   public static void main(String[] args) {
       invokeMethod("Hello");
       invokeMethod("World");
   }

   private static void invokeMethod(String<caret> text) {
       System.out.println(text);
   }
}

public class ChangeSignature {
   public static void main(String[] args) {
       invokeMethod("Hello");
       invokeMethod("World");
   }

   private static void invokeMethod(String text) {
       invokeMethod(text, null);
   }

   private static void invokeMethod(String text, String newName) {
       System.out.println(text);
   }
}

«Edit Property Value» (Alt + F6)

(Idea 2020.2) . property.value.inplace.editing=true .

«Type Migration» (Ctrl + Shift + F6)

, , .

数字: 12.使用“类型迁移”的示例
. 12. «Type Migration»

public class ChangeSignature {

   public static void main(String[] args) {
       Inte<caret>ger hello = 1;
       print(hello);
   }

   private static void print(Integer text) {
       System.out.println(text);
   }
}

public class ChangeSignature {

   public static void main(String[] args) {
       Number hello = 1;
       print(hello);
   }

   private static void print(Number text) {
       System.out.println(text);
   }
}

«Make Static» (Ctrl + Shift + F6)

. ( Convert To Instance Method)

数字: 13.使用“使静态”的示例
. 13. «Make Static»

public class MakeStatic {
   public static void main(String[] args) {
       MakeStatic makeStatic = new MakeStatic();
       makeStatic.sayHello();
   }

   public void say<caret>Hello() {
       System.out.println("Hello, World");
   }
}

public class MakeStatic {
   public static void main(String[] args) {
       MakeStatic makeStatic = new MakeStatic();
       MakeStatic.sayHello();
   }

   public static void sayHello() {
       System.out.println("Hello, World");
   }
}

«Convert To Instance Method»

( ”Make Static”). .

数字: 14.使用“转换为实例方法”的示例
. 14. «Convert To Instance Method»

public class MakeStatic {
   public static void main(String[] args) {
       sayHello();
   }

   public static void sa<caret>yHello() {
       System.out.println("Hello, World");
   }
}

public class MakeStatic {
   public static void main(String[] args) {
       new MakeStatic().sayHello();
   }

   public void sayHello() {
       System.out.println("Hello, World");
   }
}

«Move Classes» (F6)

, , .

数字: 15.使用“移动类”的示例
. 15. «Move Classes»

package org.example.test.service;

public class TestService {
<caret>
}

package org.example.test;

public class TestService {
}

«Copy Classes» (F5)

, . F5. e, .

数字: 16.使用“复制类”的示例
. 16. «Copy Classes»

«Safe Delete» (Alt+Delete)

, (Alt + Enter), . , , - F2( ) Alt + Enter Alt + Delete. , , . IDEA , IDEA , - Usages Detected. - “Remove Dead Code”

数字: 17.使用“安全删除”的示例
. 17. «Safe Delete»

package org.example.test;

public class MainClass {
   public static void main(String[] args) {
       start();
   }

   private static void start() {
       String unUsedVariable;
       System.out.println("Hello, World!");
   }

   private static void unUsedMethod() {

   }
}

<empty>

«Extract/Introduce»

- Extract/Introduce. , . .

 . 18.     «Extract/Introduce»
. 18. «Extract/Introduce»

«Variable» (Ctrl+Alt+V)

. ( “Extract Variable”).

 . 19.    «Extract/Introduce->Variable»
. 19. «Extract/Introduce->Variable»

public class ExtractVariable {
   public static void main(String[] args) {
       sayHello();
   }

   private static void sayHello() {
       System.out.println("He<caret>llo, World!");
   }
}

public class ExtractVariable {
   public static void main(String[] args) {
       sayHello();
   }

   private static void sayHello() {
       String text = "Hello, World!";
       System.out.println(text);
   }
}

«Constant» (Ctrl+Alt+C)

.

 . 20.    «Extract/Introduce->Constant»
. 20. «Extract/Introduce->Constant»

public class ExtractVariable {
   public static void main(String[] args) {
       sayHello();
   }

   private static void sayHello() {
       System.out.println("He<caret>llo, World!");
   }
}

public class ExtractVariable {

   public static final String HELLO_WORLD = "Hello, World!";

   public static void main(String[] args) {
       sayHello();
   }

   private static void sayHello() {
       System.out.println(HELLO_WORLD);
   }
}

«Field» (Ctrl+Alt+F)

.

 . 21.    «Extract/Introduce->Field»
. 21. «Extract/Introduce->Field»

public class ExtractVariable {
   public static void main(String[] args) {
       sayHello();
   }

   private static void sayHello() {
       System.out.println("He<caret>llo, World!");
   }
}

public class ExtractVariable {

   private static String x;

   public static void main(String[] args) {
       sayHello();
   }

   private static void sayHello() {
       x = "Hello, World!";
       System.out.println(x);
   }
}

«Parameter» (Ctrl+Alt+P)

() .

. 22.    «Extract/Introduce->Parameter»
. 22. «Extract/Introduce->Parameter»

public class ExtractVariable {
   public static void main(String[] args) {
       sayHello();
   }

   private static void sayHello() {
       System.out.println("He<caret>llo, World!");
   }
}

public class ExtractVariable {
   public static void main(String[] args) {
       sayHello("Hello, World!");
   }

   private static void sayHello(String x) {
       System.out.println(x);
   }
}

«Functional Parameter»

«Parameter», java.util.function.Supplier, javafx.util.Builder. , .

. 23.    «Extract/Introduce->Functional Parameter»
. 23. «Extract/Introduce->Functional Parameter»

public class ExtractParameter {
   public static void main(String[] args) {
       System.out.println(generateText());
   }
   private static String generateText() {
       return "Hello, Wor<caret>ld!".toUpperCase();
   }
}

public class ExtractParameter {
   public static void main(String[] args) {
       System.out.println(generateText(() -> "Hello, World!"));
   }
   private static String generateText(final Supplier<string> getText) {
       return getText.get().toUpperCase();
   }
}

«Functional Variable»

«Variable», java.util.function.Supplier javafx.util.Builder.

 . 24.    «Extract/Introduce->Functional Variable»
. 24. «Extract/Introduce->Functional Variable»

public class ExtractParameter {
   public static void main(String[] args) {
       System.out.println(generateText());
   }
   private static String generateText() {
       return "Hello, W<caret>orld!".toUpperCase();
   }
}

public class ExtractParameter {
   public static void main(String[] args) {
       System.out.println(generateText());
   }
   private static String generateText() {
       Supplier<string> getText = () -> "Hello, World!";
       return getText.get().toUpperCase();
   }
}

ParameterObject

«Parameter Object»

, . ( “Introduce Parameter Object”).

. 25.    «Extract/Introduce->Parameter Object»
. 25. «Extract/Introduce->Parameter Object»

public class ExtractParameter {
   public static void main(String[] args) {
       System.out.println(generateText("Hello", "World!"));
   }
   private static String generateText(Str<caret>ing hello, String world) {
       return hello.toUpperCase() + world.toUpperCase();
   }
}

public class ExtractParameter {
   public static void main(String[] args) {
       System.out.println(generateText(new HelloWorld("Hello", "World!")));
   }
   private static String generateText(HelloWorld helloWorld) {
       return helloWorld.getHello().toUpperCase() + helloWorld.getWorld().toUpperCase();
   }

   private static class HelloWorld {
       private final String hello;
       private final String world;

       private HelloWorld(String hello, String world) {
           this.hello = hello;
           this.world = world;
       }

       public String getHello() {
           return hello;
       }

       public String getWorld() {
           return world;
       }
   }
}

«Method» (Ctrl+Alt+M)

. ( - “Extract Function”).

 . 26.    «Extract/Introduce->Method»
. 26. «Extract/Introduce->Method»

public class ExtractMethod {
   public static void main(String[] args) {
       String text = "Hello, World!";
       System.out.prin<caret>tln(text);
   }
}

public class ExtractMethod {
   public static void main(String[] args) {
       String text = "Hello, World!";
       print(text);
   }

   private static void print(String text) {
       System.out.println(text);
   }
}

«Type Parameter»

Kotlin, Java ( , - ).

«Replace Method With Method Object»

. , ( -).

. 27.    «Extract/Introduce->Replace Method With Method Object»
. 27. «Extract/Introduce->Replace Method With Method Object»

public class ExtractMethod {
   public static void main(String[] args) {
       String text = "Hello, World!";
       print(text);
   }

   private static void print(String text) {
       System.out.p<caret>rintln(text);
   }
}

public class ExtractMethod {
   public static void main(String[] args) {
       String text = "Hello, World!";
       print(text);
   }

   private static void print(String text) {
       new Printer(text).invoke();
   }

   private static class Printer {
       private String text;

       public Printer(String text) {
           this.text = text;
       }

       public void invoke() {
           System.out.println(text);
       }
   }
}

«Delegate»

.

 . 28.    «Extract/Introduce->Delegate»
. 28. «Extract/Introduce->Delegate»

public class Delegate {
   public static void main(String[] args) {
       new Delegate().print();
   }

   private void print() {
       System.ou<caret>t.println("Hello, World!");
   }
}

public class Delegate {
   private final Printer printer = new Printer();

   public static void main(String[] args) {
       new Delegate().print();
   }

   private void print() {
       printer.print();
   }

   public static class Printer {
       public Printer() {
       }

       private void print() {
           System.out.println("Hello, World!");
       }
   }
}

«Interface»

. ( , Spring, - )

 . 29.    «Extract/Introduce->Interface»
. 29. «Extract/Introduce->Interface»

public class ExtractImpl {
   public static void main(String[] args) {
       new ExtractImpl().print();
   }

   public void print() {
       System.out.println("Hello, World!");
   }
}

public class ExtractImpl implements ExtractInterface {
   public static void main(String[] args) {
       new ExtractImpl().print();
   }

   @Override
   public void print() {
       System.out.println("Hello, World!");
   }
}

public interface ExtractInterface {
   void print();
}

«Superclass»

«Interface», - (Superclass). “Extract Superclass”.

. 30.    «Extract/Introduce->Superclass»
. 30. «Extract/Introduce->Superclass»

public class ExtractImpl {
   public static void main(String[] args) {
       new ExtractImpl().print();
   }

   public void print() {
       System.out.println("Hello, World!");
   }
}

public class ExtractImpl extends ExtractAbstr {
   public static void main(String[] args) {
       new ExtractImpl().print();
   }

}

public class ExtractAbstr {
   public void print() {
       System.out.println("Hello, World!");
   }
}

«Subquery as CTE»

Sql, . - , - .

«RSpec 'let'»

Ruby, - , - .

«Inline»

, . “Inline Class”, “Inline Function”, “Inline Variable”.

 . 31.     «Inline»
. 31. «Inline»

public class Inline {
   public static void main(String[] args) {
       print();
   }

   private static void print() {
       new Printer().print();
   }

   private static class Printer {
       public void print() {
           String text = "Hello, World!";
           System.out.println(t<caret>ext);
       }
   }
}

public class Inline {
   public static void main(String[] args) {
           System.out.println("Hello, World!");
   }
}

«Find and Replace code duplicate»

, , .

 . 32.     «Find and Replace code duplicate»
. 32. «Find and Replace code duplicate»

public class Replace {
   public static void main(String[] args) {
       System.out.println("Hello, World!");
   }

   public void print() {
       System.out.println("Hello, World!");
   }

   public void print2() {
       System.out.prin<caret>tln("Hello, World!");
   }
}

public class Replace {
   public static void main(String[] args) {
       print2();
   }

   public void print() {
       print2();
   }

   public static void print2() {
       System.out.println("Hello, World!");
   }
}

«Invert Boolean»

.

. 33.     «Invert Boolean»
. 33. «Invert Boolean»

public class Invert {
   public static void main(String[] args) {
       boolean co<caret>ndition = true;
       if (condition) {
           System.out.println("Hello, World!");
       }
   }
}

public class Invert {
   public static void main(String[] args) {
       boolean condition = false;
       if (!condition) {
           System.out.println("Hello, World!");
       }
   }
}

«Pull Member Up»

. “Pull Up Field” “Pull Up Method”. «Pull Member Down».

. 34.     «Pull Member Up»
. 34. «Pull Member Up»

public class PullMethod {
   public static void main(String[] args) {
       new InnerClass().print();
   }

   private static class InnerClass extends AbstClass {
       public void print() {
           System.out.pri<caret>ntln("Hello, World");
       }
   }

   private static abstract class AbstClass {
   }
}

public class PullMethod {
    public static void main(String[] args) {
        new InnerClass().print();
    }

    private static class InnerClass extends AbstClass {
    }

    private static abstract class AbstClass {
        public void print() {
            System.out.println("Hello, World");
        }
    }
}

«Pull Member Down»

«Pull Member Up». . ( - “Push Down Method”)

. 35.     «Pull Member Down»
. 35. «Pull Member Down»

public class PullMethod {
   public static void main(String[] args) {
       new InnerClass().print();
   }

   private static class InnerClass extends AbstClass {
   }

   private static abstract class AbstClass {
       public void print() {
           System.out.prin<caret>tln("Hello, World");
       }
   }
}

public class PullMethod {
   public static void main(String[] args) {
       new InnerClass().print();
   }

   private static class InnerClass extends AbstClass {
       @Override
       public void print() {
           System.out.println("Hello, World");
       }
   }

   private static abstract class AbstClass {
       public abstract void print();
   }
}

«Push ITds In»

AsperctJ.

 . 36.     «Push ITds In»
. 36. «Push ITds In»

aspect myAspect {
   boolean Account.closed = <caret>false;
   void Account.close() {
       closed = true;
   }
}
class Account {

}

aspect myAspect {
   boolean Account.closed = false;
}
class Account {

   void close() {
       closed = true;
   }
}

«Use Interface Where Possible»

IDEA , , .

. 37.     «Use Interface Where Possible»
. 37. «Use Interface Where Possible»

public class ExtractInterface {

   public static void main(String[] args) {
       InnerClass innerClass = new InnerClass();
       print(innerClass);
   }

   private static void print(InnerClass innerClass) {
       innerClass.print();
   }

   private static class InnerClass implements InnerInterface{
       @Override
       public void print() {
           System.out.println("Hello, World!");
       }
   }

   private static interface InnerInterface{
       void print();
   }
}

public class ExtractInterface {

   public static void main(String[] args) {
       InnerInterface innerClass = new InnerClass();
       print(innerClass);
   }

   private static void print(InnerInterface innerClass) {
       innerClass.print();
   }

   private static class InnerClass implements InnerInterface{
       @Override
       public void print() {
           System.out.println("Hello, World!");
       }
   }

   private static interface InnerInterface{
       void print();
   }
}

«Replace Inheritance with Delegation»

. “Replace Subclass with Delegate” “Replace Superclass with Delegate”.

. 38.     «Replace Inheritance with Delegation»
. 38. «Replace Inheritance with Delegation»

public class InheritanceDelegation {

   public static void main(String[] args) {
       InnerClass innerClass = new InnerClass();
       print(innerClass);
   }

   private static void print(InnerClass innerClass) {
       innerClass.print();
   }

   private static class In<caret>nerClass extends AbstractClass {
   }

   private static class AbstractClass {
       public void print() {
           System.out.println("Hello, World!");
       }
   }
}

public class InheritanceDelegation {

    public static void main(String[] args) {
        InnerClass innerClass = new InnerClass();
        print(innerClass);
    }

    private static void print(InnerClass innerClass) {
        innerClass.print();
    }

    private static class InnerClass {
        private final AbstractClass abstractClass = new AbstractClass();

        public void print() {
            abstractClass.print();
        }
    }

    private static class AbstractClass {
        public void print() {
            System.out.println("Hello, World!");
        }
    }
}

«Remove Middleman»

. ( - “Remove Middle Man”).

 . 39.     «Remove Middleman»
. 39. «Remove Middleman»

public class Middleman {

   public static void main(String[] args) {
       InnerClass innerClass = new InnerClass();
       innerClass.print();
   }

   private static class InnerClass {
       private final NextClass next<caret>Class = new NextClass();

       public void print() {
           nextClass.print();
       }
   }

   private static class NextClass {
       public void print() {
           System.out.println("Hello, World!");
       }
   }
}

public class Middleman {

   public static void main(String[] args) {
       InnerClass innerClass = new InnerClass();
       innerClass.getNextClass().print();
   }

   private static class InnerClass {
       private final NextClass nextClass = new NextClass();

       public NextClass getNextClass() {
           return nextClass;
       }
   }

   private static class NextClass {
       public void print() {
           System.out.println("Hello, World!");
       }
   }
}

«Wrap Method Return Value»

-. , .

 . 40.     «Wrap Method Return Value»
. 40. «Wrap Method Return Value»

public class WrapMethodReturnValue {
   public static void main(String[] args) {
       System.out.println(new MessageFolder().get());
   }

   private static class MessageFolder {
       public String get() {
           ret<caret>urn "Hello, World!";
       }
   }
}

public class WrapMethodReturnValue {
   public static void main(String[] args) {
       System.out.println(new MessageFolder().get().getValue());
   }

   private static class MessageFolder {
       public Message get() {
           return new Message("Hello, World!");
       }

       public class Message {
           private final String value;

           public Message(String value) {
               this.value = value;
           }

           public String getValue() {
               return value;
           }
       }
   }
}

«Encapsulate Field»

getter, setter.

 . 41.     «Encapsulate Field»
. 41. «Encapsulate Field»

public class EncapsulateField {
   public static void main(String[] args) {
       System.out.println(new InnerClass().message);
   }

   private static class InnerClass {
       public String m<caret>essage = "Hello, World!";
   }
}

public class EncapsulateField {
   public static void main(String[] args) {
       System.out.println(new InnerClass().getMessage());
   }

   private static class InnerClass {
       private String message = "Hello, World!";

       public String getMessage() {
           return message;
       }

       public void setMessage(String message) {
           this.message = message;
       }
   }
}

«Replace Temp with Query»

int size = getActualSize()

size getActualSize(). , . .

 . 42.     «Replace Temp with Query»
. 42. «Replace Temp with Query»

public class ReplaceTemp {
   public static void main(String[] args) {
       String hello = "Hello";
       String mes<caret>sage = hello + ", World!";
       System.out.println(message);
   }
}

public class ReplaceTemp {
   public static void main(String[] args) {
       String hello = "Hello";
       System.out.println(message(hello));
   }

   private static String message(String hello) {
       return hello + ", World!";
   }
}

«Replace Constructor with Factory Method»

. , Lombok. ( “Replace Constructor with Factory Function”).

数字: 43.使用项目“用工厂方法替换构造函数”的示例
. 43. «Replace constructor with factory method»

public class ReplaceConstructor {
   public static void main(String[] args) {
       new InnerClass("Hello", "World").print();
   }

   private static class InnerClass {
       private String message;

       public Inner<caret>Class(String hello, String world) {
           message = hello + ", " + world;
       }

       public void print() {
           System.out.println(message);
       }
   }
}

public class ReplaceConstructor {
   public static void main(String[] args) {
       InnerClass.createInnerClass("Hello", "World").print();
   }

   private static class InnerClass {
       private String message;

       private InnerClass(String hello, String world) {
           message = hello + ", " + world;
       }

       public static InnerClass createInnerClass(String hello, String world) {
           return new InnerClass(hello, world);
       }

       public void print() {
           System.out.println(message);
       }
   }
}

«Replace Constructor with Builder»

builder . , Lombok.

数字: 44.使用项目“用构建器替换构造器”的示例
. 44. «Replace Constructor with Builder»

public class ReplaceConstructor {
   public static void main(String[] args) {
       new InnerClass("Hello", "World").print();
   }

   private static class InnerClass {
       private String message;

       public InnerC<caret>lass(String hello, String world) {
           message = hello + ", " + world;
       }

       public void print() {
           System.out.println(message);
       }
   }
}

public class ReplaceConstructor {
   public static void main(String[] args) {
       new InnerClassBuilder().setHello("Hello").setWorld("World").createInnerClass().print();
   }

   static class InnerClass {
       private String message;

       public InnerClass(String hello, String world) {
           message = hello + ", " + world;
       }

       public void print() {
           System.out.println(message);
       }
   }
}

public class InnerClassBuilder {
   private String hello;
   private String world;

   public InnerClassBuilder setHello(String hello) {
       this.hello = hello;
       return this;
   }

   public InnerClassBuilder setWorld(String world) {
       this.world = world;
       return this;
   }

   public ReplaceConstructor.InnerClass createInnerClass() {
       return new ReplaceConstructor.InnerClass(hello, world);
   }
}

«Generify»

raw- Generic-. java 1.5 .

数字: 45.使用“ Generify”项目的示例
. 45. «Generify»

public class Generify {
   public static void main(String[] args) {
       List list = getList();
       Object message = list.get(0);
       System.out.println(message);
   }

   private static List getList() {
       ArrayList arrayList = new ArrayList();
       arrayList.add("Hello, World!");
       return arrayList;
   }
}

public class Generify {
   public static void main(String[] args) {
       List<string> list = getList();
       String message = list.get(0);
       System.out.println(message);
   }

   private static List<string> getList() {
       ArrayList<string> arrayList = new ArrayList&lt;>();
       arrayList.add("Hello, World!");
       return arrayList;
   }
}

«Migrate»

:

数字: 46.``迁移''项目的可用迁移列表
. 46. «Migrate»

. , , JUnit(4.x -> 5.0):

数字: 47. JUnit的迁移规则(4.x-> 5.0)
. 47. JUnit(4.x -> 5.0)

JUnit(4.x -> 5.0).

«Lombok» «Delombok»

由插件“ Lombok”提供。最近宣布,它将现在包含在标准IDEA软件包中。在使用“ Lombok”代码生成库时使用。

项目“国际化”

用于国际化。不幸的是,我目前没有在帮助中找到任何信息。IDEA现在正在积极地本地化为其他语言,很可能是为此开发了这种方法。

来源清单




All Articles