redhat-developer / redhat-developer/vscode-java

Inline Method leaves unqualified instance method call inside local class when inlining into static context

Ouverte
#4,464 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

bug
Langage dominant
TypeScript
Étoiles
2.3k
Forks
546
Merge moyen
20 h 1 min
PR mergées (30 j)
11

Description

Description

Inline Method produces uncompilable code when inlining an instance method that contains a local class into the static method main.

Before refactoring, the local class H is declared inside the instance method f(). The call helper() inside H.g() is valid because it is implicitly bound to the enclosing I01 instance.

After applying Inline Method to the invocation obj.f(), the local class H is moved into the static method main. However, the call inside H.g() remains:

return helper();

At this point, there is no implicit I01.this available in the static context. The call should be qualified with the original receiver, for example:

return obj.helper();

The refactored code no longer compiles.

Code before refactoring

public class I01 {
    public static void main(String[] args) {
        I01 obj = new I01();

        // Refactoring operation: Inline Method
        // Target invocation: obj.f()
        int result = obj.f();

        System.out.println(result);
    }

    int helper() {
        return 7;
    }

    int f() {
        class H {
            int g() {
                return helper();
            }
        }

        return H.class.getSimpleName().length();
    }
}

Code after refactoring

public class I01 {
    public static void main(String[] args) {
        I01 obj = new I01();

        class H {
            int g() {
                return helper();
            }
        }

        // Refactoring operation: Inline Method
        // Target invocation: obj.f()
        int result = H.class.getSimpleName().length();

        System.out.println(result);
    }

    int helper() {
        return 7;
    }

    int f() {
        class H {
            int g() {
                return helper();
            }
        }

        return H.class.getSimpleName().length();
    }
}

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par l’implémentation de Inline Method et reproduisez le problème à l’aide de l’exemple I01, en vous concentrant sur les classes locales déplacées dans des contextes statiques de main. Suivez la manière dont le récepteur du obj.f() d’origine est géré dans H.g() ; c’est terminé lorsque l’appel généré à helper() est qualifié avec le récepteur et que le code Java refactorisé compile.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
java
Domaine
devtools
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
Calme
Clarté
Plutôt claire
Accessibilité débutants
48/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.