PageRenderTime 29ms CodeModel.GetById 18ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/testability-explorer/src/main/java/com/google/test/metric/javasrc/JavaSrcRepository.java

http://testability-explorer.googlecode.com/
Java | 89 lines | 62 code | 12 blank | 15 comment | 6 complexity | 428a198a9682e9556684ad09a98ac4e9 MD5 | raw file
 1/*
 2 * Copyright 2009 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.javasrc;
17
18import java.io.InputStream;
19import java.util.HashMap;
20import java.util.Map;
21
22import antlr.RecognitionException;
23import antlr.TokenStreamException;
24
25import com.google.classpath.ClassPath;
26import com.google.test.metric.ClassInfo;
27import com.google.test.metric.ClassRepository;
28
29public class JavaSrcRepository implements ClassRepository {
30
31  private final ClassPath classPath;
32  private Map<String, ClassInfo> classes = new HashMap<String, ClassInfo>();
33  private final ClassRepository parentRepository;
34
35  public JavaSrcRepository(ClassRepository parentRepository, ClassPath classPath) {
36    this.parentRepository = parentRepository;
37    this.classPath = classPath;
38  }
39
40  public ClassInfo getClass(String clazzName) {
41    ClassInfo info = getCachedClass(clazzName);
42    if (info != null) {
43      return info;
44    }
45    parse(clazzName);
46    info = classes.get(clazzName);
47    if (info != null) {
48      return info;
49    }
50    return parentRepository.getClass(clazzName);
51  }
52
53  private void parse(String clazzName) {
54    String src = clazzName.replace('.', '/').replaceAll("\\$.*", "") + ".java";
55    InputStream srcStream = classPath.getResourceAsStream(src);
56    if (srcStream == null) {
57      return;
58    }
59    JavaLexer lexer = new JavaLexer(srcStream);
60    JavaRecognizer recognizer = new JavaRecognizer(lexer);
61    recognizer.getASTFactory().setASTNodeClass(CommonASTWithLine.class);
62    JavaTreeParser treeParser = new JavaTreeParser();
63    Qualifier qualifier = new Qualifier();
64    treeParser.builder = new CompilationUnitBuilder(this, qualifier, src);
65    try {
66      recognizer.compilationUnit();
67      qualifier.compilationUnit(recognizer.getAST());
68      treeParser.compilationUnit(recognizer.getAST());
69    } catch (RecognitionException e) {
70      throw new RuntimeException(e);
71    } catch (TokenStreamException e) {
72      throw new RuntimeException(e);
73    }
74  }
75
76  @Override
77  public String toString() {
78    return classes.toString();
79  }
80
81  public void addClass(ClassInfo info) {
82    classes.put(info.getName(), info);
83  }
84
85  public ClassInfo getCachedClass(String clazzName) {
86    return classes.get(clazzName);
87  }
88
89}