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

http://testability-explorer.googlecode.com/ · Java · 112 lines · 78 code · 19 blank · 15 comment · 11 complexity · d7edd29f0141e8910de6de57b4129866 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.util.ArrayList;
  18. import java.util.List;
  19. import java.util.Stack;
  20. import antlr.collections.AST;
  21. import com.google.test.metric.ClassInfo;
  22. import com.google.test.metric.JavaType;
  23. import com.google.test.metric.Type;
  24. import com.google.test.metric.Visibility;
  25. public class CompilationUnitBuilder {
  26. private final JavaSrcRepository repository;
  27. private final Stack<TypeBuilder> typeStack = new Stack<TypeBuilder>();
  28. private final Qualifier qualifier;
  29. private final String src;
  30. public TypeBuilder type;
  31. public CompilationUnitBuilder(JavaSrcRepository repository,
  32. Qualifier qualifier, String src) {
  33. this.repository = repository;
  34. this.qualifier = qualifier;
  35. this.src = src;
  36. }
  37. public void startType(int line, String name, Type superType,
  38. List<Type> interfaceTypes) {
  39. String className = (type == null ? qualifier.getPackage() : type.getName() + "$") + name;
  40. boolean isInterface = false;
  41. List<ClassInfo> interfaceClasses = new ArrayList<ClassInfo>();
  42. for (Type interfaceType : interfaceTypes) {
  43. interfaceClasses.add(toClassInfo(interfaceType));
  44. }
  45. ClassInfo info = new ClassInfo(className,
  46. isInterface, toClassInfo(superType), interfaceClasses, src);
  47. repository.addClass(info);
  48. pushType(new TypeBuilder(info));
  49. }
  50. private String getContext() {
  51. return this.type == null ? "" : this.type.getName();
  52. }
  53. private void pushType(TypeBuilder typeBuilder) {
  54. typeStack.push(type);
  55. type = typeBuilder;
  56. }
  57. public void endType() {
  58. type = typeStack.pop();
  59. }
  60. public Type toType(AST ast) {
  61. return JavaType.fromJava(qualifier.qualify(getContext(), ast));
  62. }
  63. public ClassInfo toClassInfo(Type type) {
  64. if (type == null) {
  65. return null;
  66. }
  67. return repository.getClass(type.toString());
  68. }
  69. public Visibility visibility(AST modifiers) {
  70. if (contains(modifiers, "private"))
  71. return Visibility.PRIVATE;
  72. if (contains(modifiers, "protected"))
  73. return Visibility.PROTECTED;
  74. if (contains(modifiers, "public"))
  75. return Visibility.PUBLIC;
  76. return Visibility.PACKAGE;
  77. }
  78. public boolean isStatic(AST modifiers) {
  79. return contains(modifiers, "static");
  80. }
  81. public boolean isFinal(AST modifiers) {
  82. return contains(modifiers, "final");
  83. }
  84. private boolean contains(AST modifiers, String text) {
  85. AST modifier = modifiers.getFirstChild();
  86. while (modifier != null) {
  87. if (modifier.getText().equals(text))
  88. return true;
  89. modifier = modifier.getNextSibling();
  90. }
  91. return false;
  92. }
  93. }