0N/A/*
3221N/A * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation.
0N/A *
2362N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2362N/A *
2362N/A */
0N/A
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.PrintWriter;
0N/Aimport java.io.StringWriter;
0N/A
0N/Aimport java.lang.reflect.Method;
0N/Aimport java.net.URI;
0N/Aimport java.util.Arrays;
0N/Aimport java.util.Vector;
0N/A
0N/Aimport javax.tools.Diagnostic;
0N/Aimport javax.tools.DiagnosticCollector;
0N/Aimport javax.tools.FileObject;
0N/Aimport javax.tools.ForwardingJavaFileManager;
0N/Aimport javax.tools.JavaCompiler;
0N/Aimport javax.tools.JavaCompiler.CompilationTask;
0N/Aimport javax.tools.JavaFileManager;
0N/Aimport javax.tools.JavaFileObject;
0N/Aimport javax.tools.JavaFileObject.Kind;
0N/Aimport javax.tools.SimpleJavaFileObject;
0N/Aimport javax.tools.StandardJavaFileManager;
0N/Aimport javax.tools.ToolProvider;
0N/A
0N/A/*
0N/A * @test SortMethodsTest
0N/A * @bug 6925573
0N/A * @summary verify that class loading does not need quadratic time with regard to the number of class
0N/Amethods.
0N/A * @run main SortMethodsTest
0N/A * @author volker.simonis@gmail.com
0N/A*/
0N/A
0N/Apublic class SortMethodsTest {
0N/A
0N/A static String createClass(String name, int nrOfMethods) {
0N/A StringWriter sw = new StringWriter();
0N/A PrintWriter pw = new PrintWriter(sw);
0N/A pw.println("public class " + name + "{");
0N/A for (int i = 0; i < nrOfMethods; i++) {
0N/A pw.println(" public void m" + i + "() {}");
0N/A }
0N/A pw.println(" public static String sayHello() {");
0N/A pw.println(" return \"Hello from class \" + " + name +
0N/A ".class.getName() + \" with \" + " + name +
0N/A ".class.getDeclaredMethods().length + \" methods\";");
0N/A pw.println(" }");
0N/A pw.println("}");
0N/A pw.close();
0N/A return sw.toString();
0N/A }
0N/A
0N/A public static void main(String args[]) {
0N/A
0N/A JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
0N/A DiagnosticCollector<JavaFileObject> diags = new DiagnosticCollector<JavaFileObject>();
0N/A final String cName = new String("ManyMethodsClass");
0N/A Vector<Long> results = new Vector<Long>();
0N/A
0N/A for (int i = 6; i < 600000; i*=10) {
0N/A String klass = createClass(cName, i);
0N/A JavaMemoryFileObject file = new JavaMemoryFileObject(cName, klass);
0N/A MemoryFileManager mfm = new MemoryFileManager(comp.getStandardFileManager(diags, null, null), file);
0N/A CompilationTask task = comp.getTask(null, mfm, diags, null, null, Arrays.asList(file));
0N/A
0N/A if (task.call()) {
0N/A try {
0N/A MemoryClassLoader mcl = new MemoryClassLoader(file);
0N/A long start = System.nanoTime();
0N/A Class<? extends Object> c = Class.forName(cName, true, mcl);
0N/A long end = System.nanoTime();
0N/A results.add(end - start);
0N/A Method m = c.getDeclaredMethod("sayHello", new Class[0]);
0N/A String ret = (String)m.invoke(null, new Object[0]);
0N/A System.out.println(ret + " (loaded and resloved in " + (end - start) + "ns)");
0N/A } catch (Exception e) {
0N/A System.err.println(e);
0N/A }
0N/A }
0N/A else {
0N/A System.out.println(klass);
0N/A System.out.println();
0N/A for (Diagnostic diag : diags.getDiagnostics()) {
0N/A System.out.println(diag.getCode() + "\n" + diag.getKind() + "\n" + diag.getPosition());
0N/A System.out.println(diag.getSource() + "\n" + diag.getMessage(null));
0N/A }
0N/A }
0N/A }
0N/A
0N/A long lastRatio = 0;
0N/A for (int i = 2; i < results.size(); i++) {
3221N/A long normalized1 = Math.max(results.get(i-1) - results.get(0), 1);
0N/A long normalized2 = Math.max(results.get(i) - results.get(0), 1);
0N/A long ratio = normalized2/normalized1;
lastRatio = ratio;
System.out.println("10 x more methods requires " + ratio + " x more time");
}
// The following is just vague estimation but seems to work on current x86_64 and sparcv9 machines
if (lastRatio > 80) {
throw new RuntimeException("ATTENTION: it seems that class loading needs quadratic time with regard to the number of class methods!!!");
}
}
}
class JavaMemoryFileObject extends SimpleJavaFileObject {
private final String code;
private ByteArrayOutputStream byteCode;
JavaMemoryFileObject(String name, String code) {
super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension), Kind.SOURCE);
this.code = code;
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return code;
}
@Override
public OutputStream openOutputStream() {
byteCode = new ByteArrayOutputStream();
return byteCode;
}
byte[] getByteCode() {
return byteCode.toByteArray();
}
}
class MemoryClassLoader extends ClassLoader {
private final JavaMemoryFileObject jfo;
public MemoryClassLoader(JavaMemoryFileObject jfo) {
this.jfo = jfo;
}
public Class findClass(String name) {
byte[] b = jfo.getByteCode();
return defineClass(name, b, 0, b.length);
}
}
class MemoryFileManager extends ForwardingJavaFileManager<JavaFileManager> {
private final JavaFileObject jfo;
public MemoryFileManager(StandardJavaFileManager jfm, JavaFileObject jfo) {
super(jfm);
this.jfo = jfo;
}
@Override
public FileObject getFileForInput(Location location, String packageName,
String relativeName) throws IOException {
return jfo;
}
@Override
public JavaFileObject getJavaFileForOutput(Location location, String qualifiedName,
Kind kind, FileObject outputFile) throws IOException {
return jfo;
}
}