Giter VIP home page Giter VIP logo

Comments (6)

ammachado avatar ammachado commented on June 20, 2024 1

Findings:

  1. Compilation errors being swallowed for classes provided via dependsOn, if a class is not declared on the default package:
1255073806633000:3: error: class MySchemaType is public, should be declared in a file named MySchemaType.java
public class MySchemaType {}
       ^

There's a fix for this issue: #4040

  1. Identified a possible small optimization on ChangeTypeVisitor - There's a fix for this issue: #4039

  2. Package names used the failing test does not follow the the conventions, although their values are valid. For example, ChangeType uses JavaType.ShallowClass.build(oldFullyQualifiedTypeName) to create type references. The build method identifies names based on conventions; it considers that if an uppercase letter comes after a dot, the next identifier is a class name.

from rewrite.

ammachado avatar ammachado commented on June 20, 2024 1

Updated the last comment with the fixes for the first 2 findings.

from rewrite.

timtebeek avatar timtebeek commented on June 20, 2024

That's a strange case indeed; thanks for the detailed runnable example. I wouldn't quite know why that's missed here. I'm guessing you ran into this when doing something similar in an actual recipe beyond this test case?

Thanks for the offer to look into this as well!

from rewrite.

timtebeek avatar timtebeek commented on June 20, 2024

Thanks for the fixes so far! I've given the test that you provided above another go and I can replicate it using only a capital letter in the package name already 🤔

@Test
void renamePackageWithDependsOn() {
    @Language("java") final String schemaClass1 = """
      package foo.Capital;
      public class MySchemaType {}
      """;
    @Language("java") final String schemaClass2 = """
      package bar.Capital;
      public class MySchemaType {}
      """;

    rewriteRun(
      spec -> spec
        .parser(JavaParser.fromJavaVersion().dependsOn(schemaClass1, schemaClass2))
        .recipe(new ChangeType(
          "foo.Capital.MySchemaType",
          "bar.Capital.MySchemaType",
          true
        )),
      //language=java
      java(
        """
          package my.example.app;
          
          import foo.Capital.MySchemaType;
          
          class MyApp {
              void doWork(MySchemaType mySchema) {}
          }
          """,
        """
          package my.example.app;
          
          import bar.Capital.MySchemaType;
          
          class MyApp {
              void doWork(MySchemaType mySchema) {}
          }
          """
      )
    );
}

Which again fails with

diff --git a/my/example/app/MyApp.java b/my/example/app/MyApp.java
index 7fe83d7..9ff90dd 100644
--- a/my/example/app/MyApp.java
+++ b/my/example/app/MyApp.java
@@ -1,7 +1,5 @@ 
 package my.example.app;
 
-import bar.Capital.MySchemaType;
-
 class MyApp {
     void doWork(MySchemaType mySchema) {}
 }

from rewrite.

timtebeek avatar timtebeek commented on June 20, 2024

Look like we misidentify an owning class where there is none, just because the package contains an uppercase letter. :/

image

from rewrite.

ammachado avatar ammachado commented on June 20, 2024

Here's a simpler test:

@Test
void changeTypeWithCapitalLettersInPackageName() {
    @Language("java") final String schemaClass = """
      package bar.Capital;
      public class MySchemaType {}
      """;

    rewriteRun(
      java(schemaClass, spec -> spec.afterRecipe(cu -> {
            JavaType.FullyQualified typeFromParser = cu.getClasses().get(0).getType();

            // Called on ChangeClassDefinition constructor
            JavaType.ShallowClass shallowClass = JavaType.ShallowClass
              .build(typeFromParser.getFullyQualifiedName());

            assertThat(shallowClass)
              .hasFieldOrPropertyWithValue("fullyQualifiedName", typeFromParser.getFullyQualifiedName())
              .hasFieldOrPropertyWithValue("owningClass", typeFromParser.getOwningClass());
        }
      ))
    );
}

And a variant for classes starting with lowercase letters, also failing:

@Test
void changeTypeWithClassNamesStartingWithLowercaseLetters() {
    @Language("java") final String schemaClass = """
      package bar.capital;
      public class mySchemaType {}
      """;

    rewriteRun(
      java(schemaClass, spec -> spec.afterRecipe(cu -> {
            JavaType.FullyQualified typeFromParser = cu.getClasses().get(0).getType();

            // Called on ChangeClassDefinition constructor
            JavaType.ShallowClass shallowClass = JavaType.ShallowClass
              .build(typeFromParser.getFullyQualifiedName());

            assertThat(shallowClass)
              .hasFieldOrPropertyWithValue("fullyQualifiedName", typeFromParser.getFullyQualifiedName())
              .hasFieldOrPropertyWithValue("owningClass", typeFromParser.getOwningClass());
        }
      ))
    );
}

from rewrite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.