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.

EclipseClassPathManager.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*******************************************************************************
  2. * Copyright (c) 2004 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. *******************************************************************************/
  11. package org.aspectj.ajdt.internal.core.builder;
  12. import java.io.ByteArrayInputStream;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.util.List;
  16. import org.aspectj.bridge.IMessageHandler;
  17. import org.aspectj.weaver.UnresolvedType;
  18. import org.aspectj.weaver.bcel.ClassPathManager;
  19. import org.aspectj.org.eclipse.jdt.core.compiler.CharOperation;
  20. import org.aspectj.org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
  21. import org.aspectj.org.eclipse.jdt.internal.compiler.env.IBinaryType;
  22. import org.aspectj.org.eclipse.jdt.internal.compiler.env.INameEnvironment;
  23. import org.aspectj.org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
  24. /**
  25. * @author colyer
  26. *
  27. * Provide a type lookup environment for the weaver, without having to convert
  28. * the various eclipse paths into their external form.
  29. */
  30. public class EclipseClassPathManager extends ClassPathManager {
  31. private INameEnvironment nameEnv;
  32. public EclipseClassPathManager(INameEnvironment env) {
  33. this.nameEnv = env;
  34. }
  35. // class path manager will be used by weaver across multiple compiles,
  36. // whereas a name environment may be constructed per-compile.
  37. public void setNameEnvironment(INameEnvironment env) {
  38. this.nameEnv = env;
  39. }
  40. /* (non-Javadoc)
  41. * @see org.aspectj.weaver.bcel.ClassPathManager#addPath(java.lang.String, org.aspectj.bridge.IMessageHandler)
  42. */
  43. public void addPath(String name, IMessageHandler handler) {
  44. throw new UnsupportedOperationException("Can't add paths to an *Eclipse*ClassPathManager.");
  45. }
  46. /* (non-Javadoc)
  47. * @see org.aspectj.weaver.bcel.ClassPathManager#find(org.aspectj.weaver.UnresolvedType)
  48. */
  49. public ClassFile find(UnresolvedType type) {
  50. ClassFile cf = null;
  51. String name = type.getName();
  52. if (name.endsWith(".class")) {
  53. name = name.substring(0,name.length() - ".class".length());
  54. }
  55. char[][] cname = CharOperation.splitOn('.',name.toCharArray());
  56. NameEnvironmentAnswer answer = nameEnv.findType(cname);
  57. if (answer == null || !answer.isBinaryType()) {
  58. return null;
  59. } else {
  60. IBinaryType binType = answer.getBinaryType();
  61. // XXX - but better than the alternative hacks
  62. if (binType instanceof ClassFileReader) {
  63. ClassFileReader cfr = (ClassFileReader) binType;
  64. cf = new ClassFileReaderBackedClassFile(cfr);
  65. } else {
  66. throw new IllegalArgumentException(
  67. "I'm only geared up to handle ClassFileReaders, and you gave me a " +
  68. binType.getClass().getName());
  69. }
  70. return cf;
  71. }
  72. }
  73. /* (non-Javadoc)
  74. * @see org.aspectj.weaver.bcel.ClassPathManager#getAllClassFiles()
  75. */
  76. public List getAllClassFiles() {
  77. throw new UnsupportedOperationException("I don't implement getAllClassFiles()");
  78. //return Collections.EMPTY_LIST;
  79. }
  80. /* (non-Javadoc)
  81. * @see java.lang.Object#toString()
  82. */
  83. public String toString() {
  84. StringBuffer buf = new StringBuffer("EclipseClassPathManager: ");
  85. buf.append(nameEnv.toString());
  86. return buf.toString();
  87. }
  88. private class ClassFileReaderBackedClassFile extends ClassPathManager.ClassFile {
  89. private ClassFileReader source;
  90. private InputStream is;
  91. public ClassFileReaderBackedClassFile(ClassFileReader cfr) {
  92. source = cfr;
  93. }
  94. /* (non-Javadoc)
  95. * @see org.aspectj.weaver.bcel.ClassPathManager.ClassFile#getInputStream()
  96. */
  97. public InputStream getInputStream() throws IOException {
  98. is = new ByteArrayInputStream(source.getReferenceBytes());
  99. return is;
  100. }
  101. public void close() {
  102. try {
  103. if (is!=null) is.close();
  104. } catch (IOException e) {
  105. // Should never happen !
  106. e.printStackTrace();
  107. }
  108. }
  109. /* (non-Javadoc)
  110. * @see org.aspectj.weaver.bcel.ClassPathManager.ClassFile#getPath()
  111. */
  112. public String getPath() {
  113. return new String(source.getFileName());
  114. }
  115. }
  116. }