Skip to content

Commit 95f38af

Browse files
committed
i8215 Add ability to detect attempted Tasty inspection of a Java class
1 parent 5132664 commit 95f38af

File tree

6 files changed

+52
-1
lines changed

6 files changed

+52
-1
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package dotty.tools.dotc.fromtasty
2+
3+
import dotty.tools.dotc.CompilationUnit
4+
import dotty.tools.dotc.util.NoSource
5+
6+
/**
7+
* A marker CompilationUnit to return up the call stack from ReadTasty. This will tell us that we've
8+
* encountered, and attempted to inspect, a Java class file. We can't TASTy-inspect a Java class obviously,
9+
* but we want to return the fact we found it so that higher-up we can take appropriate action if desired.
10+
*/
11+
class JavaCompilationUnit(val className: String) extends CompilationUnit(NoSource) {
12+
override def toString: String = s"java class file $className"
13+
}

compiler/src/dotty/tools/dotc/fromtasty/ReadTasty.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ class ReadTasty extends Phase {
4848
Some(unit)
4949
}
5050
case tree: Tree[?] =>
51-
alreadyLoaded()
51+
if( cls.flags.is(Flags.JavaDefined) ) // Is this a Java class we've tried to inspect?
52+
Some(JavaCompilationUnit(cls.fullName.toString))
53+
else
54+
alreadyLoaded()
5255
case _ =>
5356
cannotUnpickle(s"its class file does not have a TASTY attribute")
5457
}

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
6565
def Context_requiredClass(self: Context)(path: String): Symbol = self.requiredClass(path)
6666
def Context_requiredModule(self: Context)(path: String): Symbol = self.requiredModule(path)
6767
def Context_requiredMethod(self: Context)(path: String): Symbol = self.requiredMethod(path)
68+
def Context_getJavaClassname(self: Context): Option[String] =
69+
self.compilationUnit match {
70+
case j: fromtasty.JavaCompilationUnit => Some(j.className)
71+
case _ => None
72+
}
6873

6974

7075
///////////////

library/src/scala/tasty/Reflection.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,9 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
473473
/** Get method symbol if method is either defined in current compilation run or present on classpath. Throws if the method has an overload. */
474474
def requiredMethod(path: String): Symbol = internal.Context_requiredMethod(self)(path)
475475

476+
/** Get Java class name if we've accidentally tried to reflect on a Java class. None returned if TASTy class. */
477+
def getJavaClassname(): Option[String] = internal.Context_getJavaClassname(self)
478+
476479
}
477480

478481

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ trait CompilerInterface {
162162
/** Get method symbol if method is either defined in current compilation run or present on classpath. Throws if the method has an overload. */
163163
def Context_requiredMethod(self: Context)(path: String): Symbol
164164

165+
/** Get Java class name if we've accidentally tried to reflect on a Java class. None returned if TASTy class. */
166+
def Context_getJavaClassname(self: Context): Option[String]
167+
165168

166169
///////////////
167170
// REPORTING //
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import scala.tasty.Reflection
2+
import scala.tasty.inspector._
3+
4+
case class I8215(id: String)
5+
6+
object Test {
7+
def main(args: Array[String]): Unit = {
8+
val inspect1 = new TestInspector()
9+
inspect1.inspect("", List("I8215"))
10+
assert(inspect1.gotJava == None)
11+
12+
val inspect2 = new TestInspector()
13+
inspect2.inspect("", List("java.util.UUID"))
14+
assert(inspect2.gotJava == Some("java.util.UUID"))
15+
}
16+
}
17+
18+
class TestInspector() extends TastyInspector
19+
20+
var gotJava: Option[String] = None
21+
22+
protected def processCompilationUnit(reflect: Reflection)(root: reflect.Tree): Unit =
23+
import reflect.{given,_}
24+
gotJava = reflect.rootContext.getJavaClassname()

0 commit comments

Comments
 (0)