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