All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.adoptopenjdk.jitwatch.jarscan.JarScan Maven / Gradle / Ivy

Go to download

A Maven plugin that scans the project artifact and its dependencies for methods that cannot be inlined by the JIT compiler. It uses the JarScan utility from the JITWatch project to do that. See https://github.com/AdoptOpenJDK/jitwatch .

There is a newer version: 1.1
Show newest version
/*
 * Copyright (c) 2013, 2014 Chris Newland.
 * Licensed under https://github.com/AdoptOpenJDK/jitwatch/blob/master/LICENSE-BSD
 * Instructions: https://github.com/AdoptOpenJDK/jitwatch/wiki
 */
package org.adoptopenjdk.jitwatch.jarscan;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.adoptopenjdk.jitwatch.loader.BytecodeLoader;
import org.adoptopenjdk.jitwatch.model.MemberSignatureParts;
import org.adoptopenjdk.jitwatch.model.bytecode.BytecodeInstruction;
import org.adoptopenjdk.jitwatch.model.bytecode.ClassBC;
import org.adoptopenjdk.jitwatch.model.bytecode.MemberBytecode;

import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.*;

public final class JarScan
{
	private JarScan()
	{
	}

	@SuppressWarnings("unchecked")
	public static void iterateJar(File jarFile, int maxMethodBytes, PrintWriter writer) throws IOException
	{
		List classLocations = new ArrayList<>();

		classLocations.add(jarFile.getPath());

		try (ZipFile zip = new ZipFile(jarFile))
		{
			Enumeration list = (Enumeration) zip.entries();

			while (list.hasMoreElements())
			{
				ZipEntry entry = list.nextElement();

				String name = entry.getName();

				if (name.endsWith(S_DOT_CLASS))
				{
					String fqName = name.replace(S_SLASH, S_DOT).substring(0, name.length() - 6);

					process(classLocations, fqName, maxMethodBytes, writer);
				}
			}
		}
	}

	private static void process(List classLocations, String className, int maxMethodBytes, PrintWriter writer)
	{
		ClassBC classBytecode = BytecodeLoader.fetchBytecodeForClass(classLocations, className);

		if (classBytecode != null)
		{
			for (MemberBytecode memberBytecode : classBytecode.getMemberBytecodeList())
			{
				List instructions = memberBytecode.getInstructions();

				if (instructions != null && instructions.size() > 0)
				{
					BytecodeInstruction lastInstruction = instructions.get(instructions.size() - 1);

					// final instruction is a return for 1 byte
					int bcSize = 1 + lastInstruction.getOffset();

					MemberSignatureParts msp = memberBytecode.getMemberSignatureParts();

					if (bcSize >= maxMethodBytes && !S_STATIC_INIT.equals(msp.getMemberName()))
					{
						writer.print(C_DOUBLE_QUOTE);
						writer.print(className);
						writer.print(C_DOUBLE_QUOTE);
						writer.print(C_COMMA);

						writer.print(C_DOUBLE_QUOTE);
						writer.print(msp.getMemberName());
						writer.print(C_DOUBLE_QUOTE);
						writer.print(C_COMMA);

						writer.print(bcSize);
						writer.println();

						writer.flush();
					}
				}
			}
		}
		else
		{
			System.err.println("An error occurred while parsing " + className + ". Please see jitwatch.out for details");
			System.exit(-1);
		}
	}

	public static void main(String[] args) throws IOException
	{
		int maxMethodBytes = Integer.getInteger("maxMethodSize", 325);

		PrintWriter writer = new PrintWriter(System.out);

		for (String jar : args)
		{
			File jarFile = new File(jar);

			writer.print(jarFile.getAbsolutePath());

			writer.println(C_COLON);

			iterateJar(jarFile, maxMethodBytes, writer);

			writer.println();
		}

		writer.flush();
		writer.close();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy