KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > model > ClassNameRewriterUtil


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2005, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.model;
21
22 import java.util.Iterator JavaDoc;
23
24 import edu.umd.cs.findbugs.FieldAnnotation;
25 import edu.umd.cs.findbugs.MethodAnnotation;
26 import edu.umd.cs.findbugs.ba.SignatureParser;
27
28 /**
29  * Utility methods for using a ClassNameRewriter.
30  *
31  * @author David Hovemeyer
32  */

33 public abstract class ClassNameRewriterUtil {
34     
35     /**
36      * Rewrite a method signature.
37      *
38      * @param classNameRewriter a ClassNameRewriter
39      * @param methodSignature a method signature
40      * @return the rewritten method signature
41      */

42     public static String JavaDoc rewriteMethodSignature(ClassNameRewriter classNameRewriter, String JavaDoc methodSignature) {
43         if (classNameRewriter != IdentityClassNameRewriter.instance()) {
44             SignatureParser parser = new SignatureParser(methodSignature);
45             
46             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
47             
48             buf.append('(');
49             for (Iterator JavaDoc<String JavaDoc> i = parser.parameterSignatureIterator(); i.hasNext();) {
50                 buf.append(rewriteSignature(classNameRewriter, i.next()));
51             }
52             
53             buf.append(')');
54             buf.append(rewriteSignature(classNameRewriter, parser.getReturnTypeSignature()));
55             
56             methodSignature = buf.toString();
57         }
58         
59         return methodSignature;
60     }
61     
62     /**
63      * Rewrite a signature.
64      *
65      * @param classNameRewriter a ClassNameRewriter
66      * @param signature a signature (parameter, return type, or field)
67      * @return rewritten signature with class name updated if required
68      */

69     public static String JavaDoc rewriteSignature(ClassNameRewriter classNameRewriter, String JavaDoc signature) {
70         if (classNameRewriter != IdentityClassNameRewriter.instance()
71                 && signature.startsWith("L")) {
72         
73             String JavaDoc className = signature.substring(1, signature.length() - 1).replace('/', '.');
74             className = classNameRewriter.rewriteClassName(className);
75             
76             signature = "L" + className.replace('.', '/') + ";";
77         }
78         
79         return signature;
80     }
81
82     /**
83      * Rewrite a MethodAnnotation to update the class name,
84      * and any class names mentioned in the method signature.
85      *
86      * @param classNameRewriter a ClassNameRewriter
87      * @param annotation a MethodAnnotation
88      * @return the possibly-rewritten MethodAnnotation
89      */

90     public static MethodAnnotation convertMethodAnnotation(ClassNameRewriter classNameRewriter, MethodAnnotation annotation) {
91
92         if (classNameRewriter != IdentityClassNameRewriter.instance()) {
93             annotation = new MethodAnnotation(
94                     classNameRewriter.rewriteClassName(annotation.getClassName()),
95                     annotation.getMethodName(),
96                     rewriteMethodSignature(classNameRewriter, annotation.getMethodSignature()),
97                     annotation.isStatic());
98         }
99         return annotation;
100     }
101
102     /**
103      * Rewrite a FieldAnnotation to update the class name
104      * and field signature, if needed.
105      *
106      * @param classNameRewriter a ClassNameRewriter
107      * @param annotation a FieldAnnotation
108      * @return the possibly-rewritten FieldAnnotation
109      */

110     public static FieldAnnotation convertFieldAnnotation(ClassNameRewriter classNameRewriter, FieldAnnotation annotation) {
111         
112         if (classNameRewriter != IdentityClassNameRewriter.instance()) {
113             annotation = new FieldAnnotation(
114                     classNameRewriter.rewriteClassName(annotation.getClassName()),
115                     annotation.getFieldName(),
116                     rewriteSignature(classNameRewriter, annotation.getFieldSignature()),
117                     annotation.isStatic());
118         }
119         return annotation;
120     }
121
122 }
123
Popular Tags