PageRenderTime 17ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://testability-explorer.googlecode.com/
Java | 146 lines | 103 code | 21 blank | 22 comment | 26 complexity | 9ca235553e2bfc5062df8306ac1d1de9 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.HashMap;
 20import java.util.List;
 21import java.util.Map;
 22
 23import antlr.collections.AST;
 24
 25public class Qualifier {
 26
 27  private String pkg = "";
 28  private List<String> imports = new ArrayList<String>();
 29  private Map<String, String> aliases = new HashMap<String, String>();
 30
 31  public void compilationUnit(AST ast) {
 32    searchAST(ast, "");
 33  }
 34
 35  private void searchAST(AST ast, String prefix) {
 36    while(ast != null) {
 37      if (ast.getType() == JavaTokenTypes.PACKAGE_DEF) {
 38        setPackage(ast.getFirstChild());
 39      } else if (ast.getType() == JavaTokenTypes.IMPORT) {
 40        addImport(stringify(ast.getFirstChild()));
 41      } else if (ast.getType() == JavaTokenTypes.STATIC_IMPORT) {
 42        //System.out.println(ast.toStringList());
 43      } else if (ast.getType() == JavaTokenTypes.CLASS_DEF) {
 44        aliasType(ast.getFirstChild(), prefix);
 45      } else {
 46        searchAST(ast.getFirstChild(), prefix);
 47      }
 48      ast = ast.getNextSibling();
 49    }
 50  }
 51
 52  private void aliasType(AST ast, String prefix) {
 53    String fullName = prefix + ast.getNextSibling().getText();
 54    String qualifiedName = pkg + fullName;
 55    addAlias(fullName, qualifiedName);
 56    searchAST(ast, fullName + "$");
 57  }
 58
 59  private void setPackage(AST ast) {
 60    ast = ast.getNextSibling(); // ANNOTATIONS
 61    setPackage(stringify(ast));
 62  }
 63
 64  public String stringify(AST ast) {
 65    if (ast == null)
 66      return "";
 67    String text = "";
 68    AST child = ast.getFirstChild();
 69    if (child != null)
 70      text += stringify(child);
 71    text += ast.getText();
 72    if (child != null)
 73      text += stringify(child.getNextSibling());
 74    return text;
 75  }
 76
 77  public void setPackage(String pkg) {
 78    this.pkg = pkg +  ".";
 79  }
 80
 81  public String qualify(String context, String ident) {
 82    // Look for inner class first
 83    String[] innerClasses = context.split("\\$");
 84    for (int i = innerClasses.length; i >= 0; --i) {
 85      String innerClass = join(innerClasses, i) + ident.replace('.', '$');
 86      if (aliases.containsKey(innerClass)) {
 87        return aliases.get(innerClass);
 88      }
 89    }
 90
 91    // Check the aliases
 92    if (aliases.containsKey(ident)) {
 93      return aliases.get(ident);
 94    }
 95
 96    // If we already seem to be qualified as we have a dot
 97    if (ident.contains(".")) {
 98      return ident;
 99    }
100
101    // Look through the import statements
102    for (String imprt : imports) {
103      if (imprt.endsWith("." + ident)) {
104        return imprt;
105      }
106    }
107    // Java lang is the default import.
108    try {
109      String javaDefault = "java.lang." + ident;
110      Class.forName(javaDefault);
111      return javaDefault;
112    } catch (ClassNotFoundException e) {
113    }
114
115    // Defaoult to current package.
116    return pkg + ident;
117  }
118
119  private String join(String[] strings, int firstN) {
120    StringBuilder builder = new StringBuilder();
121    for (int i = 0; i < firstN; i++) {
122      builder.append(strings[i]);
123      builder.append("$");
124    }
125    return builder.toString();
126  }
127
128  public void addImport(String imprt) {
129    imports.add(imprt);
130  }
131
132  public void addAlias(String ident, String qualified) {
133    aliases.put(ident, qualified);
134    aliases.put(qualified, qualified);
135    aliases.put(qualified.replace('$', '.'), qualified);
136  }
137
138  public String qualify(String context, AST ast) {
139    return qualify(context, stringify(ast));
140  }
141
142  public String getPackage() {
143    return pkg;
144  }
145
146}