Refactoriser la section dans IDEA

Cet article peut être considéré comme un gif rapide sur la refactorisation des fichiers Java dans IDEA pour les débutants .

Attention , il y a beaucoup de gifs lourds.

"N'importe quel imbécile peut écrire du code qu'un ordinateur peut comprendre. Les bons programmeurs écrivent du code que les humains peuvent comprendre." —M. Fowler (1999)

Contenu

introduction

Section "Refactor"

- Refactoriser ceci

- Renommer

- Renommer le fichier

- Changer de signature

- Modifier la valeur de la propriété (pas d'exemple)

- Migration de type

- Rendre statique

- Méthode de conversion en instance

- Déplacer les classes

- Copier les classes

- Suppression sécurisée

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

Figure:  1. Section Refactoring IDEA
. 1. Refactoring IDEA

, , idea

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

«Refator»

.

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

. , , . , .

Figure:  2. "Refactor This" pour le nom de la fonction
. 2. «Refactor This»

Figure:  3. "Refactor This" pour les arguments de fonction
. 3. «Refactor This»

«Rename» (Shift+F6)

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

Figure:  4. Renommer la méthode
. 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);
   }
}

Figure:  5. Renommer une variable
. 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);
   }
}

Figure:  6. Renommer la classe imbriquée
. 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 {
   }
}

Figure:  7. Renommer une classe
. 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();
   }
}

Figure:  8. Renommer un package
. 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),

Figure:  9. Exemple d'utilisation de "Renommer le fichier"
. 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", .

Figure:  10. Exemple d'utilisation de "Mettre à jour les utilisations pour refléter le changement de signature"
. 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.   «Change Signature»
. 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.   «Type Migration»
. 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.   «Make Static»
. 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.   «Convert To Instance Method»
. 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.   «Move Classes»
. 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”.

Figure:  38. Exemple d'utilisation de l'élément "Remplacer l'héritage par la délégation"
. 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”).

Figure:  39. Exemple d'utilisation de l'élément "Supprimer l'intermédiaire"
. 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»

-. , .

Figure:  40. Un exemple d'utilisation de l'élément "Valeur de retour de la méthode d'enroulement"
. 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.

Figure:  41. Exemple d'utilisation de l'élément "Encapsuler le champ"
. 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(). , . .

Figure:  42. Un exemple d'utilisation de l'élément "Remplacer la température par une requête"
. 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”).

Figure:  43. Un exemple d'utilisation de l'élément "Remplacer le constructeur par la méthode d'usine"
. 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.

Figure:  44. Un exemple d'utilisation de l'élément "Remplacer le constructeur par le constructeur"
. 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 .

Figure:  45. Un exemple d'utilisation de l'élément "Générer"
. 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»

:

Figure:  46. ​​Liste des migrations disponibles de l'élément "Migrer"
. 46. «Migrate»

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

Figure:  47. Règles de migration pour JUnit (4.x -> 5.0)
. 47. JUnit(4.x -> 5.0)

JUnit(4.x -> 5.0).

«Lombok» «Delombok»

Fourni par le plugin "Lombok". Il a été récemment annoncé qu'il sera désormais inclus dans le package IDEA standard. Utilisé lorsque vous travaillez avec la bibliothèque de génération de code "Lombok".

Élément "Internationaliser"

Utilisé pour l'internationalisation. Malheureusement, je n'ai trouvé aucune information dans l'aide pour le moment. IDEA est maintenant activement localisé dans d'autres langues, cette méthode a probablement été développée pour cela.

Liste des sources




All Articles