/**************************************************************************************
 * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved.                 *
 * http://aspectwerkz.codehaus.org                                                    *
 * ---------------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the LGPL license      *
 * a copy of which has been included with this distribution in the license.txt file.  *
 **************************************************************************************/
package org.codehaus.aspectwerkz.transform.inlining;

import org.codehaus.aspectwerkz.ContextClassLoader;
import org.codehaus.aspectwerkz.exception.WrappedRuntimeException;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.CodeVisitor;
import org.objectweb.asm.Constants;
import org.objectweb.asm.Type;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

/**
 * Helper class with utility methods for the ASM library.
 * 
 * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
 * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur </a>
 */
public class AsmHelper implements Constants, TransformationConstants {

    /**
     * Creates a constructor descriptor. <p/>Parts of code in this method is taken from the ASM codebase.
     * 
     * @param constructor
     * @return the descriptor
     */
    public static String getConstructorDescriptor(final Constructor constructor) {
        Class[] parameters = constructor.getParameterTypes();
        StringBuffer buf = new StringBuffer();
        buf.append('(');
        for (int i = 0; i < parameters.length; ++i) {
            Class d = parameters[i];
            while (true) {
                if (d.isPrimitive()) {
                    char car;
                    if (d == Integer.TYPE) {
                        car = 'I';
                    } else if (d == Void.TYPE) {
                        car = 'V';
                    } else if (d == Boolean.TYPE) {
                        car = 'Z';
                    } else if (d == Byte.TYPE) {
                        car = 'B';
                    } else if (d == Character.TYPE) {
                        car = 'C';
                    } else if (d == Short.TYPE) {
                        car = 'S';
                    } else if (d == Double.TYPE) {
                        car = 'D';
                    } else if (d == Float.TYPE) {
                        car = 'F';
                    } else /* if (d == Long.TYPE) */{
                        car = 'J';
                    }
                    buf.append(car);
                    break;
                } else if (d.isArray()) {
                    buf.append('[');
                    d = d.getComponentType();
                } else {
                    buf.append('L');
                    String name = d.getName();
                    int len = name.length();
                    for (int i1 = 0; i1 < len; ++i1) {
                        char car = name.charAt(i1);
                        buf.append((car == '.') ? '/' : car);
                    }
                    buf.append(';');
                    break;
                }
            }
        }
        buf.append(")V");
        return buf.toString();
    }

    /**
     * Creates a method descriptor. <p/>Parts of code in this method is taken from the ASM codebase.
     * 
     * @param method
     * @return the descriptor
     */
    public static String getMethodDescriptor(final Method method) {
        Class[] parameters = method.getParameterTypes();
        StringBuffer buf = new StringBuffer();
        buf.append('(');
        for (int i = 0; i < parameters.length; ++i) {
            Class d = parameters[i];
            while (true) {
                if (d.isPrimitive()) {
                    char car;
                    if (d == Integer.TYPE) {
                        car = 'I';
                    } else if (d == Void.TYPE) {
                        car = 'V';
                    } else if (d == Boolean.TYPE) {
                        car = 'Z';
                    } else if (d == Byte.TYPE) {
                        car = 'B';
                    } else if (d == Character.TYPE) {
                        car = 'C';
                    } else if (d == Short.TYPE) {
                        car = 'S';
                    } else if (d == Double.TYPE) {
                        car = 'D';
                    } else if (d == Float.TYPE) {
                        car = 'F';
                    } else /* if (d == Long.TYPE) */{
                        car = 'J';
                    }
                    buf.append(car);
                    break;
                } else if (d.isArray()) {
                    buf.append('[');
                    d = d.getComponentType();
                } else {
                    buf.append('L');
                    String name = d.getName();
                    int len = name.length();
                    for (int i1 = 0; i1 < len; ++i1) {
                        char car = name.charAt(i1);
                        buf.append((car == '.') ? '/' : car);
                    }
                    buf.append(';');
                    break;
                }
            }
        }
        buf.append(")");
        //FIXME handles return type
        return buf.toString();
    }

