Skip to content

Fix #2556, make JUnit4 work in dotty compiled classes. #2557

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 3 commits into from
May 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/MixinOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ import Symbols._, Types._, Contexts._, SymDenotations._, DenotTransformers._, Fl
import util.Positions._
import SymUtils._
import StdNames._, NameOps._
import Decorators._

class MixinOps(cls: ClassSymbol, thisTransform: DenotTransformer)(implicit ctx: Context) {
import ast.tpd._

val superCls: Symbol = cls.superClass
val mixins: List[ClassSymbol] = cls.mixins

lazy val JUnit4Annotations: List[Symbol] = List("Test", "Ignore", "Before", "After", "BeforeClass", "AfterClass").
map(n => ctx.getClassIfDefined("org.junit." + n)).
filter(_.exists)

def implementation(member: TermSymbol): TermSymbol = {
val res = member.copy(
owner = cls,
Expand Down Expand Up @@ -59,10 +64,14 @@ class MixinOps(cls: ClassSymbol, thisTransform: DenotTransformer)(implicit ctx:
def needsDisambiguation = competingMethods.exists(x=> !(x is Deferred)) // multiple implementations are available
def hasNonInterfaceDefinition = competingMethods.exists(!_.owner.is(Trait)) // there is a definition originating from class
meth.is(Method, butNot = PrivateOrAccessorOrDeferred) &&
(meth.owner.is(Scala2x) || needsDisambiguation || hasNonInterfaceDefinition ) &&
(meth.owner.is(Scala2x) || needsDisambiguation || hasNonInterfaceDefinition || needsJUnit4Fix(meth) ) &&
isCurrent(meth)
}

private def needsJUnit4Fix(meth: Symbol): Boolean = {
meth.annotations.nonEmpty && JUnit4Annotations.exists(annot => meth.hasAnnotation(annot))
}

/** Get `sym` of the method that needs a forwarder
* Method needs a forwarder in those cases:
* - there is a trait that defines a primitive version of implemented polymorphic method.
Expand Down
15 changes: 15 additions & 0 deletions tests/run/junitForwarders/C_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
trait T {
@org.junit.Test def foo = 0
}

class C extends T

object Test extends App {
def check(c: Class[_], e: String) = {
val s = c.getDeclaredMethods.sortBy(_.getName).map(m => s"${m.getName} - ${m.getDeclaredAnnotations.mkString(", ")}").mkString(";")
assert(s == e, s"found: $s\nexpected: $e")
}
check(classOf[C], "foo - @org.junit.Test()")
// TODO scala-dev#213: should `foo$` really carry the @Test annotation?
check(classOf[T], "$init$ - ;foo - @org.junit.Test()")
}
10 changes: 10 additions & 0 deletions tests/run/junitForwarders/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.junit;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Test { }