Skip to content

Commit e7ce165

Browse files
authored
Parse native in Java bytecode as @Native (#16232)
2 parents 8948b09 + d9648b3 commit e7ce165

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ class ClassfileParser(
275275
def complete(denot: SymDenotation)(using Context): Unit = {
276276
val sym = denot.symbol
277277
val isEnum = (jflags & JAVA_ACC_ENUM) != 0
278+
val isNative = (jflags & JAVA_ACC_NATIVE) != 0
279+
val isTransient = (jflags & JAVA_ACC_TRANSIENT) != 0
280+
val isVolatile = (jflags & JAVA_ACC_VOLATILE) != 0
278281
val isConstructor = name eq nme.CONSTRUCTOR
279282

280283
/** Strip leading outer param from constructor and trailing access tag for
@@ -313,6 +316,12 @@ class ClassfileParser(
313316
val isVarargs = denot.is(Flags.Method) && (jflags & JAVA_ACC_VARARGS) != 0
314317
denot.info = sigToType(sig, isVarargs = isVarargs)
315318
if (isConstructor) normalizeConstructorParams()
319+
if isNative then
320+
attrCompleter.annotations ::= Annotation.deferredSymAndTree(defn.NativeAnnot)(New(defn.NativeAnnot.typeRef, Nil))
321+
if isTransient then
322+
attrCompleter.annotations ::= Annotation.deferredSymAndTree(defn.TransientAnnot)(New(defn.TransientAnnot.typeRef, Nil))
323+
if isVolatile then
324+
attrCompleter.annotations ::= Annotation.deferredSymAndTree(defn.VolatileAnnot)(New(defn.VolatileAnnot.typeRef, Nil))
316325
denot.info = translateTempPoly(attrCompleter.complete(denot.info, isVarargs))
317326
if (isConstructor) normalizeConstructorInfo()
318327

compiler/test/dotty/tools/AnnotationsTests.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,9 @@ class AnnotationsTest:
8989
s"A missing annotation while parsing a Java class should be silently ignored but: ${ctx.reporter.summary}")
9090
}
9191
}
92+
93+
@Test def hasNativeAnnot: Unit =
94+
inCompilerContext(TestConfiguration.basicClasspath) {
95+
val term: TermSymbol = requiredClass("java.lang.invoke.MethodHandle").requiredMethod("invokeExact")
96+
assert(term.hasAnnotation(defn.NativeAnnot), i"${term.annotations}")
97+
}

0 commit comments

Comments
 (0)