    /**
     * Gets the argument types for a constructor. <p/>Parts of code in this method is taken from the ASM codebase.
     * 
     * @param constructor
     * @return the argument types for the constructor
     */
    public static Type[] getArgumentTypes(final Constructor constructor) {
        Class[] classes = constructor.getParameterTypes();
        Type[] types = new Type[classes.length];
        for (int i = classes.length - 1; i >= 0; --i) {
            types[i] = Type.getType(classes[i]);
        }
        return types;
    }

    /**
     * Dumps an ASM class to disk.
     * 
     * @param dumpDir
     * @param className
     * @param cw
     * @throws java.io.IOException
     */
    public static void dumpClass(final String dumpDir, final String className, final ClassWriter cw) throws IOException {
        File dir = new File(dumpDir + File.separator + className.substring(0, className.lastIndexOf('/')));
        dir.mkdirs();
        String fileName = dumpDir + File.separator + className + ".class";
        System.out.println("AW INFO: dumping class " + className + " to " + dumpDir);
        FileOutputStream os = new FileOutputStream(fileName);
        os.write(cw.toByteArray());
        os.close();
    }

    /**
     * Adds a class to a class loader and loads it.
     * 
     * @param loader the class loader (if null the context class loader will be used)
     * @param bytes the bytes for the class
     * @param name the name of the class
     * @return the class
     */
    public static Class loadClass(ClassLoader loader, final byte[] bytes, final String name) {
        String className = name.replace('/', '.');
        try {
            if (loader == null) {
                loader = ContextClassLoader.getLoader();
            }
            Class klass = loader.loadClass(CLASS_LOADER_REFLECT_CLASS_NAME);
            Method method = klass.getDeclaredMethod(DEFINE_CLASS_METHOD_NAME, new Class[] {
                String.class, byte[].class, int.class, int.class
            });

            // TODO: what if we don't have rights to set this method to
            // accessible on this specific CL? Load it in System CL?
            method.setAccessible(true);
            Object[] args = new Object[] {
                className, bytes, new Integer(0), new Integer(bytes.length)
            };
            Class clazz = (Class) method.invoke(loader, args);
            method.setAccessible(false);
            return clazz;
        } catch (InvocationTargetException e) {
            // JIT failovering for Thread concurrency
            // AW-222 (Tomcat and WLS were reported for AW-222)
            if (e.getTargetException() instanceof LinkageError) {
                Class failoverJoinpointClass = loadClass(loader, className);
                if (failoverJoinpointClass != null) {
                    return failoverJoinpointClass;
                }
            }
            throw new WrappedRuntimeException(e);
        } catch (Exception e) {
            throw new WrappedRuntimeException(e);
        }
    }

