Permalink
Browse files

Abstract over java.lang.reflect.{ Method, Constructor }.

No doubt there's some plausible explanation for this design:

  % java/lang/reflect/Method.java
  public Annotation[][] getParameterAnnotations()
  public Class<?>[] getExceptionTypes()
  public Class<?>[] getParameterTypes()
  public Type[] getGenericExceptionTypes()
  public Type[] getGenericParameterTypes()
  public boolean isVarArgs()

  % java/lang/reflect/Constructor.java
  public Annotation[][] getParameterAnnotations()
  public Class<?>[] getExceptionTypes()
  public Class<?>[] getParameterTypes()
  public Type[] getGenericExceptionTypes()
  public Type[] getGenericParameterTypes()
  public boolean isVarArgs()

Sun must have really been losing their grip there as the end
approached. This class fakes some of the missing common parent.
  • Loading branch information...
1 parent 14aaa70 commit 15bc39a844a8080715f559889ed7a8b6c375f176 @paulp paulp committed Apr 2, 2013
Showing with 45 additions and 0 deletions.
  1. +45 −0 src/reflect/scala/reflect/internal/JMethodOrConstructor.scala
@@ -0,0 +1,45 @@
+/* NSC -- new Scala compiler
+ * Copyright 2005-2013 LAMP/EPFL
+ * @author Paul Phillips
+ */
+package scala.reflect
+package internal
+
+import java.lang.{ Class => jClass }
+import java.lang.annotation.{ Annotation => jAnnotation }
+import java.lang.reflect.{
+ Member => jMember, Constructor => jConstructor, Method => jMethod,
+ AnnotatedElement => jAnnotatedElement, Type => jType,
+ TypeVariable => jTypeVariable
+}
+
+/** This class tries to abstract over some of the duplication
+ * in java.lang.reflect.{ Method, Constructor }.
+ */
+class JMethodOrConstructor(val member: jMember with jAnnotatedElement) {
+ def isVarArgs: Boolean = member match {
+ case m: jMethod => m.isVarArgs
+ case m: jConstructor[_] => m.isVarArgs
+ }
+ def typeParams: Array[_ <: jTypeVariable[_]] = member match {
+ case m: jMethod => m.getTypeParameters
+ case m: jConstructor[_] => m.getTypeParameters
+ }
+ def paramTypes: Array[jType] = member match {
+ case m: jMethod => m.getGenericParameterTypes
+ case m: jConstructor[_] => m.getGenericParameterTypes
+ }
+ def paramAnnotations: Array[Array[jAnnotation]] = member match {
+ case m: jMethod => m.getParameterAnnotations
+ case m: jConstructor[_] => m.getParameterAnnotations
+ }
+ def resultType: jType = member match {
+ case m: jMethod => m.getGenericReturnType
+ case m: jConstructor[_] => classOf[Unit]
+ }
+}
+
+object JMethodOrConstructor {
+ implicit def liftMethodToJmoc(m: jMethod): JMethodOrConstructor = new JMethodOrConstructor(m)
+ implicit def liftConstructorToJmoc(m: jConstructor[_]): JMethodOrConstructor = new JMethodOrConstructor(m)
+}

0 comments on commit 15bc39a

Please sign in to comment.