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