/*
 * Copyright 2004-2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.compass.core.accessor;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;

import org.compass.core.util.JdkVersion;
import org.compass.core.util.MethodInvoker;

/**
 * @author kimchy
 */
public class AccessorUtils {

    /**
     * Returns the Java 5 generics collection parameter. Returns null
     * if working with Java 1.4 or the collection has no generics parameter.
     */
    public static Class getCollectionParameter(Getter getter) {
        if (JdkVersion.getMajorJavaVersion() <= JdkVersion.JAVA_14) {
            return null;
        }
        if (!Collection.class.isAssignableFrom(getter.getReturnType())) {
            return null;
        }
        try {
            Object type = null;
            if (getter instanceof DirectPropertyAccessor.DirectGetter) {
                Field field = ((DirectPropertyAccessor.DirectGetter) getter).getField();
                MethodInvoker methodInvoker = new MethodInvoker();
                methodInvoker.setTargetMethod("getGenericType");
                methodInvoker.setTargetObject(field);
                type = methodInvoker.prepare().invoke();
            } else if (getter instanceof BasicPropertyAccessor.BasicGetter) {
                Method method = ((BasicPropertyAccessor.BasicGetter) getter).getMethod();
                MethodInvoker methodInvoker = new MethodInvoker();
                methodInvoker.setTargetMethod("getGenericReturnType");
                methodInvoker.setTargetObject(method);
                type = methodInvoker.prepare().invoke();
            }
            MethodInvoker methodInvoker = new MethodInvoker();
            methodInvoker.setTargetObject(type);
            methodInvoker.setTargetMethod("getActualTypeArguments");
            Object[] actualTypeArguments = (Object[]) methodInvoker.prepare().invoke();
            if (actualTypeArguments != null && actualTypeArguments.length == 1) {
                return (Class) actualTypeArguments[0];
            }
        } catch (Exception e) {
            return null;
        }
        return null;
    }
}

@@ -732,109 +732,98 @@
 ava.
-util.Collection;%0A%0Aimport org.compass.core.util.JdkVersion;%0Aimport org.compass.core.util.MethodInvoker
+lang.reflect.ParameterizedType;%0Aimport java.lang.reflect.Type;%0Aimport java.util.Collection
 ;%0A%0A/
@@ -1108,113 +1108,8 @@
 ) %7B%0A
-        if (JdkVersion.getMajorJavaVersion() %3C= JdkVersion.JAVA_14) %7B%0A            return null;%0A        %7D%0A
     
@@ -1225,32 +1225,12 @@
     
-try %7B%0A            Object
+Type
  typ
@@ -1235,28 +1235,24 @@
 ype = null;%0A
-    
         if (
@@ -1320,20 +1320,16 @@
         
- 
-   
 Field fi
@@ -1409,243 +1409,39 @@
     
-    MethodInvoker methodInvoker = new MethodInvoker();%0A                methodInvoker.setTargetMethod(%22getGenericType%22);%0A                methodInvoker.setTargetObject(field);%0A                type = methodInvoker.prepare().invoke();%0A    
+type = field.getGenericType();%0A
     
@@ -1502,36 +1502,32 @@
 .BasicGetter) %7B%0A
-    
             Meth
@@ -1612,110 +1612,22 @@
     
-    MethodInvoker methodInvoker = new MethodInvoker();%0A                methodInvoker.setTargetMethod(%22
+type = method.
 getG
@@ -1642,17 +1642,17 @@
 turnType
-%22
+(
 );%0A     
@@ -1658,391 +1658,214 @@
     
-        methodInvoker.setTargetObject(method);%0A                type = methodInvoker.prepare().invoke();%0A            %7D%0A            MethodInvoker methodInvoker = new MethodInvoker();%0A            methodInvoker.setTargetObject(type);%0A            methodInvoker.setTargetMethod(%22getActualTypeArguments%22);%0A            Object%5B%5D actualTypeArguments = (Object%5B%5D) methodInvoker.prepare().invoke
+%7D%0A        if (type == null) %7B%0A            return null;%0A        %7D%0A        if (type instanceof ParameterizedType) %7B%0A            Object%5B%5D actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments
 ();%0A
@@ -2019,65 +2019,8 @@
   %7D%0A
-        %7D catch (Exception e) %7B%0A            return null;%0A
     

use direct generics since we moved to Java 5