001/* 002 * Copyright 2012 Chris Pheby 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.jadira.scanner.core.helper; 017 018import javassist.Modifier; 019import javassist.bytecode.AttributeInfo; 020import javassist.bytecode.CodeAttribute; 021import javassist.bytecode.LocalVariableAttribute; 022import javassist.bytecode.MethodInfo; 023 024public final class JavassistMethodInfoHelper { 025 026 private JavassistMethodInfoHelper() { 027 } 028 029 public static String[] getMethodParamTypeNames(MethodInfo methodInfo) { 030 String desc = methodInfo.getDescriptor(); 031 032 String paramsString = desc.substring(desc.indexOf('(') + 1, desc.lastIndexOf(')')); 033 034 if (paramsString.length() == 0) { 035 return new String[0]; 036 } 037 038 String[] classNames = paramsString.split(";"); 039 return classNames; 040 } 041 042 public static String[] getMethodParamNames(MethodInfo methodInfo) { 043 044 String[] retVal = new String[getMethodParamTypeNames(methodInfo).length]; 045 046 CodeAttribute codeAttribute = methodInfo.getCodeAttribute(); 047 048 LocalVariableAttribute localVarAttr = null; 049 if (codeAttribute != null) { 050 AttributeInfo attributeInfo = codeAttribute.getAttribute("LocalVariableTable"); 051 localVarAttr = (LocalVariableAttribute) attributeInfo; 052 } 053 054 055 int j = 0; 056 057 if (Modifier.isSynchronized(methodInfo.getAccessFlags())) { 058 j = j + 1; 059 } 060 if (!Modifier.isStatic(methodInfo.getAccessFlags())) { 061 j = j + 1; 062 } 063 064 for (int i = 0; i < retVal.length; i++) { 065 066 if (localVarAttr == null) { 067 retVal[i] = "" + i; 068 } else { 069 retVal[i] = localVarAttr.variableName(i + j); 070 } 071 } 072 return retVal; 073 } 074}