Translate Java class with static attributes and annotation to Scala equivalent

I am currently trying to "translate" the following Java class into an equivalent Scala class. This is part of a JavaEE6 application, and I need it to use MetaModel JPA2.

import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.StaticMetamodel;

@StaticMetamodel(Person.class)
public class Person_ {
  public static volatile SingularAttribute<Person, String> name;
}

Parsing the compiled class file shows the following information for the compiled file:

> javap Person_.class :
public class model.Person_ extends java.lang.Object{
  public static volatile javax.persistence.metamodel.SingularAttribute name;
  public model.Person_();
} 

So now I need an equivalent Scala file that has the same structure as JPA, so it resolves attributes by reflection to make them available at runtime. So the main problem, I think, is that the attribute is static, but the annotation should be on the object (Java) (I think) My first naive attempt to create the Scala equivalent is this:

@StaticMetamodel(classOf[Person])
class Person_

object Person_ {
  @volatile var name:SingularAttribute[Person, String] = _;
}

Java, . , . "Person_.firstname", null, , JPA ( Java org.hibernate.ejb.metamodel.SingularAttributeImpl ).

> javap Person_.class :
public class model.Person_ extends java.lang.Object implements scala.ScalaObject{
  public static final void name_$eq(javax.persistence.metamodel.SingularAttribute);
  public static final javax.persistence.metamodel.SingularAttribute name();
  public model.Person_();
}

> javap Person_$.class :
public final class model.Person__$ extends java.lang.Object implements scala.ScalaObject
  public static final model.Person__$ MODULE$;
  public static {};
  public javax.persistence.metamodel.SingularAttribute name();
  public void name_$eq(javax.persistence.metamodel.SingularAttribute);
}

, , Scala Java? , , , , - ( Java, , Scala, )

, -? !

+3
1

, Scala - , ( MODULE $). , .

AFAIK Scala , Person. JPA Scala, . , Scala .

JPA Scala, , .

+1

Source: https://habr.com/ru/post/1749814/


All Articles