1 16 package net.sf.cglib.util; 17 18 import java.lang.reflect.Method ; 19 import java.lang.reflect.Modifier ; 20 import net.sf.cglib.core.*; 21 import org.objectweb.asm.ClassVisitor; 22 import org.objectweb.asm.Type; 23 24 class ParallelSorterEmitter extends ClassEmitter { 25 private static final Type PARALLEL_SORTER = 26 TypeUtils.parseType("net.sf.cglib.util.ParallelSorter"); 27 private static final Signature CSTRUCT_OBJECT_ARRAY = 28 TypeUtils.parseConstructor("Object[]"); 29 private static final Signature NEW_INSTANCE = 30 new Signature("newInstance", PARALLEL_SORTER, new Type[]{ Constants.TYPE_OBJECT_ARRAY }); 31 private static final Signature SWAP = 32 TypeUtils.parseSignature("void swap(int, int)"); 33 34 public ParallelSorterEmitter(ClassVisitor v, String className, Object [] arrays) { 35 super(v); 36 begin_class(Constants.V1_2, Constants.ACC_PUBLIC, className, PARALLEL_SORTER, null, Constants.SOURCE_FILE); 37 EmitUtils.null_constructor(this); 38 EmitUtils.factory_method(this, NEW_INSTANCE); 39 generateConstructor(arrays); 40 generateSwap(arrays); 41 end_class(); 42 } 43 44 private String getFieldName(int index) { 45 return "FIELD_" + index; 46 } 47 48 private void generateConstructor(Object [] arrays) { 49 CodeEmitter e = begin_method(Constants.ACC_PUBLIC, CSTRUCT_OBJECT_ARRAY, null); 50 e.load_this(); 51 e.super_invoke_constructor(); 52 e.load_this(); 53 e.load_arg(0); 54 e.super_putfield("a", Constants.TYPE_OBJECT_ARRAY); 55 for (int i = 0; i < arrays.length; i++) { 56 Type type = Type.getType(arrays[i].getClass()); 57 declare_field(Constants.ACC_PRIVATE, getFieldName(i), type, null); 58 e.load_this(); 59 e.load_arg(0); 60 e.push(i); 61 e.aaload(); 62 e.checkcast(type); 63 e.putfield(getFieldName(i)); 64 } 65 e.return_value(); 66 e.end_method(); 67 } 68 69 private void generateSwap(final Object [] arrays) { 70 CodeEmitter e = begin_method(Constants.ACC_PUBLIC, SWAP, null); 71 for (int i = 0; i < arrays.length; i++) { 72 Type type = Type.getType(arrays[i].getClass()); 73 Type component = TypeUtils.getComponentType(type); 74 Local T = e.make_local(type); 75 76 e.load_this(); 77 e.getfield(getFieldName(i)); 78 e.store_local(T); 79 80 e.load_local(T); 81 e.load_arg(0); 82 83 e.load_local(T); 84 e.load_arg(1); 85 e.array_load(component); 86 87 e.load_local(T); 88 e.load_arg(1); 89 90 e.load_local(T); 91 e.load_arg(0); 92 e.array_load(component); 93 94 e.array_store(component); 95 e.array_store(component); 96 } 97 e.return_value(); 98 e.end_method(); 99 } 100 } 101 | Popular Tags |