1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
/*
Derby - Class org.apache.derby.iapi.services.cache.ClassSizeCrawler
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.derbyBuild;
import org.apache.derby.iapi.services.cache.ClassSize;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Enumeration;
/**
* This class implements a program that catalogs the size estimate coefficients of various classes.
* @see ClassSize#getSizeCoefficients.
*<p>
* The program is invoked as:
*<p>
* java -DWS=<i>work-space</i> [-DclassDir=<i>class-dir</i>] [-Dout=<i>out-file</i> [-Dprefix[.<i>x</i>=<i>package-prefix</i>]] [-Dverbose=true] org.apache.derby.iapi.services.cache.ClassSizeCrawler <i>class-or-interface</i> ...<br>
*<p>
* This program gets the size coefficients for each class in the <i>class-or-interface</i> list,
* and for each class that implements an interface in the list. If there is an interface in the list
* this program crawls through the classes hierarcy, starting at points specified by the prefix
* properties, looking for classes that implement the interfaces.
*<p>
* If the <i>class-or-interface</i> list is empty then this program searches for implementations
* of org.apache.derby.iapi.types.DataValueDescriptor, and at least one prefix property
* must be specified
*<p>
* The catalog is written as a java source file
* into <i>out-file</i>, by default
* <i>work-space</i>/java/org.apache.derby.iapi.services.cache.ClassSizeCatalogImpl.java.
*<p>
* <i>work-space</i> is the directory containing the java and classes directories. $WS in the
* standard development environment. This property is required.
*<p>
* <i>class-dir</i> is the directory containing the compiled classes. By default it is <i>work-space</i>/classes.
*<p>
* <i>package-prefix</i> is the first part of a package name. e.g. "com.ibm.db2j.impl". At least
* one prefix property must be specified if there is an interface in the list.
*<p>
* For example:<br>
* <pre>
* <code>
* java -DWS=$WS \
* -Dprefix.1=org.apache.derby.iapi.types \
* org.apache.derby.iapi.services.cache.ClassSizeCrawler \
* org.apache.derby.iapi.types.DataValueDescriptor \
* java.math.BigDecimal \
* org.apache.derby.impl.services.cache.Generic.CachedItem
*</code>
*</pre>
*/
public class ClassSizeCrawler
{
public static void main( String[] arg)
{
String[] classAndInterfaceList = {"org.apache.derby.iapi.types.DataValueDescriptor"};
if(arg.length > 0)
classAndInterfaceList = arg;
Class[] interfaceList = new Class[classAndInterfaceList.length];
int interfaceCount = 0;
Class[] classList = new Class[classAndInterfaceList.length];
int classCount = 0;
Class classSizeClass = ClassSize.class; // Make sure that the garbage collector does not unload it
ClassSize.setDummyCatalog();
/* Most of the classes we will catalog invoke ClassSize.estimateBaseFromCatalog in
* their static initializer. This dummy the catalog out so that this will not generate
* errors. We will not actually use the classes, just examine their fields.
*/
for( int i = 0; i < classAndInterfaceList.length; i++)
{
Class cls = null;
try
{
cls = Class.forName( classAndInterfaceList[i]);
}
catch( ClassNotFoundException cnfe)
{
System.err.println( "*** Could not find class " + classAndInterfaceList[i]);
System.exit(1);
}
if( cls.isInterface())
interfaceList[ interfaceCount++] = cls;
else
classList[ classCount++] = cls;
}
String WS = System.getProperty( "WS");
if( WS == null)
{
System.err.println( "*** WS is not set.");
System.exit(1);
}
StringBuffer baseDir = new StringBuffer( System.getProperty( "classDir", ""));
if( baseDir.length() == 0)
{
baseDir.append( WS);
baseDir.append( '/');
baseDir.append( "classes");
}
int baseDirLength = baseDir.length();
StringBuffer packagePrefix = new StringBuffer( );
Hashtable<String, int[]> classSizes = new Hashtable<String, int[]>();
ClassSizeCrawler crawler = new ClassSizeCrawler(interfaceList, interfaceCount, classSizes);
if( interfaceCount > 0)
{
boolean gotPrefix = false;
// Crawl through the class hierarchies for classes implementing the interfaces
for( Enumeration e = System.getProperties().propertyNames();
e.hasMoreElements();)
{
String propertyName = (String) e.nextElement();
if( propertyName.equals( "prefix") || propertyName.startsWith( "prefix."))
{
gotPrefix = true;
packagePrefix.setLength( 0);
packagePrefix.append( System.getProperty( propertyName));
baseDir.setLength( baseDirLength);
if( packagePrefix.length() > 0)
{
baseDir.append( '/');
for( int offset = 0; offset < packagePrefix.length(); offset++)
{
char c = packagePrefix.charAt( offset);
if( c == '.')
baseDir.append( '/');
else
baseDir.append( c);
}
}
crawler.crawl( new File( baseDir.toString()), packagePrefix);
}
}
if( ! gotPrefix)
{
System.err.println( "*** Could not search the class hierarchy because no starting");
System.err.println( " prefixes where specified.");
System.exit(1);
}
}
for( int i = 0; i < classCount; i++)
crawler.addClass( classList[i]);
baseDir.setLength( baseDirLength);
String outputFileName =
System.getProperty( "out", WS + "/java/org.apache.derby.iapi.services.cache.ClassSizeCatalogImpl.java");
try
{
PrintWriter out = new PrintWriter( new FileWriter( outputFileName));
out.print( "/*\n\n" +
" Licensed to the Apache Software Foundation (ASF) under one or more\n" +
" contributor license agreements. See the NOTICE file distributed with\n" +
" this work for additional information regarding copyright ownership.\n" +
" The ASF licenses this file to You under the Apache License, Version 2.0\n" +
" (the \"License\"); you may not use this file except in compliance with\n" +
" the License. You may obtain a copy of the License at\n" +
"\n" +
" http://www.apache.org/licenses/LICENSE-2.0\n" +
"\n" +
" Unless required by applicable law or agreed to in writing, software\n" +
" distributed under the License is distributed on an \"AS IS\" BASIS,\n" +
" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
" See the License for the specific language governing permissions and\n" +
" limitations under the License.\n" +
" */\n");
out.print( "package org.apache.derby.iapi.services.cache;\n" +
"class ClassSizeCatalogImpl extends ClassSizeCatalog\n" +
"{\n" +
" public ClassSizeCatalogImpl()\n" +
" {\n");
for( Enumeration e = classSizes.keys();
e.hasMoreElements();)
{
String className = (String) e.nextElement();
int[] coeff = (int[]) classSizes.get( className);
out.print( " put( \"" + className + "\", new int[]{" + coeff[0] + "," + coeff[1] + "});\n");
}
out.print(" }\n" +
"}\n");
out.flush();
out.close();
}
catch( IOException ioe)
{
System.err.println( "*** Cannot write to " + outputFileName);
System.err.println( " " + ioe.getMessage());
System.exit(1);
}
} // end of main
private Class<?>[] interfaceList; // Search for classes that implement these interfaces
private int interfaceCount;
private Hashtable<String, int[]> classSizes;
private boolean verbose = false;
private ClassSizeCrawler( Class[] interfaceList,
int interfaceCount,
Hashtable<String, int[]> classSizes)
{
this.interfaceList = interfaceList;
this.classSizes = classSizes;
this.interfaceCount = interfaceCount;
verbose = Boolean.parseBoolean( System.getProperty( "verbose", "false"));
}
private void crawl( File curDir, StringBuffer className)
{
if( verbose)
System.out.println( "Searching directory " + curDir.getPath());
try
{
if( ! curDir.isDirectory())
{
System.err.println( "*** " + curDir.getPath() + " is not a directory.");
System.exit(1);
}
}
catch( SecurityException se)
{
System.err.println( "Cannot access " + curDir.getPath());
System.exit(1);
}
String[] filenames = curDir.list( );
if( className.length() != 0)
className.append( ".");
int classNameLength = className.length();
for( int fileIdx = 0; fileIdx < filenames.length; fileIdx++)
{
if( filenames[fileIdx].endsWith( ".class"))
{
// Strip off the ".class" suffix
String s = filenames[fileIdx].substring( 0, filenames[fileIdx].length() - 6);
className.append( s);
Class<?> targetClass = null;
String targetClassName = className.toString();
try
{
targetClass = Class.forName( targetClassName);
if( !targetClass.isInterface())
{
for( int interfaceIdx = 0; interfaceIdx < interfaceCount; interfaceIdx++)
{
if( interfaceList[interfaceIdx].isAssignableFrom( targetClass))
addClass( targetClass);
}
}
}
catch( ClassNotFoundException cnfe)
{
System.err.println( "Could not find class " + targetClassName);
System.exit(1);
}
catch( Throwable t){}
className.setLength( classNameLength);
}
else
{
File nextDir = new File( curDir, filenames[fileIdx]);
if( nextDir.isDirectory())
{
className.append( filenames[fileIdx]);
crawl( nextDir, className);
className.setLength( classNameLength);
}
}
}
} // end of crawl
private void addClass( Class targetClass)
{
int[] coefficients = ClassSize.getSizeCoefficients( targetClass);
if( verbose)
System.out.println( targetClass.getName() + " " + coefficients[0] + ", " + coefficients[1]);
classSizes.put( targetClass.getName(), coefficients);
} // end of addClass
} // end of ClassSizeCrawler
|