PageRenderTime 32ms CodeModel.GetById 11ms app.highlight 16ms RepoModel.GetById 2ms app.codeStats 0ms

/testability-explorer/src/main/java/com/google/test/metric/asm/ClassInfoBuilderVisitor.java

http://testability-explorer.googlecode.com/
Java | 85 lines | 56 code | 14 blank | 15 comment | 6 complexity | 8ac612918b856209e33e54ca9f5c2383 MD5 | raw file
 1/*
 2 * Copyright 2007 Google Inc.
 3 *
 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 5 * use this file except in compliance with the License. You may obtain a copy of
 6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16package com.google.test.metric.asm;
17
18import java.util.ArrayList;
19import java.util.List;
20
21import org.objectweb.asm.FieldVisitor;
22import org.objectweb.asm.MethodVisitor;
23import org.objectweb.asm.Opcodes;
24
25import com.google.test.metric.ClassInfo;
26import com.google.test.metric.JavaClassRepository;
27
28public class ClassInfoBuilderVisitor extends NoopClassVisitor {
29
30  private final JavaClassRepository repository;
31  private ClassInfo classInfo;
32  private JavaNamer namer = new JavaNamer();
33
34  public ClassInfoBuilderVisitor(JavaClassRepository repository) {
35    this.repository = repository;
36  }
37
38  @Override
39  public void visit(int version, int access, String name, String signature,
40      String superName, String[] interfaces) {
41    ClassInfo superClass = null;
42    superClass = superName == null ? null : repository.getClass(namer.nameClass(superName));
43
44    List<ClassInfo> interfaceList = new ArrayList<ClassInfo>();
45    for (String interfaze : interfaces) {
46      interfaceList.add(repository.getClass(namer.nameClass(interfaze)));
47    }
48    boolean isInterface = (access & Opcodes.ACC_INTERFACE) == Opcodes.ACC_INTERFACE;
49    classInfo = new ClassInfo(namer.nameClass(name), isInterface, superClass,
50        interfaceList, guessSourceFileName(name));
51    repository.addClass(classInfo);
52  }
53
54  public String guessSourceFileName(String className) {
55    className = className.replace('.', '/');
56
57    int internalClassDelim = className.indexOf('$');
58    if (internalClassDelim > -1) {
59      className = className.substring(0, internalClassDelim);
60    }
61    return className + ".java";
62  }
63
64  @Override
65  public MethodVisitor visitMethod(int access, String name, String desc,
66      String signature, String[] exceptions) {
67    boolean isStatic = (access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC;
68    boolean isFinal = (access & Opcodes.ACC_FINAL) == Opcodes.ACC_FINAL;
69    return new MethodVisitorBuilder(repository, classInfo, name, desc,
70        signature, exceptions, isStatic, isFinal, JavaVisibility
71            .valueFromJavaBytecode(access));
72  }
73
74  @Override
75  public FieldVisitor visitField(int access, String name, String desc,
76      String signature, Object value) {
77    return new FieldVisitorBuilder(classInfo, access, name, desc, signature,
78        value);
79  }
80
81  public ClassInfo getClassInfo() {
82    return classInfo;
83  }
84
85}