Skip to content

Fix #8215 Add ability to detect attempted Tasty inspection of a Java class #8220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 11, 2020
Merged

Fix #8215 Add ability to detect attempted Tasty inspection of a Java class #8220

merged 7 commits into from
Feb 11, 2020

Conversation

gzoller
Copy link
Contributor

@gzoller gzoller commented Feb 5, 2020

Linking #8215

When initiating Tasty Inspection from a class, in some situations it may not be known whether the class is a Java or Scala/Tasty class, and the caller may want to handle the Java classes differently. (Of course an attempt to Tasty Inspect a non-Tasty class will result in an empty root Tree in TastyInspector.)

Unfortunately Java itself offers no way to detect host language in their Class API, but I noticed that ReadTasty fails in a consistent way with Java classes, and that it's possible to read the symbol Flags and detect JavaDefined at that point.

Wanting to minimize changes, I created a JavaCompilationUnit marker so that ReadTasty could proceed as normal upon hitting a Java class, and bubble this result up the call stack. Then I modified the Reflection.Context API to add a method that returns an Option[String] for Java class name (Some(the name) if Java, None if Tasty).

Now within TastyInspector I can simply do:

  protected def processCompilationUnit(reflect: Reflection)(root: reflect.Tree): Unit = 
    import reflect.{given,_}
    reflect.rootContext.getJavaClassname()
      .map( javaClass => doSomethingHereWithJava(javaClass) )
      .orElse(doSomethingHereWithTasty(...))

Anyone not caring about Java classes will have see no change in Dotty's behavior.

Copy link
Member

@dottybot dottybot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, and thank you for opening this PR! 🎉

All contributors have signed the CLA, thank you! ❤️

Have an awesome day! ☀️

@gzoller gzoller changed the title i8215 Add ability to detect attempted Tasty inspection of a Java class Fix 8215 Add ability to detect attempted Tasty inspection of a Java class Feb 5, 2020
@gzoller gzoller changed the title Fix 8215 Add ability to detect attempted Tasty inspection of a Java class Fix #8215 Add ability to detect attempted Tasty inspection of a Java class Feb 5, 2020
@gzoller
Copy link
Contributor Author

gzoller commented Feb 6, 2020

Seeking advice here... I'm getting a build failure but to the best of my knowledge it doesn't seem related to my change. What's the best way to proceed? Worst case I could re-clone the latest master branch and re-apply my changes?

@gzoller
Copy link
Contributor Author

gzoller commented Feb 6, 2020

Ok, after a lot of re-testing, I've learned some things. There is something my change, specifically the change in ReadTasty.scala that breaks the 'from-tasty' test. It shouldn't--but it does. Need to keep digging to see why.

@gzoller
Copy link
Contributor Author

gzoller commented Feb 6, 2020

Narrowing down the problems. In ReadTasty.scala I'm trying to detect the presence of an attempted read of a Java file by looking at the flags of the symbol available at this point:

      def compilationUnit(cls: Symbol): Option[CompilationUnit] = cls match {
        case cls: ClassSymbol =>
          (cls.rootTreeOrProvider: @unchecked) match {
            case unpickler: tasty.DottyUnpickler =>
               //... some code...
            case tree: Tree[?] =>
              println("   Flags: "+cls.flags)
              alreadyLoaded()
            case _ =>
              cannotUnpickle(s"its class file does not have a TASTY attribute")
          }

Here's where the fun starts. I can try calling ReadTasty in 2 cases: 1) passing in a Java file and 2) passing in a Scala file w/o a .tasty file, scala.collection.immutable.RedBlackTree in the case of the test code.

Here's what's weird... In both cases cls, the ClassSymbol, is the same kind of thing: dotty.tools.dotc.core.Symbols$ClassSymbol. But... in the case of a Java file there are flags available, and calling cls.flags works fine and I can test for JavaDefined (true). In the case of a non-Tasty Scala class though, just trying to access cls.flags blows up as a java.lang.UnsupportedOperationException! Didn't expect that. Going to have to dig deeper...

@gzoller
Copy link
Contributor Author

gzoller commented Feb 7, 2020

Yay--bug found and fixed! Reviews and comments invited.

@nicolasstucki nicolasstucki self-requested a review February 7, 2020 22:11
@nicolasstucki nicolasstucki self-assigned this Feb 7, 2020
@nicolasstucki
Copy link
Contributor

Thank you @gzoller

@nicolasstucki nicolasstucki merged commit 8fa5c5c into scala:master Feb 11, 2020
@anatoliykmetyuk anatoliykmetyuk added this to the 0.23.0-RC1 milestone Mar 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Need a way to gracefully handle attempts to TASTy-Inspect Java classes
4 participants