KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > services > bytecode > GClass


1 /*
2
3    Derby - Class org.apache.derby.impl.services.bytecode.GClass
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.services.bytecode;
23
24 import org.apache.derby.iapi.services.compiler.ClassBuilder;
25 import org.apache.derby.iapi.services.loader.ClassFactory;
26 import org.apache.derby.iapi.services.loader.GeneratedClass;
27 import org.apache.derby.iapi.error.StandardException;
28 import org.apache.derby.iapi.services.sanity.SanityManager;
29 import org.apache.derby.iapi.services.stream.HeaderPrintWriter;
30 import org.apache.derby.iapi.services.monitor.Monitor;
31
32 import org.apache.derby.iapi.util.ByteArray;
33
34 import java.io.File JavaDoc;
35 import java.io.FileOutputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37
38 /**
39  * This is a common superclass for the various impls.
40  * Saving class files is a common thing to do.
41  *
42  * @author ames
43  */

44 public abstract class GClass implements ClassBuilder {
45
46     protected ByteArray bytecode;
47     protected final ClassFactory cf;
48     protected final String JavaDoc qualifiedName;
49
50
51
52     public GClass(ClassFactory cf, String JavaDoc qualifiedName) {
53         this.cf = cf;
54         this.qualifiedName = qualifiedName;
55     }
56     public String JavaDoc getFullName() {
57         return qualifiedName;
58     }
59     public GeneratedClass getGeneratedClass() throws StandardException {
60         return cf.loadGeneratedClass(qualifiedName, getClassBytecode());
61     }
62
63     protected void writeClassFile(String JavaDoc dir, boolean logMessage, Throwable JavaDoc t)
64         throws StandardException {
65
66         if (SanityManager.DEBUG) {
67
68         if (bytecode == null) getClassBytecode(); // not recursing...
69

70         if (dir == null) dir="";
71
72         String JavaDoc filename = getName(); // leave off package
73

74         filename = filename + ".class";
75
76         File JavaDoc classFile = new File JavaDoc(dir,filename);
77
78         // find the error stream
79
HeaderPrintWriter errorStream = Monitor.getStream();
80
81         try {
82             FileOutputStream JavaDoc fis = new FileOutputStream JavaDoc(classFile);
83             fis.write(bytecode.getArray(),
84                 bytecode.getOffset(), bytecode.getLength());
85             fis.flush();
86             if (logMessage) {
87                 errorStream.printlnWithHeader("Wrote class "+getFullName()+" to file "+classFile.toString()+". Please provide support with the file and the following exception message: "+t);
88             }
89             fis.close();
90         } catch (IOException JavaDoc e) {
91             if (SanityManager.DEBUG)
92                 SanityManager.THROWASSERT("Unable to write .class file");
93         }
94         }
95     }
96
97     final void validateType(String JavaDoc typeName1)
98     {
99         if (SanityManager.DEBUG)
100         {
101             SanityManager.ASSERT(typeName1 != null);
102
103             String JavaDoc typeName = typeName1.trim();
104
105             if ("void".equals(typeName)) return;
106
107             // first remove all array-ness
108
while (typeName.endsWith("[]")) typeName = typeName.substring(0,typeName.length()-2);
109
110             SanityManager.ASSERT(typeName.length() > 0);
111
112             // then check for primitive types
113
if ("boolean".equals(typeName)) return;
114             if ("byte".equals(typeName)) return;
115             if ("char".equals(typeName)) return;
116             if ("double".equals(typeName)) return;
117             if ("float".equals(typeName)) return;
118             if ("int".equals(typeName)) return;
119             if ("long".equals(typeName)) return;
120             if ("short".equals(typeName)) return;
121
122             // then see if it can be found
123
// REVISIT: this will fail if ASSERT is on and the
124
// implementation at hand is missing the target type.
125
// We do plan to generate classes against
126
// different implementations from the compiler's implementation
127
// at some point...
128
try {
129                 if (cf == null)
130                     Class.forName(typeName);
131                 else
132                     cf.loadApplicationClass(typeName);
133             } catch (ClassNotFoundException JavaDoc cnfe) {
134                 SanityManager.THROWASSERT("Class "+typeName+" not found");
135             }
136
137             // all the checks succeeded, it must be okay.
138
return;
139         }
140     }
141 }
142
Popular Tags