1 54 package org.logicalcobwebs.cglib.util; 55 56 import java.lang.reflect.Method ; 57 import java.lang.reflect.Modifier ; 58 import org.logicalcobwebs.cglib.core.*; 59 import org.logicalcobwebs.asm.ClassVisitor; 60 import org.logicalcobwebs.asm.Type; 61 62 class ParallelSorterEmitter extends ClassEmitter { 63 private static final Signature CSTRUCT_OBJECT_ARRAY = 64 TypeUtils.parseConstructor("Object[]"); 65 private static final Signature NEW_INSTANCE = 66 TypeUtils.parseSignature("org.logicalcobwebs.cglib.util.ParallelSorter newInstance(Object[])"); 67 private static final Signature SWAP = 68 TypeUtils.parseSignature("void swap(int, int)"); 69 private static final Type PARALLEL_SORTER = 70 TypeUtils.parseType("org.logicalcobwebs.cglib.util.ParallelSorter"); 71 72 public ParallelSorterEmitter(ClassVisitor v, String className, Object [] arrays) { 73 super(v); 74 begin_class(Constants.ACC_PUBLIC, className, PARALLEL_SORTER, null, Constants.SOURCE_FILE); 75 EmitUtils.null_constructor(this); 76 EmitUtils.factory_method(this, NEW_INSTANCE); 77 generateConstructor(arrays); 78 generateSwap(arrays); 79 end_class(); 80 } 81 82 private String getFieldName(int index) { 83 return "FIELD_" + index; 84 } 85 86 private void generateConstructor(Object [] arrays) { 87 CodeEmitter e = begin_method(Constants.ACC_PUBLIC, CSTRUCT_OBJECT_ARRAY, null, null); 88 e.load_this(); 89 e.super_invoke_constructor(); 90 e.load_this(); 91 e.load_arg(0); 92 e.super_putfield("a", Constants.TYPE_OBJECT_ARRAY); 93 for (int i = 0; i < arrays.length; i++) { 94 Type type = Type.getType(arrays[i].getClass()); 95 declare_field(Constants.ACC_PRIVATE, getFieldName(i), type, null, null); 96 e.load_this(); 97 e.load_arg(0); 98 e.push(i); 99 e.aaload(); 100 e.checkcast(type); 101 e.putfield(getFieldName(i)); 102 } 103 e.return_value(); 104 e.end_method(); 105 } 106 107 private void generateSwap(final Object [] arrays) { 108 CodeEmitter e = begin_method(Constants.ACC_PUBLIC, SWAP, null, null); 109 for (int i = 0; i < arrays.length; i++) { 110 Type type = Type.getType(arrays[i].getClass()); 111 Type component = TypeUtils.getComponentType(type); 112 Local T = e.make_local(type); 113 114 e.load_this(); 115 e.getfield(getFieldName(i)); 116 e.store_local(T); 117 118 e.load_local(T); 119 e.load_arg(0); 120 121 e.load_local(T); 122 e.load_arg(1); 123 e.array_load(component); 124 125 e.load_local(T); 126 e.load_arg(1); 127 128 e.load_local(T); 129 e.load_arg(0); 130 e.array_load(component); 131 132 e.array_store(component); 133 e.array_store(component); 134 } 135 e.return_value(); 136 e.end_method(); 137 } 138 } 139 | Popular Tags |