1 11 package org.eclipse.jdt.internal.eval; 12 13 import org.eclipse.jdt.core.compiler.CategorizedProblem; 14 import org.eclipse.jdt.internal.compiler.ClassFile; 15 import org.eclipse.jdt.internal.compiler.CompilationResult; 16 import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; 17 import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; 18 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; 19 import org.eclipse.jdt.internal.compiler.codegen.CodeStream; 20 import org.eclipse.jdt.internal.compiler.codegen.ConstantPool; 21 import org.eclipse.jdt.internal.compiler.codegen.StackMapFrameCodeStream; 22 import org.eclipse.jdt.internal.compiler.lookup.Binding; 23 import org.eclipse.jdt.internal.compiler.lookup.FieldBinding; 24 import org.eclipse.jdt.internal.compiler.lookup.MethodBinding; 25 import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding; 26 import org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding; 27 28 public class CodeSnippetClassFile extends ClassFile { 29 35 public CodeSnippetClassFile( 36 org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding aType, 37 org.eclipse.jdt.internal.compiler.ClassFile enclosingClassFile, 38 boolean creatingProblemType) { 39 47 this.referenceBinding = aType; 48 initByteArrays(); 49 this.header[this.headerOffset++] = (byte) (0xCAFEBABEL >> 24); 51 this.header[this.headerOffset++] = (byte) (0xCAFEBABEL >> 16); 52 this.header[this.headerOffset++] = (byte) (0xCAFEBABEL >> 8); 53 this.header[this.headerOffset++] = (byte) (0xCAFEBABEL >> 0); 54 55 this.targetJDK = this.referenceBinding.scope.compilerOptions().targetJDK; 56 this.header[this.headerOffset++] = (byte) (targetJDK >> 8); this.header[this.headerOffset++] = (byte) (targetJDK >> 0); this.header[this.headerOffset++] = (byte) (targetJDK >> 24); this.header[this.headerOffset++] = (byte) (targetJDK >> 16); 61 this.constantPoolOffset = this.headerOffset; 62 this.headerOffset += 2; 63 this.constantPool = new ConstantPool(this); 64 int accessFlags = aType.getAccessFlags(); 65 66 if (!aType.isInterface()) { accessFlags |= ClassFileConstants.AccSuper; 68 } 69 if (aType.isNestedType()) { 70 if (aType.isStatic()) { 71 accessFlags &= ~ClassFileConstants.AccStatic; 73 } 74 if (aType.isPrivate()) { 75 accessFlags &= ~(ClassFileConstants.AccPrivate | ClassFileConstants.AccPublic); 77 } 78 if (aType.isProtected()) { 79 accessFlags &= ~ClassFileConstants.AccProtected; 81 accessFlags |= ClassFileConstants.AccPublic; 82 } 83 } 84 accessFlags &= ~ClassFileConstants.AccStrictfp; 86 87 this.enclosingClassFile = enclosingClassFile; 88 this.contents[this.contentsOffset++] = (byte) (accessFlags >> 8); 90 this.contents[this.contentsOffset++] = (byte) accessFlags; 91 int classNameIndex = this.constantPool.literalIndexForType(aType); 92 this.contents[this.contentsOffset++] = (byte) (classNameIndex >> 8); 93 this.contents[this.contentsOffset++] = (byte) classNameIndex; 94 int superclassNameIndex; 95 if (aType.isInterface()) { 96 superclassNameIndex = this.constantPool.literalIndexForType(ConstantPool.JavaLangObjectConstantPoolName); 97 } else { 98 superclassNameIndex = 99 (aType.superclass == null ? 0 : this.constantPool.literalIndexForType(aType.superclass)); 100 } 101 this.contents[this.contentsOffset++] = (byte) (superclassNameIndex >> 8); 102 this.contents[this.contentsOffset++] = (byte) superclassNameIndex; 103 ReferenceBinding[] superInterfacesBinding = aType.superInterfaces(); 104 int interfacesCount = superInterfacesBinding.length; 105 this.contents[this.contentsOffset++] = (byte) (interfacesCount >> 8); 106 this.contents[this.contentsOffset++] = (byte) interfacesCount; 107 for (int i = 0; i < interfacesCount; i++) { 108 int interfaceIndex = this.constantPool.literalIndexForType(superInterfacesBinding[i]); 109 this.contents[this.contentsOffset++] = (byte) (interfaceIndex >> 8); 110 this.contents[this.contentsOffset++] = (byte) interfaceIndex; 111 } 112 this.produceAttributes = this.referenceBinding.scope.compilerOptions().produceDebugAttributes; 113 this.creatingProblemType = creatingProblemType; 114 if (this.targetJDK >= ClassFileConstants.JDK1_6) { 115 this.codeStream = new StackMapFrameCodeStream(this); 116 this.produceAttributes |= ClassFileConstants.ATTR_STACK_MAP; 117 } else { 118 this.codeStream = new CodeStream(this); 119 } 120 if (this.enclosingClassFile == null) { 123 this.codeStream.maxFieldCount = aType.scope.referenceType().maxFieldCount; 124 } else { 125 ClassFile outermostClassFile = this.outerMostEnclosingClassFile(); 126 this.codeStream.maxFieldCount = outermostClassFile.codeStream.maxFieldCount; 127 } 128 } 129 136 public static void createProblemType(TypeDeclaration typeDeclaration, CompilationResult unitResult) { 137 SourceTypeBinding typeBinding = typeDeclaration.binding; 138 ClassFile classFile = new CodeSnippetClassFile(typeBinding, null, true); 139 140 if (typeBinding.isNestedType()) { 142 classFile.recordInnerClasses(typeBinding); 143 } 144 145 FieldBinding[] fields = typeBinding.fields(); 147 if ((fields != null) && (fields != Binding.NO_FIELDS)) { 148 classFile.addFieldInfos(); 149 } else { 150 classFile.contents[classFile.contentsOffset++] = 0; 152 classFile.contents[classFile.contentsOffset++] = 0; 153 } 154 classFile.setForMethodInfos(); 156 int problemsLength; 158 CategorizedProblem[] problems = unitResult.getErrors(); 159 if (problems == null) { 160 problems = new CategorizedProblem[0]; 161 } 162 CategorizedProblem[] problemsCopy = new CategorizedProblem[problemsLength = problems.length]; 163 System.arraycopy(problems, 0, problemsCopy, 0, problemsLength); 164 AbstractMethodDeclaration[] methodDecls = typeDeclaration.methods; 165 if (methodDecls != null) { 166 if (typeBinding.isInterface()) { 167 classFile.addProblemClinit(problemsCopy); 170 for (int i = 0, length = methodDecls.length; i < length; i++) { 171 AbstractMethodDeclaration methodDecl = methodDecls[i]; 172 MethodBinding method = methodDecl.binding; 173 if (method == null || method.isConstructor()) continue; 174 classFile.addAbstractMethod(methodDecl, method); 175 } 176 } else { 177 for (int i = 0, length = methodDecls.length; i < length; i++) { 178 AbstractMethodDeclaration methodDecl = methodDecls[i]; 179 MethodBinding method = methodDecl.binding; 180 if (method == null) continue; 181 if (method.isConstructor()) { 182 classFile.addProblemConstructor(methodDecl, method, problemsCopy); 183 } else { 184 classFile.addProblemMethod(methodDecl, method, problemsCopy); 185 } 186 } 187 } 188 classFile.addDefaultAbstractMethods(); 190 } 191 if (typeDeclaration.memberTypes != null) { 193 for (int i = 0, max = typeDeclaration.memberTypes.length; i < max; i++) { 194 TypeDeclaration memberType = typeDeclaration.memberTypes[i]; 195 if (memberType.binding != null) { 196 ClassFile.createProblemType(memberType, unitResult); 197 } 198 } 199 } 200 classFile.addAttributes(); 201 unitResult.record(typeBinding.constantPoolName(), classFile); 202 } 203 } 204 | Popular Tags |