JavaNames.java

    1/*******************************************************************************
    2 * Copyright (c) 2009 Mountainminds GmbH & Co. KG 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 *    Marc R. Hoffmann - initial API and implementation
   10 *    
   11 * $Id: $
   12 *******************************************************************************/
   13package org.jacoco.report;
   14
   15import org.objectweb.asm.Type;
   16
   17/**
   18 * Names for the Java language.
   19 * 
   20 * @author Marc R. Hoffmann
   21 * @version $Revision: $
   22 */
   23public class JavaNames implements ILanguageNames {
   24
   25    public String getPackageName(final String vmname) {
   26        if (vmname.length() == 0) {
   27            return "default";
   28        }
   29        return vmname.replace('/', '.');
   30    }
   31
   32    public String getClassName(final String vmname) {
   33        final int pos = vmname.lastIndexOf('/');
   34        final String name = pos == -1 ? vmname : vmname.substring(pos + 1);
   35        return name.replace('$', '.');
   36    }
   37
   38    public String getMethodName(final String vmclassname,
   39            final String vmmethodname, final String vmdesc) {
   40        if (vmmethodname.equals("<cinit>")) {
   41            return "static {...}";
   42        }
   43        final Type[] arguments = Type.getArgumentTypes(vmdesc);
   44        final StringBuilder result = new StringBuilder();
   45        if (vmmethodname.equals("<init>")) {
   46            result.append(getClassName(vmclassname));
   47        } else {
   48            result.append(vmmethodname);
   49        }
   50        result.append('(');
   51        boolean colon = false;
   52        for (final Type arg : arguments) {
   53            if (colon) {
   54                result.append(", ");
   55            }
   56            result.append(getShortTypeName(arg));
   57            colon = true;
   58        }
   59        result.append(')');
   60        return result.toString();
   61    }
   62
   63    private String getShortTypeName(final Type type) {
   64        final String name = type.getClassName();
   65        final int pos = name.lastIndexOf('.');
   66        final String shortName = pos == -1 ? name : name.substring(pos + 1);
   67        return shortName.replace('$', '.');
   68    }
   69
   70}