1 11 package org.eclipse.jdt.internal.core; 12 13 import org.eclipse.jdt.core.IField; 14 import org.eclipse.jdt.core.IMethod; 15 import org.eclipse.jdt.core.IType; 16 import org.eclipse.jdt.core.JavaModelException; 17 import org.eclipse.jdt.core.Signature; 18 import org.eclipse.jdt.core.compiler.CharOperation; 19 import org.eclipse.jdt.internal.compiler.CompilationResult; 20 import org.eclipse.jdt.internal.compiler.ast.ASTNode; 21 import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; 22 import org.eclipse.jdt.internal.compiler.ast.Argument; 23 import org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference; 24 import org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference; 25 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; 26 import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration; 27 import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; 28 import org.eclipse.jdt.internal.compiler.ast.ImportReference; 29 import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration; 30 import org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference; 31 import org.eclipse.jdt.internal.compiler.ast.SingleTypeReference; 32 import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; 33 import org.eclipse.jdt.internal.compiler.ast.TypeReference; 34 import org.eclipse.jdt.internal.compiler.lookup.CompilerModifiers; 35 36 39 public class TypeConverter { 40 41 44 public static TypeDeclaration buildTypeDeclaration(IType type, CompilationUnitDeclaration compilationUnit, CompilationResult compilationResult) throws JavaModelException { 45 char[] packageName = type.getPackageFragment().getElementName().toCharArray(); 46 47 if (packageName != null && packageName.length > 0) { 48 compilationUnit.currentPackage = new ImportReference(CharOperation.splitOn('.', packageName), new long[]{0}, false, CompilerModifiers.AccDefault); 49 } 50 51 52 TypeDeclaration typeDeclaration = convert(type, null, null, compilationResult); 53 54 IType alreadyComputedMember = type; 55 IType parent = type.getDeclaringType(); 56 TypeDeclaration previousDeclaration = typeDeclaration; 57 while(parent != null) { 58 TypeDeclaration declaration = convert(parent, alreadyComputedMember, previousDeclaration, compilationResult); 59 60 alreadyComputedMember = parent; 61 previousDeclaration = declaration; 62 parent = parent.getDeclaringType(); 63 } 64 65 compilationUnit.types = new TypeDeclaration[]{previousDeclaration}; 66 67 return typeDeclaration; 68 } 69 70 private static FieldDeclaration convert(IField field, IType type) throws JavaModelException { 71 72 FieldDeclaration fieldDeclaration = new FieldDeclaration(); 73 74 fieldDeclaration.name = field.getElementName().toCharArray(); 75 fieldDeclaration.type = createTypeReference(Signature.toString(field.getTypeSignature()).toCharArray(), type); 76 fieldDeclaration.modifiers = field.getFlags(); 77 78 return fieldDeclaration; 79 } 80 81 private static AbstractMethodDeclaration convert(IMethod method, IType type, CompilationResult compilationResult) throws JavaModelException { 82 83 AbstractMethodDeclaration methodDeclaration; 84 85 if (method.isConstructor()) { 86 ConstructorDeclaration decl = new ConstructorDeclaration(compilationResult); 87 decl.isDefaultConstructor = false; 88 methodDeclaration = decl; 89 } else { 90 MethodDeclaration decl = new MethodDeclaration(compilationResult); 91 92 decl.returnType = createTypeReference(Signature.toString(method.getReturnType()).toCharArray(), type); 93 methodDeclaration = decl; 94 } 95 methodDeclaration.selector = method.getElementName().toCharArray(); 96 methodDeclaration.modifiers = method.getFlags(); 97 98 99 String [] argumentTypeNames = method.getParameterTypes(); 100 String [] argumentNames = method.getParameterNames(); 101 int argumentCount = argumentTypeNames == null ? 0 : argumentTypeNames.length; 102 methodDeclaration.arguments = new Argument[argumentCount]; 103 for (int i = 0; i < argumentCount; i++) { 104 methodDeclaration.arguments[i] = new Argument( 105 argumentNames[i].toCharArray(), 106 0, 107 createTypeReference(Signature.toString(argumentTypeNames[i]).toCharArray(), type), 108 CompilerModifiers.AccDefault); 109 } 111 112 113 String [] exceptionTypeNames = method.getExceptionTypes(); 114 int exceptionCount = exceptionTypeNames == null ? 0 : exceptionTypeNames.length; 115 if(exceptionCount > 0) { 116 methodDeclaration.thrownExceptions = new TypeReference[exceptionCount]; 117 for (int i = 0; i < exceptionCount; i++) { 118 methodDeclaration.thrownExceptions[i] = 119 createTypeReference(Signature.toString(exceptionTypeNames[i]).toCharArray(), type); 120 } 121 } 122 return methodDeclaration; 123 } 124 125 private static TypeDeclaration convert(IType type, IType alreadyComputedMember,TypeDeclaration alreadyComputedMemberDeclaration, CompilationResult compilationResult) throws JavaModelException { 126 127 TypeDeclaration typeDeclaration = new TypeDeclaration(compilationResult); 128 129 if (type.getDeclaringType() != null) { 130 typeDeclaration.bits |= ASTNode.IsMemberTypeMASK; 131 } 132 typeDeclaration.name = type.getElementName().toCharArray(); 133 typeDeclaration.modifiers = type.getFlags(); 134 135 136 137 if (type.getSuperclassName() != null) { 138 typeDeclaration.superclass = createTypeReference(type.getSuperclassName().toCharArray(), type); 139 } 140 String [] interfaceNames = type.getSuperInterfaceNames(); 141 int interfaceCount = interfaceNames == null ? 0 : interfaceNames.length; 142 typeDeclaration.superInterfaces = new TypeReference[interfaceCount]; 143 for (int i = 0; i < interfaceCount; i++) { 144 typeDeclaration.superInterfaces[i] = createTypeReference(interfaceNames[i].toCharArray(), type); 145 } 146 147 148 IType[] memberTypes = type.getTypes(); 149 int memberTypeCount = memberTypes == null ? 0 : memberTypes.length; 150 typeDeclaration.memberTypes = new TypeDeclaration[memberTypeCount]; 151 for (int i = 0; i < memberTypeCount; i++) { 152 if(alreadyComputedMember != null && alreadyComputedMember.getFullyQualifiedName().equals(memberTypes[i].getFullyQualifiedName())) { 153 typeDeclaration.memberTypes[i] = alreadyComputedMemberDeclaration; 154 } else { 155 typeDeclaration.memberTypes[i] = convert(memberTypes[i], null, null, compilationResult); 156 } 157 } 158 159 160 IField[] fields = type.getFields(); 161 int fieldCount = fields == null ? 0 : fields.length; 162 typeDeclaration.fields = new FieldDeclaration[fieldCount]; 163 for (int i = 0; i < fieldCount; i++) { 164 typeDeclaration.fields[i] = convert(fields[i], type); 165 } 166 167 168 IMethod[] methods = type.getMethods(); 169 int methodCount = methods == null ? 0 : methods.length; 170 171 172 173 int neededCount = 1; 174 for (int i = 0; i < methodCount; i++) { 175 if (methods[i].isConstructor()) { 176 neededCount = 0; 177 break; 179 } 180 } 181 boolean isInterface = type.isInterface(); 182 neededCount = isInterface ? 0 : neededCount; 183 typeDeclaration.methods = new AbstractMethodDeclaration[methodCount + neededCount]; 184 if (neededCount != 0) { typeDeclaration.methods[0] = typeDeclaration.createsInternalConstructor(false, false); 186 } 187 for (int i = 0; i < methodCount; i++) { 188 AbstractMethodDeclaration method =convert(methods[i], type, compilationResult); 189 if (isInterface || method.isAbstract()) { method.modifiers |= CompilerModifiers.AccSemicolonBody; 191 } 192 typeDeclaration.methods[neededCount + i] = method; 193 } 194 return typeDeclaration; 195 } 196 197 private static TypeReference createTypeReference(char[] type, IType contextType) { 198 try { 199 String [][] resolvedName = contextType.resolveType(new String (type)); 200 if(resolvedName != null && resolvedName.length == 1) { 201 type= CharOperation.concat(resolvedName[0][0].toCharArray(), resolvedName[0][1].toCharArray(), '.'); 202 } 203 } catch (JavaModelException e) { 204 } 206 207 208 int max = type.length; 209 int dimStart = max; 210 int dim = 0; 211 int identCount = 1; 212 for (int i = 0; i < max; i++) { 213 switch (type[i]) { 214 case '[' : 215 if (dim == 0) 216 dimStart = i; 217 dim++; 218 break; 219 case '.' : 220 identCount++; 221 break; 222 } 223 } 224 225 if (identCount == 1) { if (dim == 0) { 227 return new SingleTypeReference(type, 0); 228 } else { 229 char[] identifier = new char[dimStart]; 230 System.arraycopy(type, 0, identifier, 0, dimStart); 231 return new ArrayTypeReference(identifier, dim, 0); 232 } 233 } else { char[][] identifiers = CharOperation.splitOn('.', type, 0, dimStart); 235 if (dim == 0) { 236 return new QualifiedTypeReference(identifiers, new long[]{0}); 237 } else { 238 return new ArrayQualifiedTypeReference(identifiers, dim, new long[]{0}); 239 } 240 } 241 } 242 } 243 | Popular Tags |