001: /*
002: * Copyright 2003 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package net.sf.cglib.util;
017:
018: import java.lang.reflect.Method;
019: import java.lang.reflect.Modifier;
020: import net.sf.cglib.core.*;
021: import org.objectweb.asm.ClassVisitor;
022: import org.objectweb.asm.Type;
023:
024: class ParallelSorterEmitter extends ClassEmitter {
025: private static final Type PARALLEL_SORTER = TypeUtils
026: .parseType("net.sf.cglib.util.ParallelSorter");
027: private static final Signature CSTRUCT_OBJECT_ARRAY = TypeUtils
028: .parseConstructor("Object[]");
029: private static final Signature NEW_INSTANCE = new Signature(
030: "newInstance", PARALLEL_SORTER,
031: new Type[] { Constants.TYPE_OBJECT_ARRAY });
032: private static final Signature SWAP = TypeUtils
033: .parseSignature("void swap(int, int)");
034:
035: public ParallelSorterEmitter(ClassVisitor v, String className,
036: Object[] arrays) {
037: super (v);
038: begin_class(Constants.V1_2, Constants.ACC_PUBLIC, className,
039: PARALLEL_SORTER, null, Constants.SOURCE_FILE);
040: EmitUtils.null_constructor(this );
041: EmitUtils.factory_method(this , NEW_INSTANCE);
042: generateConstructor(arrays);
043: generateSwap(arrays);
044: end_class();
045: }
046:
047: private String getFieldName(int index) {
048: return "FIELD_" + index;
049: }
050:
051: private void generateConstructor(Object[] arrays) {
052: CodeEmitter e = begin_method(Constants.ACC_PUBLIC,
053: CSTRUCT_OBJECT_ARRAY, null);
054: e.load_this ();
055: e.super _invoke_constructor();
056: e.load_this ();
057: e.load_arg(0);
058: e.super _putfield("a", Constants.TYPE_OBJECT_ARRAY);
059: for (int i = 0; i < arrays.length; i++) {
060: Type type = Type.getType(arrays[i].getClass());
061: declare_field(Constants.ACC_PRIVATE, getFieldName(i), type,
062: null);
063: e.load_this ();
064: e.load_arg(0);
065: e.push(i);
066: e.aaload();
067: e.checkcast(type);
068: e.putfield(getFieldName(i));
069: }
070: e.return_value();
071: e.end_method();
072: }
073:
074: private void generateSwap(final Object[] arrays) {
075: CodeEmitter e = begin_method(Constants.ACC_PUBLIC, SWAP, null);
076: for (int i = 0; i < arrays.length; i++) {
077: Type type = Type.getType(arrays[i].getClass());
078: Type component = TypeUtils.getComponentType(type);
079: Local T = e.make_local(type);
080:
081: e.load_this ();
082: e.getfield(getFieldName(i));
083: e.store_local(T);
084:
085: e.load_local(T);
086: e.load_arg(0);
087:
088: e.load_local(T);
089: e.load_arg(1);
090: e.array_load(component);
091:
092: e.load_local(T);
093: e.load_arg(1);
094:
095: e.load_local(T);
096: e.load_arg(0);
097: e.array_load(component);
098:
099: e.array_store(component);
100: e.array_store(component);
101: }
102: e.return_value();
103: e.end_method();
104: }
105: }
|