PageRenderTime 15ms CodeModel.GetById 3ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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