/*
 * Copyright (C) 2013 salesforce.com, inc.
 *
 * 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.auraframework.impl.java.type;

import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Set;

import org.auraframework.Aura;
import org.auraframework.def.DefDescriptor;
import org.auraframework.def.TypeDef;
import org.auraframework.impl.system.DefDescriptorImpl;
import org.auraframework.impl.system.DefinitionImpl;
import org.auraframework.instance.BaseComponent;
import org.auraframework.service.ConverterService;
import org.auraframework.throwable.quickfix.QuickFixException;
import org.auraframework.util.json.Json;

/**
 */
public class JavaTypeDef extends DefinitionImpl<TypeDef> implements TypeDef {

    private static final long serialVersionUID = -1650260598462052988L;
    private final Class<?> clazz;
    private final String simpleParamName;

    protected JavaTypeDef(Builder builder) {
        super(builder);
        this.clazz = builder.typeClass;

        simpleParamName = makeSimpleParamName();
    }

    private String makeSimpleParamName() {
        if (descriptor.getNameParameters() == null) {
            return null;
        }

        // remove angle brackets and namespace qualifiers
        String tempParamName = descriptor.getNameParameters().replaceAll("[<>]", "");
        int pos = tempParamName.lastIndexOf('.');
        if (pos != -1) {
            tempParamName = tempParamName.substring(pos + 1);
        }
        return tempParamName;
    }

    private boolean hasCollectionConverters() {
        if (!descriptor.isParameterized() || simpleParamName == null) {
            return false;
        }

        return Aura.getConverterService().hasConverter(ArrayList.class, clazz, simpleParamName);

    }

    @Override
    public void serialize(Json json) throws IOException {
        json.writeString(getName());
    }

    /**
     * @return The Java class wrapped by this Aura TypeDef
     * @throws QuickFixException
     */
    @Override
    public Object getExternalType(String prefix) throws QuickFixException {
        prefix = prefix.toLowerCase();
        if (!prefix.equals("java")) {
            TypeDef typeDef = DefDescriptorImpl.getAssociateDescriptor(descriptor, TypeDef.class, prefix).getDef();
            if (typeDef != null) {
                return typeDef.getExternalType(prefix);
            }
        }
        return clazz;
    }

    @Override
    public String toString() {
        return descriptor.getQualifiedName();
    }

    @Override
    public Object valueOf(Object value) {
        ConverterService converterService = Aura.getConverterService();
        if (hasCollectionConverters()) {
            return converterService.convert(value, clazz, simpleParamName, false);
        }
        return converterService.convert(value, clazz, null, false, true);
    }

    @Override
    public Object wrap(Object o) {
        return new JavaValueProvider(o);
    }

    @Override
    public Object initialize(Object config, BaseComponent<?, ?> valueProvider) {
        if (config != null && config instanceof String && !clazz.isInstance(config)) {
            // TODO: KRIS WE CAN'T PASS NULL HERE. THATS MESSED UP AND WILL FAIL.
            return valueOf(config);
        }
        return config;
    }

    /**
     * Get the underlying class for a type, or null if the type is a variable
     * type. Code in part from Ian Roberton's June 23, 2007 blog post :
     * http://www.artima.com/weblogs/viewpost.jsp?thread=208860
     * 
     * @param type the type
     * @return the underlying class
     */
    public static Class<?> getClass(Type type) {
        if (type instanceof Class) {
            return (Class<?>) type;
        } else if (type instanceof ParameterizedType) {
            return getClass(((ParameterizedType) type).getRawType());
        } else if (type instanceof GenericArrayType) {
            Type componentType = ((GenericArrayType) type).getGenericComponentType();
            Class<?> componentClass = getClass(componentType);
            if (componentClass != null) {
                return Array.newInstance(componentClass, 0).getClass();
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    public static class Builder extends DefinitionImpl.BuilderImpl<TypeDef> {

        public Builder() {
            super(TypeDef.class);
        }

        private Class<?> typeClass;

        public Builder setTypeClass(Class<?> c) {
            this.typeClass = c;
            return this;
        }

        @Override
        public JavaTypeDef build() {
            return new JavaTypeDef(this);
        }
    }
}

@@ -857,30 +857,8 @@
 ist;
-%0Aimport java.util.Set;
 %0A%0Aim
@@ -919,115 +919,15 @@
 def.
-DefDescriptor;%0Aimport org.auraframework.def.TypeDef;%0Aimport org.auraframework.impl.system.DefDescriptorImpl
+TypeDef
 ;%0Aim
@@ -1081,71 +1081,8 @@
 ce;%0A
-import org.auraframework.throwable.quickfix.QuickFixException;%0A
 impo
@@ -1986,270 +1986,8 @@
  %7D%0A%0A
-    private boolean hasCollectionConverters() %7B%0A        if (!descriptor.isParameterized() %7C%7C simpleParamName == null) %7B%0A            return false;%0A        %7D%0A%0A        return Aura.getConverterService().hasConverter(ArrayList.class, clazz, simpleParamName);%0A%0A    %7D%0A%0A
     
@@ -2175,34 +2175,36 @@
    *
- @throws QuickFixException
+%0A     * @param prefix unused
 %0A   
@@ -2275,332 +2275,143 @@
 ix) 
-throws QuickFixException %7B%0A        prefix = prefix.toLowerCase();%0A        if (!prefix.equals(%22java%22)) %7B%0A            TypeDef typeDef = DefDescriptorImpl.getAssociateDescriptor(descriptor, TypeDef.class, prefix).getDef();%0A            if (typeDef != null) %7B%0A                return typeDef.getExternalType(prefix);%0A            %7D
+%7B%0A        if (!%22java%22.equalsIgnoreCase(prefix)) %7B%0A            throw new UnsupportedOperationException(%22We only support java wrappers%22);
 %0A   
@@ -2688,32 +2688,152 @@
 if (
-hasCollectionConverters(
+descriptor.isParameterized() && simpleParamName != null%0A                && converterService.hasConverter(ArrayList.class, clazz, simpleParamName
 )) %7B

Remove useless code from JavaTypeDef. (#2764)