KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > cglib > util > ParallelSorterEmitter


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2003 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  * if any, must include the following acknowledgment:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowledgment may appear in the software itself,
24  * if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Apache" and "Apache Software Foundation" must
27  * not be used to endorse or promote products derived from this
28  * software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache",
32  * nor may "Apache" appear in their name, without prior written
33  * permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation. For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  */

54 package org.logicalcobwebs.cglib.util;
55
56 import java.lang.reflect.Method JavaDoc;
57 import java.lang.reflect.Modifier JavaDoc;
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 JavaDoc className, Object JavaDoc[] 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 JavaDoc getFieldName(int index) {
83         return "FIELD_" + index;
84     }
85
86     private void generateConstructor(Object JavaDoc[] 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 JavaDoc[] 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