1 28 29 package org.jibx.binding.classes; 30 31 import java.io.File ; 32 import java.io.IOException ; 33 import java.util.ArrayList ; 34 import java.util.HashMap ; 35 36 import org.apache.bcel.classfile.Utility; 37 import org.jibx.runtime.JiBXException; 38 39 47 48 public class ClassCache 49 { 50 51 private static ClassCache s_instance; 52 53 54 private String [] m_paths; 55 56 57 private File [] m_roots; 58 59 60 private HashMap m_classMap; 61 62 68 69 private ClassCache(String [] paths) { 70 ArrayList keepers = new ArrayList (); 71 for (int i = 0; i < paths.length; i++) { 72 String path = paths[i]; 73 int mark = path.lastIndexOf('.'); 74 if (mark < 0 || !"jar".equalsIgnoreCase(path.substring(mark+1))) { 75 keepers.add(paths[i]); 76 } 77 } 78 m_paths = new String [keepers.size()]; 79 m_roots = new File [keepers.size()]; 80 for (int i = 0; i < keepers.size(); i++) { 81 String path = (String )keepers.get(i); 82 int length = path.length(); 83 if (length > 0) { 84 if (path.charAt(length-1) != File.separatorChar) { 85 path = path + File.separator; 86 } 87 } 88 m_paths[i] = path; 89 m_roots[i] = new File (path); 90 } 91 m_classMap = new HashMap (); 92 } 93 94 104 105 private ClassFile getClassFileImpl(String name) throws JiBXException { 106 107 Object match = m_classMap.get(name); 109 if (match != null) { 110 return (ClassFile)match; 111 } else if (ClassItem.isPrimitive(name) || name.endsWith("[]")) { 112 113 ClassFile cf = new ClassFile(name, Utility.getSignature(name)); 115 m_classMap.put(name, cf); 116 return cf; 117 118 } else { 119 try { 120 121 ClassFile cf = null; 123 String path = name.replace('.', File.separatorChar) + ".class"; 124 for (int i = 0; i < m_paths.length; i++) { 125 126 File file = new File (m_paths[i], path); 128 if (file.exists()) { 129 130 cf = new ClassFile(name, m_roots[i], file); 132 break; 133 134 } 135 } 136 137 if (cf == null) { 139 cf = new ClassFile(name); 140 } 141 142 if (cf != null) { 144 145 String sname = cf.getSuperName(); 147 if (!name.equals(sname) && sname != null) { 148 ClassFile sf = getClassFileImpl(sname); 149 if (sf == null) { 150 throw new JiBXException("Superclass " + sname + 151 " of class " + name + " not found"); 152 } 153 cf.setSuperFile(sf); 154 } 155 156 m_classMap.put(name, cf); 158 159 } 160 return cf; 161 162 } catch (IOException ex) { 163 throw new JiBXException("Error loading class " + name); 164 } 165 } 166 } 167 168 177 178 public static ClassFile getClassFile(String name) throws JiBXException { 179 return s_instance.getClassFileImpl(name); 180 } 181 182 187 188 static void addClassFile(ClassFile cf) { 189 s_instance.m_classMap.put(cf.getName(), cf); 190 } 191 192 199 200 public static void setPaths(String [] paths) { 201 s_instance = new ClassCache(paths); 202 } 203 } | Popular Tags |