You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OptimizedFullPathHandleProvider.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* *******************************************************************
  2. * Copyright (c) 2003 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Mik Kersten initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.asm.internal;
  13. import java.io.File;
  14. import java.util.HashMap;
  15. import java.util.Iterator;
  16. import java.util.Map;
  17. import java.util.Set;
  18. import java.util.StringTokenizer;
  19. import org.aspectj.asm.AsmManager;
  20. import org.aspectj.asm.IElementHandleProvider;
  21. import org.aspectj.asm.IProgramElement;
  22. import org.aspectj.bridge.ISourceLocation;
  23. /**
  24. * Uses int keys rather than the full file path as the first part of the handle.
  25. */
  26. public class OptimizedFullPathHandleProvider implements IElementHandleProvider {
  27. static final String ID_DELIM = "|";
  28. Map kToF = new HashMap();
  29. int key = 1;
  30. private Integer getKey(String file) {
  31. Integer k = null;
  32. if (kToF.values().contains(file)) {
  33. Set keys = kToF.keySet();
  34. for (Iterator iter = keys.iterator(); iter.hasNext() && k==null;) {
  35. Integer element = (Integer) iter.next();
  36. if (kToF.get(element).equals(file)) {
  37. k = element;
  38. }
  39. }
  40. } else {
  41. k = new Integer(key);
  42. kToF.put(k,file);
  43. key++;
  44. }
  45. return k;
  46. }
  47. public String createHandleIdentifier(ISourceLocation location) {
  48. StringBuffer sb = new StringBuffer();
  49. String file = AsmManager.getDefault().getCanonicalFilePath(location.getSourceFile());
  50. sb.append(getKey(file).intValue());
  51. sb.append(ID_DELIM);
  52. sb.append(location.getLine());
  53. sb.append(ID_DELIM);
  54. sb.append(location.getColumn());
  55. sb.append(ID_DELIM);
  56. sb.append(location.getOffset());
  57. return sb.toString();
  58. }
  59. public String createHandleIdentifier(File sourceFile, int line,int column,int offset) {
  60. StringBuffer sb = new StringBuffer();
  61. sb.append(getKey(AsmManager.getDefault().getCanonicalFilePath(sourceFile)).intValue());
  62. sb.append(ID_DELIM);
  63. sb.append(line);
  64. sb.append(ID_DELIM);
  65. sb.append(column);
  66. sb.append(ID_DELIM);
  67. sb.append(offset);
  68. return sb.toString();
  69. }
  70. public String getFileForHandle(String handle) {
  71. StringTokenizer st = new StringTokenizer(handle, ID_DELIM);
  72. String k = st.nextToken();
  73. return (String)kToF.get(new Integer(k));
  74. // return file;
  75. }
  76. public int getLineNumberForHandle(String handle) {
  77. StringTokenizer st = new StringTokenizer(handle, ID_DELIM);
  78. st.nextToken(); // skip over the file
  79. return new Integer(st.nextToken()).intValue();
  80. }
  81. public int getOffSetForHandle(String handle) {
  82. StringTokenizer st = new StringTokenizer(handle, ID_DELIM);
  83. st.nextToken(); // skip over the file
  84. st.nextToken(); // skip over the line number
  85. st.nextToken(); // skip over the column
  86. return new Integer(st.nextToken()).intValue();
  87. }
  88. public String createHandleIdentifier(IProgramElement ipe) {
  89. if (ipe == null) return null;
  90. if (ipe.getHandleIdentifier(false) != null) {
  91. return ipe.getHandleIdentifier(false);
  92. }
  93. String handle = null;
  94. if (ipe.getSourceLocation() != null) {
  95. handle = createHandleIdentifier(ipe.getSourceLocation());
  96. } else {
  97. handle = createHandleIdentifier(ISourceLocation.NO_FILE,-1,-1,-1);
  98. }
  99. ipe.setHandleIdentifier(handle);
  100. return handle;
  101. }
  102. public boolean dependsOnLocation() {
  103. // handles contain information from the source location therefore
  104. // return true;
  105. return true;
  106. }
  107. public void initialize() {
  108. // nothing to initialize
  109. }
  110. }