KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > eval > CodeSnippetEnvironment


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
22  * An environment that wraps the client's name environment.
23  * This wrapper always considers the wrapped environment then if the name is
24  * not found, it search in the code snippet support. This includes the super class
25  * org.eclipse.jdt.internal.eval.target.CodeSnippet as well as the global variable classes.
26  */

27 public class CodeSnippetEnvironment implements INameEnvironment, EvaluationConstants {
28     INameEnvironment env;
29     EvaluationContext context;
30 /**
31  * Creates a new wrapper for the given environment.
32  */

33 public CodeSnippetEnvironment(INameEnvironment env, EvaluationContext context) {
34     this.env = env;
35     this.context = context;
36 }
37 /**
38  * @see INameEnvironment#findType(char[][])
39  */

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 /*no access restriction*/);
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(); // Should never happen since we compiled this type
63
return null;
64             }
65             return new NameEnvironmentAnswer(binary, null /*no access restriction*/);
66         }
67     }
68     return null;
69 }
70 /**
71  * @see INameEnvironment#findType(char[], char[][])
72  */

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 /**
81  * @see INameEnvironment#isPackage(char[][], char[])
82  */

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