Compile Error:when the Annotation param type is user defined Enumeration, param can't be set when used

The java annotation file: DrWeave.java

package test.weaver;

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

@Target({java.lang.annotation.ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface DrWeave
{
  public abstract MatchType type() default MatchType.ExactClass;

  public abstract String originalName() default "";
}

the user defined type:

package test.weaver;

public enum MatchType
{
    ExactClass(true),
    BaseClass(false),
    Interface(false);

    private final boolean exactMatch;

    private MatchType(final boolean exact) {
        this.exactMatch = exact;
    }

    public boolean isExactMatch() {
        return this.exactMatch;
    }
}

if i use the annotation like fllowed, it works all right:

package play.api.mvc

import test.weaver.MatchType;
import test.weaver.BrWeave;

import scala.concurrent.Future;

@DrWeave(originalName = "aaa")
class Action[A] {
  def apply(request: Request[A]): Future[SimpleResult] = {
  }
}

but if i use like this, it lead to compile failure:

package play.api.mvc

import test.weaver.MatchType;
import test.weaver.BrWeave;

import scala.concurrent.Future;

@DrWeave(type = MatchType.Interface)
class Action[A] {
  def apply(request: Request[A]): Future[SimpleResult] = {
  }
}

the error message like this:

[ERROR] ... Action.scala:8: error:error: illegal start of simple expression
[ERROR]   @DrWeave(type = MatchType.Interface)
[ERROR]            ^                       (point to 't')
[ERROR] ... Action.scala:13: error: ')' expected but eof found.

my environment infomation:

Maven:
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
Maven home: /opt/apache-maven-3.3.9
Java version: 1.7.0_171, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.171-2.6.13.0.el7_4.x86_64/jre

Compile plugin:

<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.3.2</version>

Dependencies:

  <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <scala.version>2.10.3</scala.version>
      <scala.compat.version>2.10</scala.compat.version>
  </properties>

     <dependency>
          <groupId>com.typesafe.play</groupId>
          <artifactId>play_2.10</artifactId>
          <version>2.2.2</version>
      </dependency>

      <dependency>
          <groupId>org.scala-lang</groupId>
          <artifactId>scala-library</artifactId>
          <version>${scala.version}</version>
      </dependency>

do anyone used to face to similar probolem?

type is a keyword in scala and therefore cannot be used directly as an identifier. You can escape it with backticks:

@DrWeave(`type` = MatchType.Interface)
class Action[A] {
  def apply(request: Request[A]): Future[SimpleResult] = {
  }
}

1 Like

Thank you very much!