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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 v 2.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  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. // TODO [j9] passing null client/module here...
  57. NameEnvironmentAnswer answer = nameEnv.findType(cname);
  58. if (answer == null || !answer.isBinaryType()) {
  59. return null;
  60. } else {
  61. IBinaryType binType = answer.getBinaryType();
  62. // XXX - but better than the alternative hacks
  63. if (binType instanceof ClassFileReader) {
  64. ClassFileReader cfr = (ClassFileReader) binType;
  65. cf = new ClassFileReaderBackedClassFile(cfr);
  66. } else {
  67. throw new IllegalArgumentException(
  68. "I'm only geared up to handle ClassFileReaders, and you gave me a " +
  69. binType.getClass().getName());
  70. }
  71. return cf;
  72. }
  73. }
  74. /* (non-Javadoc)
  75. * @see org.aspectj.weaver.bcel.ClassPathManager#getAllClassFiles()
  76. */
  77. public List getAllClassFiles() {
  78. throw new UnsupportedOperationException("I don't implement getAllClassFiles()");
  79. //return Collections.EMPTY_LIST;
  80. }
  81. /* (non-Javadoc)
  82. * @see java.lang.Object#toString()
  83. */
  84. public String toString() {
  85. StringBuilder buf = new StringBuilder("EclipseClassPathManager: ");
  86. buf.append(nameEnv.toString());
  87. return buf.toString();
  88. }
  89. private class ClassFileReaderBackedClassFile extends ClassPathManager.ClassFile {
  90. private ClassFileReader source;
  91. private InputStream is;
  92. public ClassFileReaderBackedClassFile(ClassFileReader cfr) {
  93. source = cfr;
  94. }
  95. /* (non-Javadoc)
  96. * @see org.aspectj.weaver.bcel.ClassPathManager.ClassFile#getInputStream()
  97. */
  98. public InputStream getInputStream() throws IOException {
  99. is = new ByteArrayInputStream(source.getReferenceBytes());
  100. return is;
  101. }
  102. public void close() {
  103. try {
  104. if (is!=null) is.close();
  105. } catch (IOException e) {
  106. // Should never happen !
  107. e.printStackTrace();
  108. }
  109. }
  110. /* (non-Javadoc)
  111. * @see org.aspectj.weaver.bcel.ClassPathManager.ClassFile#getPath()
  112. */
  113. public String getPath() {
  114. return new String(source.getFileName());
  115. }
  116. }
  117. }