01: /*
02: * Copyright 2003,2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
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 getClassName(String prefix, String source,
34: Object key, Predicate names) {
35:
36: StringBuffer sb = new StringBuffer();
37: sb.append((prefix != null) ? (prefix.startsWith("java") ? "$"
38: + prefix : prefix) : "net.sf.cglib.empty.Object");
39: sb.append("$$");
40: sb.append(source.substring(source.lastIndexOf('.') + 1));
41: sb.append("ByCGLIB$$");
42: sb.append(Integer.toHexString(key.hashCode()));
43: String base = sb.toString();
44: String attempt = base;
45: int index = 2;
46: while (names.evaluate(attempt)) {
47: attempt = base + "_" + index++;
48: }
49:
50: return attempt;
51: }
52: }
|