当前位置: 首页>>代码示例>>Java>>正文


Java LocalVariableAttribute.variableName方法代码示例

本文整理汇总了Java中javassist.bytecode.LocalVariableAttribute.variableName方法的典型用法代码示例。如果您正苦于以下问题:Java LocalVariableAttribute.variableName方法的具体用法?Java LocalVariableAttribute.variableName怎么用?Java LocalVariableAttribute.variableName使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javassist.bytecode.LocalVariableAttribute的用法示例。


在下文中一共展示了LocalVariableAttribute.variableName方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getParamNames

import javassist.bytecode.LocalVariableAttribute; //导入方法依赖的package包/类
/**
 * 获取方法的参数名
 * @param clazz  类
 * @param method 方法
 * @return 方法名称数组
 * @throws NotFoundException .{@link NotFoundException}
 */
public static String[] getParamNames(Class clazz, String method) throws NotFoundException {
    ClassPool pool = ClassPool.getDefault();
    CtClass cc = pool.get(clazz.getName());
    CtMethod cm = cc.getDeclaredMethod(method);
    // 使用javaassist的反射方法获取方法的参数名
    MethodInfo methodInfo = cm.getMethodInfo();
    CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
    LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
    if (attr == null) {
        throw new NotFoundException("cannot get LocalVariableAttribute");
    }
    String[] paramNames = new String[cm.getParameterTypes().length];
    int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
    for (int i = 0; i < paramNames.length; i++) {
        paramNames[i] = attr.variableName(i + pos);
    }
    return paramNames;
}
 
开发者ID:DreamYa0,项目名称:zeratul,代码行数:26,代码来源:ReflectionUtils.java

示例2: getParamNames

import javassist.bytecode.LocalVariableAttribute; //导入方法依赖的package包/类
/**
 * get method parameter names
 * @param cls
 * @param method
 * @return
 * @throws Exception
 */
public static String[] getParamNames(Class<?> cls, Method method) throws Exception {
    ClassPool pool = ClassPool.getDefault();
    CtClass cc = pool.get(cls.getName());

    Class<?>[] paramAry = method.getParameterTypes();
    String[] paramTypeNames = new String[paramAry.length];
    for (int i = 0; i < paramAry.length; i++) {
        paramTypeNames[i] = paramAry[i].getName();
    }

    CtMethod cm = cc.getDeclaredMethod(method.getName(), pool.get(paramTypeNames));

    MethodInfo methodInfo = cm.getMethodInfo();
    CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
    LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
    if (attr == null) {
        throw new Exception("class:" + cls.getName() + ", have no LocalVariableTable, please use javac -g:{vars} to compile the source file");
    }
    String[] paramNames = new String[cm.getParameterTypes().length];
    int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
    for (int i = 0; i < paramNames.length; i++) {
        paramNames[i] = attr.variableName(i + pos);
    }
    return paramNames;
}
 
开发者ID:lemonJun,项目名称:TakinRPC,代码行数:33,代码来源:ClassHelper.java

示例3: getParamName

import javassist.bytecode.LocalVariableAttribute; //导入方法依赖的package包/类
/**
 * 获取方法参数名
 * @param method
 * @return 
 */
public static String[] getParamName(Method method) throws Exception {
	ClassPool pool = ClassPool.getDefault();
	CtClass ctClass = pool.get(method.getDeclaringClass().getName());
	CtMethod ctMethod = ctClass.getDeclaredMethod(method.getName());
	// 使用javassist的反射方法的参数名
	MethodInfo methodInfo = ctMethod.getMethodInfo();
	CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
	LocalVariableAttribute attr = (LocalVariableAttribute) 
		codeAttribute.getAttribute(LocalVariableAttribute.tag);

	int length = ctMethod.getParameterTypes().length;
	String[] array = new String[length];
	for (int i = 0; i < length; i++) {
		array[i] = attr.variableName(i + 1);
	}
	return array;
}
 
开发者ID:NymphWeb,项目名称:nymph,代码行数:23,代码来源:Javassists.java

示例4: getMethodArgNames

import javassist.bytecode.LocalVariableAttribute; //导入方法依赖的package包/类
/**
 * Get method arg names.
 *
 * @param clazz
 * @param methodName
 * @return
 */
