1 11 package org.eclipse.jdt.internal.eval; 12 13 import org.eclipse.jdt.core.compiler.CharOperation; 14 import org.eclipse.jdt.internal.compiler.ClassFile; 15 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader; 16 import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException; 17 import org.eclipse.jdt.internal.compiler.env.IBinaryType; 18 import org.eclipse.jdt.internal.compiler.env.INameEnvironment; 19 import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer; 20 21 27 public class CodeSnippetEnvironment implements INameEnvironment, EvaluationConstants { 28 INameEnvironment env; 29 EvaluationContext context; 30 33 public CodeSnippetEnvironment(INameEnvironment env, EvaluationContext context) { 34 this.env = env; 35 this.context = context; 36 } 37 40 public NameEnvironmentAnswer findType(char[][] compoundTypeName) { 41 NameEnvironmentAnswer result = this.env.findType(compoundTypeName); 42 if (result != null) { 43 return result; 44 } 45 if (CharOperation.equals(compoundTypeName, ROOT_COMPOUND_NAME)) { 46 IBinaryType binary = this.context.getRootCodeSnippetBinary(); 47 if (binary == null) { 48 return null; 49 } else { 50 return new NameEnvironmentAnswer(binary, null ); 51 } 52 } 53 VariablesInfo installedVars = this.context.installedVars; 54 ClassFile[] classFiles = installedVars.classFiles; 55 for (int i = 0; i < classFiles.length; i++) { 56 ClassFile classFile = classFiles[i]; 57 if (CharOperation.equals(compoundTypeName, classFile.getCompoundName())) { 58 ClassFileReader binary = null; 59 try { 60 binary = new ClassFileReader(classFile.getBytes(), null); 61 } catch (ClassFormatException e) { 62 e.printStackTrace(); return null; 64 } 65 return new NameEnvironmentAnswer(binary, null ); 66 } 67 } 68 return null; 69 } 70 73 public NameEnvironmentAnswer findType(char[] typeName, char[][] packageName) { 74 NameEnvironmentAnswer result = this.env.findType(typeName, packageName); 75 if (result != null) { 76 return result; 77 } 78 return findType(CharOperation.arrayConcat(packageName, typeName)); 79 } 80 83 public boolean isPackage(char[][] parentPackageName, char[] packageName) { 84 return this.env.isPackage(parentPackageName, packageName); 85 } 86 public void cleanup() { 87 this.env.cleanup(); 88 } 89 } 90 | Popular Tags |