    /**
     * Tries to load a class if unsuccessful returns null.
     * 
     * @param loader the class loader
     * @param name the name of the class
     * @return the class
     */
    public static Class loadClass(ClassLoader loader, final String name) {
        String className = name.replace('/', '.');
        try {
            if (loader == null) {
                loader = ContextClassLoader.getLoader();
            }
            return loader.loadClass(className);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Calculates the method hash. The computation MUST be the same as in ReflectHelper, thus we switch back the names
     * to Java style. Note that for array type, Java.reflect is using "[Lpack.foo;" style unless primitive.
     * 
     * @param name
     * @param desc
     * @return
     */
    public static int calculateMethodHash(final String name, final String desc) {
        int hash = 17;
        hash = (37 * hash) + name.replace('/', '.').hashCode();
        Type[] argumentTypes = Type.getArgumentTypes(desc);
        for (int i = 0; i < argumentTypes.length; i++) {
            hash = (37 * hash)
                + AsmHelper.convertAsmTypeDescriptorToReflect(argumentTypes[i].getDescriptor()).hashCode();
        }
        return hash;
    }

    /**
     * Calculates the constructor hash.
     * 
     * @param name
     * @param desc
     * @return
     */
    public static int calculateConstructorHash(final String desc) {
        return calculateMethodHash("<init>", desc);
    }

    /**
     * Calculates the field hash.
     * 
     * @param name
     * @param desc
     * @return
     */
    public static int calculateFieldHash(final String name, final String desc) {
        int hash = 17;
        hash = (37 * hash) + name.hashCode();
        Type type = Type.getType(desc);
        hash = (37 * hash) + AsmHelper.convertAsmTypeDescriptorToReflect(type.getDescriptor()).hashCode();
        return hash;
    }

    /**
     * Calculates the class hash.
     * 
     * @param declaringType
     * @return
     */
    public static int calculateClassHash(final String declaringType) {
        return AsmHelper.convertAsmTypeDescriptorToReflect(declaringType).hashCode();
    }

    /**
     * Converts an internal Java array type name ([Lblabla) to the a the format used by the expression matcher
     * (blabla[])
     * 
     * @param typeName is type name
     * @return
     */
    public static String convertArrayTypeName(final String typeName) {
        int index = typeName.lastIndexOf('[');
        if (index != -1) {
            StringBuffer arrayType = new StringBuffer();
            if (typeName.endsWith("I")) {
                arrayType.append("int");
            } else if (typeName.endsWith("J")) {
                arrayType.append("long");
            } else if (typeName.endsWith("S")) {
                arrayType.append("short");
            } else if (typeName.endsWith("F")) {
                arrayType.append("float");
            } else if (typeName.endsWith("D")) {
                arrayType.append("double");
            } else if (typeName.endsWith("Z")) {
                arrayType.append("boolean");
            } else if (typeName.endsWith("C")) {
                arrayType.append("char");
            } else if (typeName.endsWith("B")) {
                arrayType.append("byte");
            } else {
                arrayType.append(typeName.substring(index + 2, typeName.length() - 1));
            }
            for (int i = 0; i < (index + 1); i++) {
                arrayType.append("[]");
            }
            return arrayType.toString();
        } else {
            return typeName;
        }
    }

    /**
     * Converts an ASM type descriptor" (I, [I, [Ljava/lang/String;, Ljava/lang/String;) to a Java.reflect one (int, [I,
     * [Ljava.lang.String;, java.lang.String)
     * 
     * @param typeDesc
     * @return the Java.reflect string representation
     */
    public static String convertAsmTypeDescriptorToReflect(final String typeDesc) {
        String result = null;
        // change needed for array types only
        if (typeDesc.startsWith("[")) {
            result = typeDesc;
        } else {
            // support for single dimension type
            if (typeDesc.startsWith("L") && typeDesc.endsWith(";")) {
                result = typeDesc.substring(1, typeDesc.length() - 1);
            } else {
                // primitive type, single dimension
                if (typeDesc.equals("I")) {
                    result = "int";
                } else if (typeDesc.equals("J")) {
                    result = "long";
                } else if (typeDesc.equals("S")) {
                    result = "short";
                } else if (typeDesc.equals("F")) {
                    result = "float";
                } else if (typeDesc.equals("D")) {
                    result = "double";
                } else if (typeDesc.equals("Z")) {
                    result = "boolean";
                } else if (typeDesc.equals("C")) {
                    result = "char";
                } else if (typeDesc.equals("B")) {
                    result = "byte";
                } else {
                    throw new RuntimeException("unhandled ASM type " + typeDesc);
                }
            }
        }
        return result.replace('/', '.');
    }

    /**
     * Adds the correct return statement.
     * 
     * @param mv
     * @param type
     */
    public static void addReturnStatement(final CodeVisitor mv, final Type type) {
        switch (type.getSort()) {
            case Type.VOID:
                mv.visitInsn(RETURN);
                break;
            case Type.LONG:
                mv.visitInsn(LRETURN);
                break;
            case Type.INT:
                mv.visitInsn(IRETURN);
                break;
            case Type.SHORT:
                mv.visitInsn(IRETURN);
                break;
            case Type.DOUBLE:
                mv.visitInsn(DRETURN);
                break;
            case Type.FLOAT:
                mv.visitInsn(FRETURN);
                break;
            case Type.BYTE:
                mv.visitInsn(IRETURN);
                break;
            case Type.BOOLEAN:
                mv.visitInsn(IRETURN);
                break;
            case Type.CHAR:
                mv.visitInsn(IRETURN);
                break;
            case Type.ARRAY:
                mv.visitInsn(ARETURN);
                break;
            case Type.OBJECT:
                mv.visitInsn(ARETURN);
                break;
        }
    }

    /**
     * Loads argument types.
     * 
     * @param mv
     * @param argumentTypes
     */
    public static void loadArgumentTypes(final CodeVisitor mv, final Type[] argumentTypes, final boolean staticMethod) {
        int opcode;
        if (staticMethod) {
            opcode = 0;
        } else {
            opcode = 1;
        }
        for (int i = 0; i < argumentTypes.length; i++) {
            opcode = loadType(mv, opcode, argumentTypes[i]);
        }
    }

    /**
     * Loads a type.
     * 
     * @param cv
     * @param opcode
     * @param type
     * @return the incremented opcode
     */
    public static int loadType(final CodeVisitor cv, int opcode, final Type type) {
        switch (type.getSort()) {
            case Type.LONG:
                cv.visitVarInsn(LLOAD, opcode++);
                opcode++;
                break;
            case Type.INT:
                cv.visitVarInsn(ILOAD, opcode++);
                break;
            case Type.SHORT:
                cv.visitVarInsn(ILOAD, opcode++);
                break;
            case Type.DOUBLE:
                cv.visitVarInsn(DLOAD, opcode++);
                opcode++;
                break;
            case Type.FLOAT:
                cv.visitVarInsn(FLOAD, opcode++);
                break;
            case Type.BYTE:
                cv.visitVarInsn(ILOAD, opcode++);
                break;
            case Type.BOOLEAN:
                cv.visitVarInsn(ILOAD, opcode++);
                break;
            case Type.CHAR:
                cv.visitVarInsn(ILOAD, opcode++);
                break;
            case Type.ARRAY:
                cv.visitVarInsn(ALOAD, opcode++);
                break;
            case Type.OBJECT:
                cv.visitVarInsn(ALOAD, opcode++);
                break;
        }
        return opcode;
    }

    /**
     * Stores a type.
     * 
     * @param cv
     * @param opcode
     * @param type
     * @return the incremented opcode
     */
    public static int storeType(final CodeVisitor cv, int opcode, final Type type) {
        switch (type.getSort()) {
            case Type.VOID:
                break;
            case Type.LONG:
                cv.visitVarInsn(LSTORE, opcode++);
                opcode++;
                break;
            case Type.INT:
                cv.visitVarInsn(ISTORE, opcode++);
                break;
            case Type.SHORT:
                cv.visitVarInsn(ISTORE, opcode++);
                break;
            case Type.DOUBLE:
                cv.visitVarInsn(DSTORE, opcode++);
                opcode++;
                break;
            case Type.FLOAT:
                cv.visitVarInsn(FSTORE, opcode++);
                break;
            case Type.BYTE:
                cv.visitVarInsn(ISTORE, opcode++);
                break;
            case Type.BOOLEAN:
                cv.visitVarInsn(ISTORE, opcode++);
                break;
            case Type.CHAR:
                cv.visitVarInsn(ISTORE, opcode++);
                break;
            case Type.ARRAY:
                cv.visitVarInsn(ASTORE, opcode++);
                break;
            case Type.OBJECT:
                cv.visitVarInsn(ASTORE, opcode++);
                break;
        }
        return opcode;
    }

    /**
     * Creates and adds the correct parameter index.
     * 
     * @param cv
     * @param index
     */
    public static void loadConstant(final CodeVisitor cv, final int index) {
        switch (index) {
            case 0:
                cv.visitInsn(ICONST_0);
                break;
            case 1:
                cv.visitInsn(ICONST_1);
                break;
            case 2:
                cv.visitInsn(ICONST_2);
                break;
            case 3:
                cv.visitInsn(ICONST_3);
                break;
            case 4:
                cv.visitInsn(ICONST_4);
                break;
            case 5:
                cv.visitInsn(ICONST_5);
                break;
            default:
                cv.visitIntInsn(LDC, index);
                break;
        }
    }

    /**
     * Prepares the wrapping or a primitive type.
     * 
     * @param cv
     * @param type
     */
    public static void prepareWrappingOfPrimitiveType(final CodeVisitor cv, final Type type) {
        switch (type.getSort()) {
            case Type.SHORT:
                cv.visitTypeInsn(NEW, SHORT_CLASS_NAME);
                cv.visitInsn(DUP);
                break;
            case Type.INT:
                cv.visitTypeInsn(NEW, INTEGER_CLASS_NAME);
                cv.visitInsn(DUP);
                break;
            case Type.LONG:
                cv.visitTypeInsn(NEW, LONG_CLASS_NAME);
                cv.visitInsn(DUP);
                break;
            case Type.FLOAT:
                cv.visitTypeInsn(NEW, FLOAT_CLASS_NAME);
                cv.visitInsn(DUP);
                break;
            case Type.DOUBLE:
                cv.visitTypeInsn(NEW, DOUBLE_CLASS_NAME);
                cv.visitInsn(DUP);
                break;
            case Type.BYTE:
                cv.visitTypeInsn(NEW, BYTE_CLASS_NAME);
                cv.visitInsn(DUP);
                break;
            case Type.BOOLEAN:
                cv.visitTypeInsn(NEW, BOOLEAN_CLASS_NAME);
                cv.visitInsn(DUP);
                break;
            case Type.CHAR:
                cv.visitTypeInsn(NEW, CHARACTER_CLASS_NAME);
                cv.visitInsn(DUP);
                break;
        }
    }

    /**
     * Handles the wrapping of a primitive type.
     * 
     * @param cv
     * @param type
     */
    public static void wrapPrimitiveType(final CodeVisitor cv, final Type type) {
        switch (type.getSort()) {
            case Type.VOID:
                cv.visitInsn(ACONST_NULL);
                break;
            case Type.SHORT:
                cv
                        .visitMethodInsn(
                            INVOKESPECIAL,
                            SHORT_CLASS_NAME,
                            INIT_METHOD_NAME,
                            SHORT_CLASS_INIT_METHOD_SIGNATURE);
                break;
            case Type.INT:
                cv.visitMethodInsn(
                    INVOKESPECIAL,
                    INTEGER_CLASS_NAME,
                    INIT_METHOD_NAME,
                    INTEGER_CLASS_INIT_METHOD_SIGNATURE);
                break;
            case Type.LONG:
                cv.visitMethodInsn(INVOKESPECIAL, LONG_CLASS_NAME, INIT_METHOD_NAME, LONG_CLASS_INIT_METHOD_SIGNATURE);
                break;
            case Type.FLOAT:
                cv
                        .visitMethodInsn(
                            INVOKESPECIAL,
                            FLOAT_CLASS_NAME,
                            INIT_METHOD_NAME,
                            FLOAT_CLASS_INIT_METHOD_SIGNATURE);
                break;
            case Type.DOUBLE:
                cv.visitMethodInsn(
                    INVOKESPECIAL,
                    DOUBLE_CLASS_NAME,
                    INIT_METHOD_NAME,
                    DOUBLE_CLASS_INIT_METHOD_SIGNATURE);
                break;
            case Type.BYTE:
                cv.visitMethodInsn(INVOKESPECIAL, BYTE_CLASS_NAME, INIT_METHOD_NAME, BYTE_CLASS_INIT_METHOD_SIGNATURE);
                break;
            case Type.BOOLEAN:
                cv.visitMethodInsn(
                    INVOKESPECIAL,
                    BOOLEAN_CLASS_NAME,
                    INIT_METHOD_NAME,
                    BOOLEAN_CLASS_INIT_METHOD_SIGNATURE);
                break;
            case Type.CHAR:
                cv.visitMethodInsn(
                    INVOKESPECIAL,
                    CHARACTER_CLASS_NAME,
                    INIT_METHOD_NAME,
                    CHARACTER_CLASS_INIT_METHOD_SIGNATURE);
                break;
        }
    }

    /**
     * Handles the unwrapping of a type, unboxing of primitives and casting to the correct object type.
     * 
     * @param cv
     * @param type
     */
    public static void unwrapType(final CodeVisitor cv, final Type type) {
        switch (type.getSort()) {
            case Type.SHORT:
                cv.visitTypeInsn(CHECKCAST, SHORT_CLASS_NAME);
                cv.visitMethodInsn(
                    INVOKEVIRTUAL,
                    SHORT_CLASS_NAME,
                    SHORT_VALUE_METHOD_NAME,
                    SHORT_VALUE_METHOD_SIGNATURE);
                break;
            case Type.INT:
                cv.visitTypeInsn(CHECKCAST, INTEGER_CLASS_NAME);
                cv
                        .visitMethodInsn(
                            INVOKEVIRTUAL,
                            INTEGER_CLASS_NAME,
                            INT_VALUE_METHOD_NAME,
                            INT_VALUE_METHOD_SIGNATURE);
                break;
            case Type.LONG:
                cv.visitTypeInsn(CHECKCAST, LONG_CLASS_NAME);
                cv.visitMethodInsn(INVOKEVIRTUAL, LONG_CLASS_NAME, LONG_VALUE_METHOD_NAME, LONG_VALUE_METHOD_SIGNATURE);
                break;
            case Type.FLOAT:
                cv.visitTypeInsn(CHECKCAST, FLOAT_CLASS_NAME);
                cv.visitMethodInsn(
                    INVOKEVIRTUAL,
                    FLOAT_CLASS_NAME,
                    FLOAT_VALUE_METHOD_NAME,
                    FLOAT_VALUE_METHOD_SIGNATURE);
                break;
            case Type.DOUBLE:
                cv.visitTypeInsn(CHECKCAST, DOUBLE_CLASS_NAME);
                cv.visitMethodInsn(
                    INVOKEVIRTUAL,
                    DOUBLE_CLASS_NAME,
                    DOUBLE_VALUE_METHOD_NAME,
                    DOUBLE_VALUE_METHOD_SIGNATURE);
                break;
            case Type.BYTE:
                cv.visitTypeInsn(CHECKCAST, BYTE_CLASS_NAME);
                cv.visitMethodInsn(INVOKEVIRTUAL, BYTE_CLASS_NAME, BYTE_VALUE_METHOD_NAME, BYTE_VALUE_METHOD_SIGNATURE);
                break;
            case Type.BOOLEAN:
                cv.visitTypeInsn(CHECKCAST, BOOLEAN_CLASS_NAME);
                cv.visitMethodInsn(
                    INVOKEVIRTUAL,
                    BOOLEAN_CLASS_NAME,
                    BOOLEAN_VALUE_METHOD_NAME,
                    BOOLEAN_VALUE_METHOD_SIGNATURE);
                break;
            case Type.CHAR:
                cv.visitTypeInsn(CHECKCAST, CHARACTER_CLASS_NAME);
                cv.visitMethodInsn(
                    INVOKEVIRTUAL,
                    CHARACTER_CLASS_NAME,
                    CHAR_VALUE_METHOD_NAME,
                    CHAR_VALUE_METHOD_SIGNATURE);
                break;
            case Type.OBJECT:
                String objectTypeName = type.getClassName().replace('.', '/');
                cv.visitTypeInsn(CHECKCAST, objectTypeName);
                break;
            case Type.ARRAY:
                cv.visitTypeInsn(CHECKCAST, type.getDescriptor());
                break;
        }
    }

    /**
     * Adds a string and inserts null if the string is null.
     * 
     * @param cv
     * @param value
     */
    public static void addNullableString(final CodeVisitor cv, final String value) {
        if (value == null) {
            cv.visitInsn(ACONST_NULL);
        } else {
            cv.visitLdcInsn(value);
        }
    }
}