ClassNameReader.java revision 674060f01e9090cd21b3c5656cc3204912ad17a6
1/*
2 * Copyright 2003 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 */
16package org.mockito.cglib.core;
17
18import org.mockito.asm.ClassAdapter;
19import org.mockito.asm.ClassReader;
20
21import java.util.*;
22
23// TODO: optimize (ClassReader buffers entire class before accept)
24public class ClassNameReader {
25    private ClassNameReader() {
26    }
27
28    private static final EarlyExitException EARLY_EXIT = new EarlyExitException();
29    private static class EarlyExitException extends RuntimeException { }
30
31    public static String getClassName(ClassReader r) {
32
33        return getClassInfo(r)[0];
34
35    }
36
37    public static String[] getClassInfo(ClassReader r) {
38        final List array = new ArrayList();
39        try {
40            r.accept(new ClassAdapter(null) {
41                public void visit(int version,
42                                  int access,
43                                  String name,
44                                  String signature,
45                                  String superName,
46                                  String[] interfaces) {
47                    array.add( name.replace('/', '.') );
48                    if(superName != null){
49                      array.add( superName.replace('/', '.') );
50                    }
51                    for(int i = 0; i < interfaces.length; i++  ){
52                       array.add( interfaces[i].replace('/', '.') );
53                    }
54
55                    throw EARLY_EXIT;
56                }
57            }, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
58        } catch (EarlyExitException e) { }
59
60        return (String[])array.toArray( new String[]{} );
61    }
62}
63