0

i know java Constructor convert into Scala code. but this is my project Generic class implementation. i am using hibernate ,spring in this project and i create genericDAO trait but can't create It's implementation or i can't convert this java constructor to scala

This are 2 variables

private Class<T> entityClass;

private String entityClassName;

This is GenericDAOImpl class code here GenericDAOImpl is the constructor

@SuppressWarnings("unchecked")
        public GenericDAOImpl() {
            Type genericSuperclass;
            Class<?> parametrizedClass = getClass();
            do {
                genericSuperclass = parametrizedClass.getGenericSuperclass();
                if (genericSuperclass instanceof Class) {
                    parametrizedClass = (Class<?>) genericSuperclass;
                }
            } while (genericSuperclass != null
                    && !(genericSuperclass instanceof ParameterizedType));
            this.entityClass = (Class<T>) ((ParameterizedType) genericSuperclass)
                    .getActualTypeArguments()[0];
            if (entityClass != null) {
                entityClassName = entityClass.getSimpleName();
            }
        }

Thanks From Milano

EDIT

I tried this

@SuppressWarnings("unchecked")
 def this(T,ID){
   var genericSuperclass:Type;
   var parametrizedClass:Class[?]=getClass
   do {
            genericSuperclass = parametrizedClass.getGenericSuperclass()
            if (genericSuperclass instanceof[Class]) {
                parametrizedClass = (Class<?>) genericSuperclass
            }
        } while (genericSuperclass != null
                && !(genericSuperclass instanceof [ParameterizedType]))
        this.entityClass = (Class[T]) ((ParameterizedType) genericSuperclass)
                .getActualTypeArguments()[0]
        if (entityClass != null) {
            entityClassName = entityClass.getSimpleName()
        }

 }


 And this got compilation Error 
16
  • 1
    What have you got/tried so far? Commented Jul 8, 2013 at 9:33
  • 1
    @milano: you should study scala a little before dealing with reflection or other complicated things. Your "tried" code is very far from valid scala code. Commented Jul 8, 2013 at 9:49
  • 1
    @milano: s\Class[?]\Class[_]\ , s\instanceof[Class]\isInstanceOf[Class]\ , s\(Class<?>) genericSuperclass\genericSuperclass.asInstanceOf[Class[_]]\ , s\[0]\(0)\ , remove def this(T,ID){ (class body is construvtor), there is no do{...} while in scala, you should initialize even vars. Commented Jul 8, 2013 at 10:01
  • 1
    @milano: class GenericDAOImpl[T, ID] Commented Jul 8, 2013 at 10:28
  • 2
    @milano - try and work it out for yourself. Look things up, think about it. At the moment senia is just rewriting your code and you're not learning anything. Commented Jul 8, 2013 at 10:38

1 Answer 1

1

I can see some problems with the Java to Scala conversion. Specifically I would replace Java's instanceof with

isInstanceOf[SomeClass]

Casts change from

(Class[T])

to

.asInstanceOf[Class[T]]

Add the class bound ? changes to _ (I think)

Array indexes change from square brackets to round brackets e.g.

.getActualTypeArguments[0]

to

.getActualTypeArguments(0)

Applying these changes to the original Java I get

class GenericDAOImpl[T] {
var genericSuperclass: Type = null
var parametrizedClass: Class[_] = getClass()
do {
    genericSuperclass = parametrizedClass.getGenericSuperclass()
    if (genericSuperclass.isInstanceOf[Class])
        parametrizedClass = genericSuperclass.asInstanceOf[Class[_]]
} while (genericSuperclass != null
                && !(genericSuperclass.isInstanceOf[ParameterizedType]))
val entityClass = genericSuperclass.asInstanceOf[ParameterizedType]
                .getActualTypeArguments()(0).asInstanceOf[Class[T]]
val entityClassName =
  if (entityClass != null) 
      entityClass.getSimpleName()
  else
      null

but ... it doesn't make sense to me, I would need to see more of your original class to get it to work.

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

Comments

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.