| 1 | // |
| 2 | // (c) Copyright, Moebius Solutions, 2002 |
| 3 | // |
| 4 | // All Rights Reserved |
| 5 | // |
| 6 | // This material may be reproduced by or for the U. S. Government |
| 7 | // pursuant to the copyright license under the clause at |
| 8 | // DFARS 252.227-7013 (OCT 1988). |
| 9 | // |
| 10 | package com.moesol.generator.core; |
| 11 | |
| 12 | import java.lang.reflect.*; |
| 13 | |
| 14 | import com.moesol.generator.visitor.*; |
| 15 | |
| 16 | public class VisitClass { |
| 17 | public static void visitBases(Class c, BaseVisitor v) { |
| 18 | Class superClass = c.getSuperclass(); |
| 19 | Class interfaces[] = c.getInterfaces(); |
| 20 | if (superClass != null) { |
| 21 | v.visitSuperClass(c, superClass); |
| 22 | } |
| 23 | for (int i = 0; i < interfaces.length; i++) { |
| 24 | v.visitInterface(c, interfaces[i], i); |
| 25 | } |
| 26 | } |
| 27 | public static void visitReflect(Class c, ReflectVisitor v) |
| 28 | throws ApplyException |
| 29 | { |
| 30 | visitReflect(c, v, 0, 0); |
| 31 | } |
| 32 | public static void visitReflect(Class c, ReflectVisitor v, int onMods, int offMods) |
| 33 | throws ApplyException |
| 34 | { |
| 35 | Constructor ctors[] = c.getDeclaredConstructors(); |
| 36 | java.util.Arrays.sort(ctors, m_ctor_cmp); |
| 37 | for (int i = 0; i < ctors.length; i++) { |
| 38 | if (checkMods(ctors[i].getModifiers(), onMods, offMods)) { |
| 39 | v.visitConstructor(c, ctors[i]); |
| 40 | } |
| 41 | } |
| 42 | Method methods[] = c.getDeclaredMethods(); |
| 43 | java.util.Arrays.sort(methods, m_method_cmp); |
| 44 | for (int i = 0; i < methods.length; i++) { |
| 45 | if (checkMods(methods[i].getModifiers(), onMods, offMods)) { |
| 46 | v.visitMethod(c, methods[i]); |
| 47 | } |
| 48 | } |
| 49 | Field fields[] = c.getDeclaredFields(); |
| 50 | for (int i = 0; i < fields.length; i++) { |
| 51 | if (checkMods(fields[i].getModifiers(), onMods, offMods)) { |
| 52 | v.visitField(c, fields[i]); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | // TODO does anyone need the declaring class? |
| 57 | public static void visitParamTypes(Class[] paramTypes, ParamTypeVisitor v) |
| 58 | throws ApplyException |
| 59 | { |
| 60 | _visitParamTypes(null, paramTypes, v); |
| 61 | } |
| 62 | private static void _visitParamTypes(Class c, Class[] paramTypes, ParamTypeVisitor v) |
| 63 | throws ApplyException |
| 64 | { |
| 65 | int j = 0; |
| 66 | if (j < paramTypes.length) { |
| 67 | v.visitParamType(c, j, paramTypes[j]); |
| 68 | } |
| 69 | for (j = j + 1; j < paramTypes.length; j++) { |
| 70 | v.visitParamSeparator(c); |
| 71 | v.visitParamType(c, j, paramTypes[j]); |
| 72 | } |
| 73 | } |
| 74 | public static void visitParamTypes(Method m, ParamTypeVisitor v) |
| 75 | throws ApplyException |
| 76 | { |
| 77 | _visitParamTypes(m.getDeclaringClass(), m.getParameterTypes(), v); |
| 78 | } |
| 79 | // TODO deprecate |
| 80 | public static void visitParamTypes(Class c, Method m, ParamTypeVisitor v) |
| 81 | throws ApplyException |
| 82 | { |
| 83 | _visitParamTypes(c, m.getParameterTypes(), v); |
| 84 | } |
| 85 | public static void visitParamTypes(Constructor ctor, ParamTypeVisitor v) |
| 86 | throws ApplyException |
| 87 | { |
| 88 | _visitParamTypes(ctor.getDeclaringClass(), ctor.getParameterTypes(), v); |
| 89 | } |
| 90 | // TODO deprecate |
| 91 | public static void visitParamTypes(Class c, Constructor ctor, ParamTypeVisitor v) |
| 92 | throws ApplyException |
| 93 | { |
| 94 | _visitParamTypes(c, ctor.getParameterTypes(), v); |
| 95 | } |
| 96 | |
| 97 | private static void _visitPrimitive(Class type, TypeVisitor v) { |
| 98 | if (type == Boolean.TYPE) { |
| 99 | v.visitBoolean(type); |
| 100 | } else if (type == Byte.TYPE) { |
| 101 | v.visitByte(type); |
| 102 | } else if (type == Character.TYPE) { |
| 103 | v.visitCharacter(type); |
| 104 | } else if (type == Short.TYPE) { |
| 105 | v.visitShort(type); |
| 106 | } else if (type == Integer.TYPE) { |
| 107 | v.visitInteger(type); |
| 108 | } else if (type == Long.TYPE) { |
| 109 | v.visitLong(type); |
| 110 | } else if (type == Float.TYPE) { |
| 111 | v.visitFloat(type); |
| 112 | } else if (type == Double.TYPE) { |
| 113 | v.visitDouble(type); |
| 114 | } else if (type == Void.TYPE) { |
| 115 | v.visitVoid(type); |
| 116 | } else { |
| 117 | throw new IllegalArgumentException("Not a known primitive type" + type.getName()); |
| 118 | } |
| 119 | } |
| 120 | /** |
| 121 | * Call the correct visit method in the TypeVisitor based on the |
| 122 | * <code>type</code>. |
| 123 | */ |
| 124 | public static void visitType(Class type, TypeVisitor v) { |
| 125 | if (type.isPrimitive()) { |
| 126 | _visitPrimitive(type, v); |
| 127 | } else if (type.isArray()) { |
| 128 | v.visitArray(type); |
| 129 | } else { |
| 130 | v.visitObject(type); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // mods onMods (~mods & onMods) |
| 135 | // 0 0 0 |
| 136 | // 1 0 0 |
| 137 | // 0 1 1 |
| 138 | // 1 1 0 |
| 139 | public static boolean checkMods(int mods, int onMods, int offMods) { |
| 140 | return (((~mods & onMods) == 0) && ((mods & offMods) == 0)); |
| 141 | } |
| 142 | |
| 143 | public static java.util.Comparator getMethodComparator() { |
| 144 | return m_method_cmp; |
| 145 | } |
| 146 | |
| 147 | private static java.util.Comparator m_method_cmp = new java.util.Comparator() { |
| 148 | public int compare(Object o1, Object o2) { |
| 149 | return compare((Method)o1, (Method)o2); |
| 150 | } |
| 151 | private int compare(Method m1, Method m2) { |
| 152 | int r = m1.getName().compareTo(m2.getName()); |
| 153 | if (r != 0) { return r; } |
| 154 | r = m1.getParameterTypes().length - m2.getParameterTypes().length; |
| 155 | if (r != 0) { return r; } |
| 156 | for (int i = 0; i < m1.getParameterTypes().length; i++) { |
| 157 | r = m1.getParameterTypes()[i].getName() |
| 158 | .compareTo(m2.getParameterTypes()[i].getName()); |
| 159 | if (r != 0) { return r; } |
| 160 | } |
| 161 | return r; |
| 162 | } |
| 163 | }; |
| 164 | private static java.util.Comparator m_ctor_cmp = new java.util.Comparator() { |
| 165 | public int compare(Object o1, Object o2) { |
| 166 | return compare((Constructor)o1, (Constructor)o2); |
| 167 | } |
| 168 | private int compare(Constructor c1, Constructor c2) { |
| 169 | int r; |
| 170 | r = c1.getParameterTypes().length - c2.getParameterTypes().length; |
| 171 | if (r != 0) { return r; } |
| 172 | for (int i = 0; i < c1.getParameterTypes().length; i++) { |
| 173 | r = c1.getParameterTypes()[i].getName() |
| 174 | .compareTo(c2.getParameterTypes()[i].getName()); |
| 175 | if (r != 0) { return r; } |
| 176 | } |
| 177 | return r; |
| 178 | } |
| 179 | }; |
| 180 | } |