1 /* 2 * Copyright 2002-2004 The Apache Software Foundation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.apache.commons.clazz; 17 18 import java.util.HashSet; 19 20 /** 21 * Constructs/loads Clazzes. 22 * 23 * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a> 24 * @version $Id: ClazzLoader.java 155436 2005-02-26 13:17:48Z dirkv $ 25 */ 26 public abstract class ClazzLoader { 27 private ModelClazzLoader modelClazzLoader; 28 29 /** 30 * ClazzLoaders are grouped into models. 31 */ 32 public ClazzLoader(ModelClazzLoader modelClazzLoader) { 33 this.modelClazzLoader = modelClazzLoader; 34 } 35 36 /** 37 * Returns the model clazz loader for this clazz loader. 38 */ 39 public ModelClazzLoader getModelClazzLoader() { 40 return modelClazzLoader; 41 } 42 43 /** 44 * Returns the name of the model this clazz loader belongs to. 45 */ 46 public String getModel() { 47 return getModelClazzLoader().getModel(); 48 } 49 50 /** 51 * Given a Clazz name, returns the corresponding Clazz. Does not 52 * cache clazzes, that's the job of CachingGroupClazzLoader. Return null if 53 * there is no Clazz for this name. 54 * <p> 55 * Note that we use canonical class names for primitive types and arrays, 56 * e.g. "byte", "byte[]", "boolean[][]", "java.lang.String[]". 57 */ 58 public abstract Clazz getClazzForName(String name); 59 60 /** 61 * Returns true iff this loader has or can construct a Clazz for the 62 * supplied instance. 63 */ 64 public abstract boolean isMember(Object instance); 65 66 /** 67 * Returns the Clazz for the supplied instance. Returns <code>null</code> 68 * if it cannot produce such a Clazz. 69 */ 70 public Clazz getClazz(Object instance) { 71 if (instance == null) { 72 return null; 73 } 74 75 String name = getClazzName(instance); 76 if (name == null) { 77 return null; 78 } 79 return getClazzForName(name); 80 } 81 82 /** 83 * Returns the clazz name for the supplied instance. Returns 84 * <code>null</code> if it cannot produce a clazz name for the instance. 85 */ 86 public abstract String getClazzName(Object instance); 87 88 /** 89 * Defines a new Clazz with the supplied name. If it cannot define a 90 * clazz of the supplied type or if it cannot define clazzes at all, returns 91 * null. 92 * 93 * @param name the name of the clazz, should be unique within the scope of 94 * the top-level ClazzLoader 95 * @param clazzClass the Class of the Clazz that this method needs to 96 * produce. 97 * @param instanceClass the Class of the instances the new Clazz will be 98 * able to create using the <code>newInstance()</code> method. 99 * 100 * @return Clazz 101 */ 102 public abstract Clazz defineClazz( 103 String name, 104 Class clazzClass, 105 Class instanceClass); 106 107 private HashSet loggingEnabledClasses = new HashSet(); 108 109 /** 110 * Enable diagnistic logging for the specified class. 111 */ 112 public void enableLogging(String className) { 113 loggingEnabledClasses.add(className); 114 } 115 116 public boolean isLoggingEnabled(String className) { 117 return loggingEnabledClasses.contains(className); 118 } 119 120 }