Example usage for org.apache.commons.bcel6.classfile Method getSignature

List of usage examples for org.apache.commons.bcel6.classfile Method getSignature

Introduction

In this page you can find the example usage for org.apache.commons.bcel6.classfile Method getSignature.

Prototype

public final String getSignature() 

Source Link

Usage

From source file:ru.objective.jni.tasks.builders.AbstractBuilder.java

protected String getHeaderDeclarationMethod(MethodExportInfo info, Method method, boolean overloaded) {
    StringBuilder stringBuilder = new StringBuilder();
    String[] argumentTypes = Utility.methodSignatureArgumentTypes(method.getSignature());
    String name = info.name;// w w  w .j  av a  2  s  .  c  o  m

    String methodReturnType = null;

    if (Utils.isConstructor(method)) {
        methodReturnType = "instancetype";

        if (name.equals("<init>"))
            name = "init";
    } else {
        if (Utils.isOccupiedWord(name)) {
            name = "_" + name;
        }

        methodReturnType = Utility.methodSignatureReturnType(method.getSignature()).toString();

        methodReturnType = Utils.getBasicType(methodReturnType);

        if (!Utils.isPrimitive(method.getReturnType())) {
            JavaClass typeJavaClass = OJNIClassLoader.getInstance()
                    .loadClass(method.getReturnType().toString());

            if (typeJavaClass != null && typeJavaClass.isInterface())
                methodReturnType = "id <" + getPrefix() + Utils.getShortClassName(methodReturnType) + ">";
            else
                methodReturnType = getPrefix() + Utils.getShortClassName(methodReturnType) + " *";
        }

        if (Utils.isArrayType(method.getReturnType()))
            methodReturnType = getStringArrayType(methodReturnType, (ArrayType) method.getReturnType());
        else
            methodReturnType = PrimitiveTypeConverter.convertToOBJCType(methodReturnType);
    }
    stringBuilder.append((method.isStatic() ? "+" : "-") + " " + "("
            + PrimitiveTypeConverter.convertToOBJCType(methodReturnType) + ")");

    String[] nameParameters = name.split(":");

    if (info.isCustom)
        overloaded = false;

    LocalVariableTable table = method.getLocalVariableTable();

    if (argumentTypes.length == 0)
        stringBuilder.append(name);

    for (int i = 0, var_index = (method.isStatic() ? 0 : 1); i < argumentTypes.length; i++, var_index++) {
        Type javaType = method.getArgumentTypes()[i];
        String type = argumentTypes[i];

        String variable_name = "";
        if (table == null) {
            variable_name = "arg" + var_index;
        } else {
            LocalVariable lv = table.getLocalVariable(var_index, 0);
            if (lv != null) {
                variable_name = lv.getName();
            }
        }

        if (javaType.equals(Type.LONG) || javaType.equals(Type.DOUBLE))
            var_index++;

        String nameParameter = (i < nameParameters.length ? nameParameters[i] : variable_name);

        type = Utils.getBasicType(type);

        String overloadedParameter = "";

        if (overloaded) {
            overloadedParameter = "With" + (i == 0 ? StringUtils.capitalize(variable_name) : "")
                    + Utils.getShortClassName(type);/*+ StringUtils.capitalize(utils.getShortClassName(type));*/
        }

        if (!Utils.isPrimitive(javaType)) {
            JavaClass argTypeJavaClass = OJNIClassLoader.getInstance().loadClass(javaType.toString());

            if (argTypeJavaClass != null && argTypeJavaClass.isInterface())
                type = "id <" + getPrefix() + Utils.getShortClassName(type) + ">";
            else
                type = getPrefix() + Utils.getShortClassName(type) + " *";
        }

        if (Utils.isArrayType(javaType)) {
            if (overloaded) {
                int dimensions = ((ArrayType) javaType).getDimensions();

                if (dimensions > 1)
                    overloadedParameter += dimensions + "dimArray";
                else
                    overloadedParameter += "Array";
            }
            type = getStringArrayType(type, (ArrayType) javaType);
        } else {
            type = PrimitiveTypeConverter.convertToOBJCType(type);
        }

        if (Utils.isOccupiedWord(variable_name)) {
            variable_name = "_" + variable_name;
        }

        stringBuilder.append(nameParameter + overloadedParameter + ":(" + type + ")" + variable_name + " ");
    }

    stringBuilder.append(";");

    return stringBuilder.toString();
}

From source file:ru.objective.jni.tasks.builders.ClassBuilder.java

private String getMethodImplementation(Method method, String declaration) {
    StringBuilder builder = new StringBuilder();

    String vars = generateArgumentString(method);

    builder.append(declaration).append(" {").append(System.lineSeparator());

    builder.append("OJNIEnv *__env = [OJNIEnv currentEnv];").append(System.lineSeparator());
    builder.append("jmethodID mid = [[OJNIMidManager sharedManager] methodIDFor");
    if (method.isStatic())
        builder.append("Static");
    builder.append("Method:@\"" + method.getName() + "\" ");
    builder.append("signature:@\"" + method.getSignature() + "\" inClass:self.class];");
    builder.append(System.lineSeparator());

    // todo remove [self.class OJNIClass];
    if (method.getReturnType().equals(Type.VOID)) {
        if (Utils.isConstructor(method)) {
            builder.append("jobject __obj = [__env newObject:[self.class OJNIClass] method:mid");
            builder.append(vars).append("];").append(System.lineSeparator());
            builder.append("return [super initWithJavaObject:__obj];");

        } else {/*from w  ww .ja  va 2 s. c  om*/
            if (method.isStatic()) {
                builder.append("[__env callStaticVoidMethodOnClass:[self.class OJNIClass] method:mid");
            } else {
                builder.append("[__env callVoidMethodOnObject:[self javaObject] method:mid");
            }

            builder.append(vars).append("];");
        }
    } else {
        builder.append(generateCallMethod(method, vars));
        builder.append(generateReturnObject(method.getReturnType()));
    }

    builder.append(System.lineSeparator()).append("}");

    return builder.toString();
}