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.

StringToType.java 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* *******************************************************************
  2. * Copyright (c) 2005 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://eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Adrian Colyer Initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.internal.lang.reflect;
  13. import java.lang.reflect.ParameterizedType;
  14. import java.lang.reflect.Type;
  15. import java.lang.reflect.TypeVariable;
  16. import java.util.StringTokenizer;
  17. import org.aspectj.lang.reflect.AjTypeSystem;
  18. /**
  19. * @author colyer
  20. * Helper class for converting type representations in Strings into java.lang.reflect.Types.
  21. */
  22. public class StringToType {
  23. public static Type[] commaSeparatedListToTypeArray(String typeNames, Class classScope)
  24. throws ClassNotFoundException {
  25. StringTokenizer strTok = new StringTokenizer(typeNames,",");
  26. Type[] ret = new Type[strTok.countTokens()];
  27. int index = 0;
  28. //outer:
  29. while (strTok.hasMoreTokens()) {
  30. String typeName = strTok.nextToken().trim();
  31. ret[index++] = stringToType(typeName, classScope);
  32. }
  33. return ret;
  34. }
  35. public static Type stringToType(String typeName, Class classScope)
  36. throws ClassNotFoundException {
  37. try {
  38. if (!typeName.contains("<")) {
  39. return AjTypeSystem.getAjType(Class.forName(typeName,false,classScope.getClassLoader()));
  40. } else {
  41. return makeParameterizedType(typeName,classScope);
  42. }
  43. } catch (ClassNotFoundException e) {
  44. // could be a type variable
  45. TypeVariable[] tVars = classScope.getTypeParameters();
  46. for (TypeVariable tVar : tVars) {
  47. if (tVar.getName().equals(typeName)) {
  48. return tVar;
  49. }
  50. }
  51. throw new ClassNotFoundException(typeName);
  52. }
  53. }
  54. private static Type makeParameterizedType(String typeName, Class classScope)
  55. throws ClassNotFoundException {
  56. int paramStart = typeName.indexOf('<');
  57. String baseName = typeName.substring(0, paramStart);
  58. final Class baseClass = Class.forName(baseName,false,classScope.getClassLoader());
  59. int paramEnd = typeName.lastIndexOf('>');
  60. String params = typeName.substring(paramStart+1,paramEnd);
  61. final Type[] typeParams = commaSeparatedListToTypeArray(params,classScope);
  62. return new ParameterizedType() {
  63. public Type[] getActualTypeArguments() {
  64. return typeParams;
  65. }
  66. public Type getRawType() {
  67. return baseClass;
  68. }
  69. public Type getOwnerType() {
  70. return baseClass.getEnclosingClass();
  71. }
  72. };
  73. }
  74. }