001 package org.apache.commons.ognl; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022 import java.util.HashMap; 023 import java.util.Map; 024 025 /** 026 * Default class resolution. Uses ClassLoader.loadClass() to look up classes by name. It also looks in the "java.lang" 027 * package 028 * if the class named does not give a package specifier, allowing easier usage of these classes. 029 * 030 * @author Luke Blanshard (blanshlu@netscape.net) 031 * @author Drew Davidson (drew@ognl.org) 032 */ 033 public class DefaultClassResolver 034 implements ClassResolver 035 { 036 private final Map<String, Class<?>> classes = new HashMap<String, Class<?>>( 101 ); 037 038 /** 039 * Resolves a class for a given className 040 * 041 * @param className The name of the Class 042 * @return The resulting Class object 043 * @throws ClassNotFoundException If the class could not be found 044 */ 045 public Class<?> classForName( String className ) 046 throws ClassNotFoundException 047 { 048 return classForName( className, null ); 049 } 050 051 /** 052 * {@inheritDoc} 053 */ 054 public Class<?> classForName( String className, Map<String, Object> unused ) 055 throws ClassNotFoundException 056 { 057 Class<?> result = classes.get( className ); 058 059 if ( result == null ) 060 { 061 ClassLoader classLoader = ClassLoader.getSystemClassLoader(); 062 try 063 { 064 result = classLoader.loadClass( className ); 065 } 066 catch ( ClassNotFoundException ex ) 067 { 068 if ( className.indexOf( '.' ) == -1 ) 069 { 070 result = classLoader.loadClass( "java.lang." + className ); 071 classes.put( "java.lang." + className, result ); 072 } 073 } 074 classes.put( className, result ); 075 } 076 return result; 077 } 078 }