KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cglib > core > DefaultNamingPolicy


1 /*
2  * Copyright 2003,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 net.sf.cglib.core;
17
18 import java.util.Set JavaDoc;
19
20 /**
21  * The default policy used by {@link AbstractClassGenerator}.
22  * Generates names such as
23  * <p><code>net.sf.cglib.Foo$$EnhancerByCGLIB$$38272841</code><p>
24  * This is composed of a prefix based on the name of the superclass, a fixed
25  * string incorporating the CGLIB class responsible for generation, and a
26  * hashcode derived from the parameters used to create the object. If the same
27  * name has been previously been used in the same <code>ClassLoader</code>, a
28  * suffix is added to ensure uniqueness.
29  */

30 public class DefaultNamingPolicy implements NamingPolicy {
31     public static final DefaultNamingPolicy INSTANCE = new DefaultNamingPolicy();
32     
33     public String JavaDoc getClassName(String JavaDoc prefix, String JavaDoc source, Object JavaDoc key, Predicate names) {
34         
35         
36         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
37         sb.append(
38                   (prefix != null) ?
39                                      (
40                                       prefix.startsWith("java") ?
41                                                    "$" + prefix : prefix
42                                      )
43                                     : "net.sf.cglib.empty.Object"
44                  );
45         sb.append("$$");
46         sb.append(source.substring(source.lastIndexOf('.') + 1));
47         sb.append("ByCGLIB$$");
48         sb.append(Integer.toHexString(key.hashCode()));
49         String JavaDoc base = sb.toString();
50         String JavaDoc attempt = base;
51         int index = 2;
52         while (names.evaluate(attempt)) {
53             attempt = base + "_" + index++;
54         }
55        
56         return attempt;
57     }
58 }
59
Popular Tags