-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Can't get constructor of java defined class in reflect api #18694
Comments
@goshacodes could you provide the implementation of the macro? |
|
@nicolasstucki minimized example. Also I can try contribute this with a spree, want this fixed build.sbtlazy val root = (project in file("."))
.settings(
scalaVersion := "3.6.2",
name := "java_class",
scalacOptions ++= Seq(
"-explain",
"-Xcheck-macros"
)
) JavaClass.javapublic class JavaClass {
} main.scalaimport scala.annotation.experimental
@main
@experimental
def main(): Unit =
mock[JavaClass] Macro.scalaimport scala.annotation.experimental
import scala.quoted.{Expr, Quotes, Type}
@experimental
inline def mock[T]: T = ${mockMacro[T]}
@experimental
def mockMacro[T: Type](using quotes: Quotes): Expr[T] =
import quotes.reflect.*
val tree = TypeTree.of[T]
val parents =
List(
Select(
New(TypeIdent(tree.tpe.typeSymbol)),
tree.tpe.typeSymbol.primaryConstructor
).appliedToArgs(List('{()}.asTerm))
)
val sym = Symbol.newClass(
Symbol.spliceOwner,
"anon",
parents.map {
case term: Term => term.tpe
case tpt: TypeTree => tpt.tpe
},
decls = _ => Nil,
selfType = None
)
val cls = ClassDef(sym, parents, Nil)
Block(
List(cls),
Typed(Apply(Select(New(TypeIdent(sym)), sym.primaryConstructor), Nil), TypeTree.of[T])
).asExprOf[T]
Output[error] java.lang.AssertionError: assertion failed: private constructor JavaClass in class JavaClass in /Users/gosha/IdeaProjects/java_class/src/main/java/JavaClass.java accessed from constructor anon in class anon in /Users/gosha/IdeaProjects/java_class/src/main/scala/main.scala
[error] scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
[error] dotty.tools.dotc.transform.ExpandPrivate.ensurePrivateAccessible(ExpandPrivate.scala:88)
[error] dotty.tools.dotc.transform.ExpandPrivate.transformSelect(ExpandPrivate.scala:98)
[error] dotty.tools.dotc.transform.ExpandPrivate.transformSelect(ExpandPrivate.scala:97)
[error] dotty.tools.dotc.transform.MegaPhase.goSelect(MegaPhase.scala:636)
[error] dotty.tools.dotc.transform.MegaPhase.transformNamed$1(MegaPhase.scala:245)
[error] dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:452)
[error] dotty.tools.dotc.transform.MegaPhase.transformUnnamed$1(MegaPhase.scala:295)
[error] dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:454)
[error] dotty.tools.dotc.transform.MegaPhase.loop$2(MegaPhase.scala:471) |
@jchyb Also tagging you. Nicolas seems not active on github currently |
Yes, I recently took over some of Nicolas' responsibilities. Thank you for the minimization! I believe this might be getting fixed soon with #22104. I'll make sure it is in 3.6.4 (should be back portable to LTS). |
Compiler version
3.3.0
Description
I'm trying to extend a java class using reflect api.
To create a symbol for a class I need parent, either TypeTree or Term. TypeTree is not working here, so I'am trying to get a constructor and apply nulls as arguments
Minimized code
When I'm trying to use
tpe.typeSymbol.primaryConstructor
compiler crushes with this:So instead of trying to use primary constructor, I am trying to use another public one and I got this one.
After it I am trying to pass there a () as argument and it gives me another error:
The text was updated successfully, but these errors were encountered: