IDEAのリファクタリングセクション

この記事は、初心者向けのIDEAでのJavaファイルのリファクタリングに関する簡単なgifと考えることができます

注意してください、重いgifがたくさんあります。

「愚か者なら誰でも、コンピューターが理解できるコードを書くことができます。優れたプログラマーは、人間が理解できるコードを書くことができます。」—M。ファウラー(1999)

コンテンツ

前書き

セクション「リファクタリング」

-これをリファクタリング

-名前を変更

- ファイル名を変更する

-署名を変更します

-プロパティ値の編集(例なし)

-タイプの移行

-静的にする

-インスタンスメソッドに変換

-クラスの移動

-クラスのコピー

-安全な削除

-抽出/紹介

---変数

- - 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.関数名の「これをリファクタリング」
. 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.   «Copy Classes»
. 16. «Copy Classes»

«Safe Delete» (Alt+Delete)

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

 . 17.   «Safe Delete»
. 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.アイテム「ConstructorをBuilderに置き換える」の使用例
. 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