/*
 * Copyright 2003,2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.sf.cglib.core;

import java.util.Set;

/**
 * The default policy used by {@link AbstractClassGenerator}.
 * Generates names such as
 * <p><code>net.sf.cglib.Foo$$EnhancerByCGLIB$$38272841</code><p>
 * This is composed of a prefix based on the name of the superclass, a fixed
 * string incorporating the CGLIB class responsible for generation, and a
 * hashcode derived from the parameters used to create the object. If the same
 * name has been previously been used in the same <code>ClassLoader</code>, a
 * suffix is added to ensure uniqueness.
 */
public class DefaultNamingPolicy implements NamingPolicy {
    public static final DefaultNamingPolicy INSTANCE = new DefaultNamingPolicy();
    
    public String getClassName(String prefix, String source, Object key, Predicate names) {
        
        
        StringBuffer sb = new StringBuffer();
        sb.append( 
                  (prefix != null) ? 
                                     ( 
                                      prefix.startsWith("java") ? 
                                                   "$" + prefix : prefix 
                                     )
                                    : "net.sf.cglib.empty.Object"
                 );
        sb.append("$$");
        sb.append(source.substring(source.lastIndexOf('.') + 1));
        sb.append("ByCGLIB$$");
        sb.append(Integer.toHexString(key.hashCode()));
        String base = sb.toString();
        String attempt = base;
        int index = 2;
        while (names.evaluate(attempt)) {
            attempt = base + "_" + index++;
        }
       
        return attempt;
    }
}

@@ -1428,83 +1428,29 @@
     
-%0A        %0A        StringBuffer sb = new StringBuffer();%0A        sb.append( 
+if (prefix == null) %7B
 %0A   
@@ -1462,112 +1462,65 @@
     
-      (
 prefix 
-!
 = 
-null) ? %0A                                     ( %0A                                      
+%22net.sf.cglib.empty.Object%22;%0A        %7D else if (
 pref
@@ -1544,13 +1544,12 @@
 va%22)
- ? %0A 
+) %7B%0A
     
@@ -1560,237 +1560,104 @@
     
-                                      %22$%22 + prefix : prefix %0A                                     )%0A                                    : %22net.sf.cglib.empty.Object%22%0A                 );%0A        sb.append(%22$$%22);%0A        sb.append(
+prefix = %22$%22 + prefix;%0A        %7D%0A        String base =%0A            prefix + %22$$%22 + %0A            
 sour
@@ -1697,27 +1697,31 @@
 .') + 1)
-);%0A
+ +%0A    
         
 sb.appen
@@ -1716,31 +1716,25 @@
     
-sb.append(%22ByCGLIB$$%22);
+getTag() + %22$$%22 +
 %0A   
@@ -1742,18 +1742,12 @@
     
-sb.append(
+    
 Inte
@@ -1781,46 +1781,8 @@
 e())
-);%0A        String base = sb.toString()
 ;%0A  
@@ -1872,18 +1872,16 @@
 ttempt))
- %7B
 %0A       
@@ -1921,26 +1921,8 @@
 ++;%0A
-        %7D%0A       %0A
     
@@ -1946,11 +1946,207 @@
 t;%0A    %7D
+%0A%0A    /**%0A     * Returns a string which is incorporated into every generated class name.%0A     * By default returns %22ByCGLIB%22%0A     */%0A    protected String getTag() %7B%0A        return %22ByCGLIB%22;%0A    %7D
 %0A%7D%0A

add protected getTag method to DefaultNamingPolicy