1

I have a piece of Java code as follows:

import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;


@BindingAnnotation
@Target({ FIELD, PARAMETER, METHOD })
@Retention(RUNTIME)
public @interface MessageCount {
}

This is not my code, but I am trying rewrite this in Scala. I do not understand the @interface construct very well. Nor do I understand how those annotations can be translated over to Scala. Or is it possible to use this in Scala somehow?

I prefer rewriting/porting this in Scala. The only thing I can think of is to write it as a Scala trait.

Any pointers, suggestions are appreciated.

1
  • It's not interface. It's java annotation. I'm not sure it may be correctly ported to scala. Commented Mar 23, 2015 at 7:42

1 Answer 1

1

No, currently annotations with @Retention(RUNTIME) can't be written in Scala.

Or is it possible to use this in Scala somehow?

Yes, you can use it in Scala. You can have both Scala and Java code in the same project (Java code normally goes to src/main/java, Scala code to src/main/scala), and they can even refer to each other.

Sign up to request clarification or add additional context in comments.

3 Comments

That is good news. Can you show me how this can be done in an example? Do I have to use the javaconversions package somehow? How can I use this in a Scala
No, you don't need to do anything special. Just use @MessageCount in your Scala code in the same way you would in Java. Except you may need to specify whether you want to annotate a field or a method, see scala-lang.org/api/2.11.5/….
How can i use @Retention (Runtime) then?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.