1 /*******************************************************************************
2  * Copyright (c) 2009, 2015 Mountainminds GmbH & Co. KG and Contributors
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  *******************************************************************************/
12 package org.jacoco.report;
13 
14 import org.objectweb.asm.Type;
15 
16 /**
17  * Names for the Java language.
18  */
19 public class JavaNames implements ILanguageNames {
20 
getPackageName(final String vmname)21 	public String getPackageName(final String vmname) {
22 		if (vmname.length() == 0) {
23 			return "default";
24 		}
25 		return vmname.replace('/', '.');
26 	}
27 
getClassName(final String vmname)28 	private String getClassName(final String vmname) {
29 		final int pos = vmname.lastIndexOf('/');
30 		final String name = pos == -1 ? vmname : vmname.substring(pos + 1);
31 		return name.replace('$', '.');
32 	}
33 
isAnonymous(final String vmname)34 	private boolean isAnonymous(final String vmname) {
35 		final int dollarPosition = vmname.lastIndexOf('$');
36 		if (dollarPosition == -1) {
37 			return false;
38 		}
39 		final int internalPosition = dollarPosition + 1;
40 		if (internalPosition == vmname.length()) {
41 			// shouldn't happen for classes compiled from Java source
42 			return false;
43 		}
44 		// assume non-identifier start character for anonymous classes
45 		final char start = vmname.charAt(internalPosition);
46 		return !Character.isJavaIdentifierStart(start);
47 	}
48 
getClassName(final String vmname, final String vmsignature, final String vmsuperclass, final String[] vminterfaces)49 	public String getClassName(final String vmname, final String vmsignature,
50 			final String vmsuperclass, final String[] vminterfaces) {
51 		if (isAnonymous(vmname)) {
52 			final String vmsupertype;
53 			if (vminterfaces != null && vminterfaces.length > 0) {
54 				vmsupertype = vminterfaces[0];
55 			} else if (vmsuperclass != null) {
56 				vmsupertype = vmsuperclass;
57 			} else {
58 				vmsupertype = null;
59 			}
60 			// append Eclipse style label, e.g. "Foo.new Bar() {...}"
61 			if (vmsupertype != null) {
62 				final StringBuilder builder = new StringBuilder();
63 				final String vmenclosing = vmname.substring(0,
64 						vmname.lastIndexOf('$'));
65 				builder.append(getClassName(vmenclosing)).append(".new ")
66 						.append(getClassName(vmsupertype)).append("() {...}");
67 				return builder.toString();
68 			}
69 		}
70 		return getClassName(vmname);
71 	}
72 
getQualifiedClassName(final String vmname)73 	public String getQualifiedClassName(final String vmname) {
74 		return vmname.replace('/', '.').replace('$', '.');
75 	}
76 
getMethodName(final String vmclassname, final String vmmethodname, final String vmdesc, final String vmsignature)77 	public String getMethodName(final String vmclassname,
78 			final String vmmethodname, final String vmdesc,
79 			final String vmsignature) {
80 		return getMethodName(vmclassname, vmmethodname, vmdesc, false);
81 	}
82 
getQualifiedMethodName(final String vmclassname, final String vmmethodname, final String vmdesc, final String vmsignature)83 	public String getQualifiedMethodName(final String vmclassname,
84 			final String vmmethodname, final String vmdesc,
85 			final String vmsignature) {
86 		return getQualifiedClassName(vmclassname) + "."
87 				+ getMethodName(vmclassname, vmmethodname, vmdesc, true);
88 	}
89 
getMethodName(final String vmclassname, final String vmmethodname, final String vmdesc, final boolean qualifiedParams)90 	private String getMethodName(final String vmclassname,
91 			final String vmmethodname, final String vmdesc,
92 			final boolean qualifiedParams) {
93 		if ("<clinit>".equals(vmmethodname)) {
94 			return "static {...}";
95 		}
96 		final StringBuilder result = new StringBuilder();
97 		if ("<init>".equals(vmmethodname)) {
98 			if (isAnonymous(vmclassname)) {
99 				return "{...}";
100 			} else {
101 				result.append(getClassName(vmclassname));
102 			}
103 		} else {
104 			result.append(vmmethodname);
105 		}
106 		result.append('(');
107 		final Type[] arguments = Type.getArgumentTypes(vmdesc);
108 		boolean comma = false;
109 		for (final Type arg : arguments) {
110 			if (comma) {
111 				result.append(", ");
112 			} else {
113 				comma = true;
114 			}
115 			if (qualifiedParams) {
116 				result.append(getQualifiedClassName(arg.getClassName()));
117 			} else {
118 				result.append(getShortTypeName(arg));
119 			}
120 		}
121 		result.append(')');
122 		return result.toString();
123 	}
124 
getShortTypeName(final Type type)125 	private String getShortTypeName(final Type type) {
126 		final String name = type.getClassName();
127 		final int pos = name.lastIndexOf('.');
128 		final String shortName = pos == -1 ? name : name.substring(pos + 1);
129 		return shortName.replace('$', '.');
130 	}
131 
132 }
133