public static <T> String[] getMethodArgNames(Class<T> clazz, String methodName) {
    try {
        ClassPool pool = ClassPool.getDefault();
        CtClass cc = pool.get(clazz.getName());

        CtMethod cm = cc.getDeclaredMethod(methodName);

        // 使用javaassist的反射方法获取方法的参数名
        MethodInfo methodInfo = cm.getMethodInfo();
        CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
        LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
        if (attr == null) {
            throw new RuntimeException("LocalVariableAttribute of method is null! Class " + clazz.getName() + ",method name is " + methodName);
        }
        String[] paramNames = new String[cm.getParameterTypes().length];
        int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
        for (int i = 0; i < paramNames.length; i++)
            paramNames[i] = attr.variableName(i + pos);

        return paramNames;
    } catch (NotFoundException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
 
开发者ID:KoperGroup,项目名称:koper,代码行数:32,代码来源:ReflectUtil.java

示例5: getMethodVariableName

import javassist.bytecode.LocalVariableAttribute; //导入方法依赖的package包/类
/**
 * Get method variable names for the provided method.
 * 
 * @param classname String
 * @param methodname String
 * @return the String[] of method variable name.
 */
public static String[] getMethodVariableName(String classname, String methodname) {
  try {
    ClassPool pool = ClassPool.getDefault();
    CtClass cc = pool.get(classname);
    CtMethod cm = cc.getDeclaredMethod(methodname);
    MethodInfo methodInfo = cm.getMethodInfo();
    CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
    String[] paramNames = new String[cm.getParameterTypes().length];
    LocalVariableAttribute attr =
        (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
    if (attr != null) {
      int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
      for (int i = 0; i < paramNames.length; i++) {
        paramNames[i] = attr.variableName(i + pos);
      }
      return paramNames;
    }
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
  return null;
}
 
开发者ID:benson-git,项目名称:ibole-microservice,代码行数:30,代码来源:ClassHelper.java

示例6: getReflectParamName

import javassist.bytecode.LocalVariableAttribute; //导入方法依赖的package包/类
public static String[] getReflectParamName(Class<?> clazz, String methodName) {
	String[] paramNames = null;
	try {
		ClassPool pool = ClassPool.getDefault();
		CtClass cc = pool.get(clazz.getName());
		CtMethod cm = cc.getDeclaredMethod(methodName);
		// 使用javaassist的反射方法获取方法的参数名
		MethodInfo methodInfo = cm.getMethodInfo();
		CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
		LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
		if (attr == null) {
			// exception
		}
		paramNames = new String[cm.getParameterTypes().length];
		int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
		for (int i = 0; i < paramNames.length; i++)
			paramNames[i] = attr.variableName(i + pos);
	} catch (NotFoundException e) {
		e.printStackTrace();
	}
	return paramNames;
}
 
开发者ID:East196,项目名称:maker,代码行数:23,代码来源:Classes.java

示例7: tes

import javassist.bytecode.LocalVariableAttribute; //导入方法依赖的package包/类
static void tes() throws NotFoundException {
    ClassPool classPool = ClassPool.getDefault();
    CtClass ctClass = classPool.get("org.rural.test.Test");
    CtMethod[] methods = ctClass.getDeclaredMethods();
    for(CtMethod method : methods){
        MethodInfo methodInfo = method.getMethodInfo();
        CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
        LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
        if (attr == null)  {
            return;
        }
        String[] paramNames = new String[method.getParameterTypes().length];
        int pos = Modifier.isStatic(method.getModifiers()) ? 0 : 1;
        for (int i = 0; i < paramNames.length; i++)
            paramNames[i] = attr.variableName(i + pos);
    }
}
 
开发者ID:nullcodeexecutor,项目名称:rural,代码行数:18,代码来源:JavaassistTest.java

示例8: testReflectParamName

import javassist.bytecode.LocalVariableAttribute; //导入方法依赖的package包/类
/**
   * 反射获取方法参数名称
   */
  public static void testReflectParamName() {
Class clazz = MyClass.class;
      try {
          ClassPool pool = ClassPool.getDefault();
          CtClass cc = pool.get(clazz.getName());
          CtMethod cm = cc.getDeclaredMethod("concatString");

          // 使用javaassist的反射方法获取方法的参数名
          MethodInfo methodInfo = cm.getMethodInfo();
          CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
          LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute
                  .getAttribute(LocalVariableAttribute.tag);
          if (attr == null) {
              // exception
          }
          String[] paramNames = new String[cm.getParameterTypes().length];
          int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
          for (int i = 0; i < paramNames.length; i++)
              paramNames[i] = attr.variableName(i + pos);
          // paramNames即参数名
          for (int i = 0; i < paramNames.length; i++) {
              System.out.println("参数名" + i + ":" + paramNames[i]);
          }

      } catch (NotFoundException e) {
          e.printStackTrace();
      }
  }
 
开发者ID:chenmins,项目名称:objector,代码行数:32,代码来源:Test.java

示例9: getParameterNames

import javassist.bytecode.LocalVariableAttribute; //导入方法依赖的package包/类
/**
 * 获取方法的参数名称.
 * 
 * @param ctMethod
 * @return
 * @throws NotFoundException
 */
public static String[] getParameterNames(CtMethod ctMethod) throws NotFoundException {
	MethodInfo methodInfo = ctMethod.getMethodInfo();
	CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
	// logger.info("methodInfo.getConstPool().getSize():");
	LocalVariableAttribute attribute = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
	// String[] names = new String[attribute.tableLength() - 1];
	String[] paramNames = new String[ctMethod.getParameterTypes().length];

	int pos = 0;
	if (true) {
		int size = attribute.tableLength();
		if (size > 0) {
			String[] names = new String[size - 1];
			for (int i = 0; i < names.length; i++) {
				names[i] = attribute.variableName(i);
				if ("this".equals(names[i])) {
					pos = i + 1;
					break;
				}
			}
			// logger.info(methodInfo.getName() + " pos:" + pos + " allNames:" + StringUtils.join(names, ", "));
		}
	}

	// logger.info(methodInfo.getName() + " pos:" + pos);
	for (int i = 0; i < paramNames.length; i++) {
		// paramNames[i] = attribute.variableName(i + 1);
		try {
			paramNames[i] = attribute.variableName(i + pos);
			// logger.info("paramNames[" + i + "]:" + paramNames[i]);
		}
		catch (RuntimeException e) {
			throw e;
		}
	}
	// System.err.println("paramNames:" + StringUtils.join(paramNames));
	return paramNames;
}
 
开发者ID:tanhaichao,项目名称:leopard,代码行数:46,代码来源:CtClassUtil.java

示例10: parameterNameFor

import javassist.bytecode.LocalVariableAttribute; //导入方法依赖的package包/类
/**
 * Determine the name of parameter with index i in the given method. Use the
 * locals attributes about local variables from the classfile. Note: This is
 * still work in progress.
 * 
 * @param method
 * @param locals
 * @param i
 * @return the name of the parameter if available or a number if not.
 */
static String parameterNameFor(CtBehavior method, LocalVariableAttribute locals, int i) {

    if (locals == null) {
        return Integer.toString(i + 1);
    }

    int modifiers = method.getModifiers();

    int j = i;

    if (Modifier.isSynchronized(modifiers)) {
        // skip object to synchronize upon.
        j++;
        // System.err.println("Synchronized");
    }
    if (Modifier.isStatic(modifiers) == false) {
        // skip "this"
        j++;
        // System.err.println("Instance");
    }
    String variableName = locals.variableName(j);
    // if (variableName.equals("this")) {
    // System.err.println("'this' returned as a parameter name for "
    // + method.getName() + " index " + j
    // +
    // ", names are probably shifted. Please submit source for class in slf4j bugreport");
    // }
    return variableName;
}
 
开发者ID:cscfa,项目名称:bartleby,代码行数:40,代码来源:JavassistHelper.java

示例11: getParameterName

import javassist.bytecode.LocalVariableAttribute; //导入方法依赖的package包/类
public static String getParameterName(LocalVariableAttribute localVarAttr, int parameterIndex, boolean isStaticMethod) {
	if(isStaticMethod) {
		return localVarAttr.variableName(parameterIndex);
	} else {
		return localVarAttr.variableName(parameterIndex + 1);
	}
}
 
开发者ID:SalamaSoft,项目名称:REST-framework,代码行数:8,代码来源:JavaAssistUtil.java

示例12: getParameterNames

import javassist.bytecode.LocalVariableAttribute; //导入方法依赖的package包/类
public static String[] getParameterNames(String className, String methodName) throws NotFoundException {
	ClassPool clsPool = ClassPool.getDefault();
	
	CtClass ctCls = clsPool.get(className);

	CtMethod[] ctMethods = ctCls.getMethods();
	CtMethod ctMethod = null;

	for(CtMethod methodTmp : ctMethods) {
		if(methodTmp.getName().equals(methodName)
				&& (methodTmp.getModifiers() & Modifier.PUBLIC) != 0
			) {
			ctMethod = methodTmp;
			break;
		}
	}

	MethodInfo methodInfo = ctMethod.getMethodInfo();
	CodeAttribute codeAttr = methodInfo.getCodeAttribute();
	LocalVariableAttribute localVarAttr = (LocalVariableAttribute) codeAttr.getAttribute(LocalVariableAttribute.tag);
	
	if(localVarAttr == null) {
		return null;
	}
	
	String[] paramNames = new String[ctMethod.getParameterTypes().length];
	int startIndex = 0;
	if(!Modifier.isStatic(ctMethod.getModifiers())) {
		startIndex = 1;
	}
	for(int i = 0; i < paramNames.length; i++) {
		paramNames[i] = localVarAttr.variableName(i + startIndex);
	}
	
	return paramNames;
}
 
开发者ID:SalamaSoft,项目名称:REST-framework,代码行数:37,代码来源:JavaAssistUtil.java

示例13: getParameterVariableName

import javassist.bytecode.LocalVariableAttribute; //导入方法依赖的package包/类
public static String[] getParameterVariableName(CtBehavior method, LocalVariableAttribute localVariableAttribute) {
    // Inspired by
    // http://www.jarvana.com/jarvana/view/org/jboss/weld/servlet/weld-servlet/1.0.1-Final/weld-servlet-1.0.1-Final-sources.jar!/org/slf4j/instrumentation/JavassistHelper.java?format=ok
    // http://grepcode.com/file/repo1.maven.org/maven2/jp.objectfanatics/assertion-weaver/0.0.30/jp/objectfanatics/commons/javassist/JavassistUtils.java
    if (localVariableAttribute == null) {
        // null means that the class is not compiled with debug option.
        return null;
    }

    dump(localVariableAttribute);
    String[] parameterTypes = JavaAssistUtils.parseParameterSignature(method.getSignature());
    if (parameterTypes.length == 0) {
        return EMPTY_STRING_ARRAY;
    }
    String[] parameterVariableNames = new String[parameterTypes.length];
    boolean thisExist = thisExist(method);

    int paramIndex = 0;
    for (int i = 0; i < localVariableAttribute.tableLength(); i++) {
        // if start pc is not 0, it's not a parameter.
        if (localVariableAttribute.startPc(i) != 0) {
            continue;
        }
        int index = localVariableAttribute.index(i);
        if (index == 0 && thisExist) {
            // variable this. skip.
            continue;
        }
        String variablename = localVariableAttribute.variableName(i);
        parameterVariableNames[paramIndex++] = variablename;
        
        if (paramIndex == parameterTypes.length) {
            break;
        }
    }
    return parameterVariableNames;
}
 
开发者ID:naver,项目名称:pinpoint,代码行数:38,代码来源:JavaAssistUtils.java

示例14: param

import javassist.bytecode.LocalVariableAttribute; //导入方法依赖的package包/类
/**
	 * 解析参数
	 * @param klass
	 * @return
	 * @creatTime 上午10:45:36
	 * @author Eddy
	 * @throws NotFoundException 
	 */
	public static Map<String, String[]> param(Class<?> klass){
		try {
			CtClass cl = pool.get(klass.getName());
			if (null == cl) {
				pool.insertClassPath(klass.getName());
				cl = pool.get(klass.getName());
			}
			Map<String, String[]> result = new HashMap<String, String[]>();
			Method[] methods = klass.getDeclaredMethods();
			for (Method method : methods) {
				if (!method.isAccessible()) method.setAccessible(true);
				CtMethod cm = cl.getDeclaredMethod(method.getName(), paramType(method));
				MethodInfo methodInfo = cm.getMethodInfo();
				CodeAttribute ca = methodInfo.getCodeAttribute();
				LocalVariableAttribute lva = (LocalVariableAttribute) ca.getAttribute(LocalVariableAttribute.tag);
				String[] methodParam = new String[method.getParameterTypes().length];
				for (int i = 1; i <= methodParam.length; i++) {
//					if ("this".equals(lva.descriptorIndex(i))) continue;
					methodParam[i - 1] = lva.variableName(i);
				}
				result.put(new StringBuilder().append(klass.getName()).append("_").append(method.getName()).toString(), methodParam);
			}
			return result;
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new IllegalArgumentException(e);
		}
	}
 
开发者ID:Justice-love,项目名称:kiby,代码行数:38,代码来源:ParamParser.java


注:本文中的javassist.bytecode.LocalVariableAttribute.variableName方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。