KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > inject > util > GuiceNamingPolicy


1 /**
2  * Copyright (C) 2006 Google Inc.
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
17 package com.google.inject.util;
18
19 import net.sf.cglib.core.NamingPolicy;
20 import net.sf.cglib.core.Predicate;
21
22 /**
23  * Cglib class naming policy for Guice.
24  *
25  * @author crazybob@google.com (Bob Lee)
26  */

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