javassist.bytecode.MethodInfo Java Examples
The following examples show how to use
javassist.bytecode.MethodInfo.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReflectUtil.java From koper with Apache License 2.0 | 7 votes |
/** * 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); } }
Example #2
Source File: JavassistAdapter.java From panda with Apache License 2.0 | 6 votes |
@Override public List<String> getParameterAnnotationNames(MethodInfo method, int parameterIndex) { List<String> result = new ArrayList<>(); List<ParameterAnnotationsAttribute> parameterAnnotationsAttributes = Arrays.asList((ParameterAnnotationsAttribute) method.getAttribute(ParameterAnnotationsAttribute.visibleTag), (ParameterAnnotationsAttribute) method.getAttribute(ParameterAnnotationsAttribute.invisibleTag)); for (ParameterAnnotationsAttribute parameterAnnotationsAttribute : parameterAnnotationsAttributes) { if (parameterAnnotationsAttribute == null) { continue; } Annotation[][] annotations = parameterAnnotationsAttribute.getAnnotations(); if (parameterIndex < annotations.length) { Annotation[] annotation = annotations[parameterIndex]; result.addAll(getAnnotationNames(annotation)); } } return result; }
Example #3
Source File: BeanUtil.java From framework with Apache License 2.0 | 6 votes |
/** * 获取方法参数名称 * * @param cm <br> * @return <br> * @throws NotFoundException <br> * @throws UtilException 如果最终编译的class文件不包含局部变量表信息 <br> */ protected static String[] getMethodParamNames(final CtMethod cm) throws NotFoundException, UtilException { CtClass cc = cm.getDeclaringClass(); MethodInfo methodInfo = cm.getMethodInfo(); CodeAttribute codeAttribute = methodInfo.getCodeAttribute(); if (codeAttribute == null) { throw new UtilException(ErrorCodeDef.CAN_NOT_FIND_VER_NAME_10003, cc.getName()); } LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag); if (null == attr) { throw new UtilException(ErrorCodeDef.CAN_NOT_FIND_VER_NAME_10003, cc.getName()); } 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; }
Example #4
Source File: MethodAnnotationSelector.java From panda with Apache License 2.0 | 6 votes |
@Override public Collection<Method> select(AnnotationsScannerProcess process, AnnotationScannerStore store) { Set<String> selectedClasses = new HashSet<>(); MetadataAdapter<ClassFile, FieldInfo, MethodInfo> adapter = process.getMetadataAdapter(); for (ClassFile cachedClassFile : store.getCachedClassFiles()) { for (MethodInfo method : adapter.getMethods(cachedClassFile)) { for (String annotationName : adapter.getMethodAnnotationNames(method)) { if (annotationType.getName().equals(annotationName)) { selectedClasses.add(adapter.getMethodFullKey(cachedClassFile, method)); } } } } return AnnotationsScannerUtils.forMethods(process, selectedClasses); }
Example #5
Source File: FieldTransformer.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
private void addSetFieldHandlerMethod(ClassFile classfile) throws CannotCompileException { ConstPool cp = classfile.getConstPool(); int this_class_index = cp.getThisClassInfo(); MethodInfo minfo = new MethodInfo(cp, SETFIELDHANDLER_METHOD_NAME, SETFIELDHANDLER_METHOD_DESCRIPTOR); /* local variables | this | callback | */ Bytecode code = new Bytecode(cp, 3, 3); // aload_0 // load this code.addAload(0); // aload_1 // load callback code.addAload(1); // putfield // put field "$JAVASSIST_CALLBACK" defined already code.addOpcode(Opcode.PUTFIELD); int field_index = cp.addFieldrefInfo(this_class_index, HANDLER_FIELD_NAME, HANDLER_FIELD_DESCRIPTOR); code.addIndex(field_index); // return code.addOpcode(Opcode.RETURN); minfo.setCodeAttribute(code.toCodeAttribute()); minfo.setAccessFlags(AccessFlag.PUBLIC); classfile.addMethod(minfo); }
Example #6
Source File: JavasisstUnitTest.java From tutorials with MIT License | 6 votes |
@Test public void givenJavaClass_whenLoadAtByJavassist_thenTraversWholeClass() throws NotFoundException, CannotCompileException, BadBytecode { // given ClassPool cp = ClassPool.getDefault(); ClassFile cf = cp.get("com.baeldung.javasisst.Point").getClassFile(); MethodInfo minfo = cf.getMethod("move"); CodeAttribute ca = minfo.getCodeAttribute(); CodeIterator ci = ca.iterator(); // when List<String> operations = new LinkedList<>(); while (ci.hasNext()) { int index = ci.next(); int op = ci.byteAt(index); operations.add(Mnemonic.OPCODE[op]); } // then assertEquals(operations, Arrays.asList("aload_0", "iload_1", "putfield", "aload_0", "iload_2", "putfield", "return")); }
Example #7
Source File: ClassFileJavassist.java From jbse with GNU General Public License v3.0 | 6 votes |
@Override public boolean hasOneSignaturePolymorphicMethodDeclaration(String methodName) { //cannot be signature polymorphic if it is not in JAVA_METHODHANDLE if (!JAVA_METHODHANDLE.equals(getClassName())) { return false; } //the method declaration must be unique final MethodInfo uniqueMethod = findUniqueMethodDeclarationWithName(methodName); if (uniqueMethod == null) { return false; } //cannot be signature polymorphic if it has wrong descriptor if (!SIGNATURE_POLYMORPHIC_DESCRIPTOR.equals(uniqueMethod.getDescriptor())) { return false; } //cannot be signature polymorphic if it not native or if it is not varargs if (!Modifier.isNative(AccessFlag.toModifier(uniqueMethod.getAccessFlags())) || (AccessFlag.toModifier(uniqueMethod.getAccessFlags()) & Modifier.VARARGS) == 0) { return false; } //all checks passed return true; }
Example #8
Source File: HandlerMethodArgsBuilder.java From potato-webmvc with MIT License | 6 votes |
private void initMethodParamInfo(Object controller, String methodName) { methodParamInfo = new LinkedHashMap<Class<?>, String>(); Class<?> clazz = controller.getClass(); try { ClassPool classPool = ClassPool.getDefault(); classPool.insertClassPath(new ClassClassPath(clazz)); CtClass ctClass = classPool.get(clazz.getName()); CtMethod ctMethod = ctClass.getDeclaredMethod(methodName); MethodInfo methodInfo = ctMethod.getMethodInfo(); CodeAttribute codeAttribute = methodInfo.getCodeAttribute(); LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute .getAttribute("LocalVariableTable"); CtClass[] parameterTypes = ctMethod.getParameterTypes(); int pos = Modifier.isStatic(ctMethod.getModifiers()) ? 0 : 1; for (int i = 0; i < parameterTypes.length; i++) methodParamInfo.put(Class.forName(parameterTypes[i].getName()), attr.variableName(i + pos)); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } }
Example #9
Source File: ClassFileJavassist.java From jbse with GNU General Public License v3.0 | 6 votes |
/** * Finds a method declaration in the classfile. * * @param methodSignature a {@link Signature}. * @return {@code null} if no method with {@code methodSignature} * signature is declared in this classfile, otherwise the * {@link CtBehavior} for it; the class name in {@code methodSignature} * is ignored. */ private MethodInfo findMethodDeclaration(Signature methodSignature) { if ("<clinit>".equals(methodSignature.getName())) { return this.cf.getStaticInitializer(); } final List<MethodInfo> ms = this.cf.getMethods(); for (MethodInfo m : ms) { final String internalName = m.getName(); if (internalName.equals(methodSignature.getName()) && m.getDescriptor().equals(methodSignature.getDescriptor())) { return m; } } return null; }
Example #10
Source File: BulkAccessorFactory.java From lams with GNU General Public License v2.0 | 6 votes |
/** * Declares a constructor that takes no parameter. * * @param classfile The class descriptor * * @throws CannotCompileException Indicates trouble with the underlying Javassist calls */ private void addDefaultConstructor(ClassFile classfile) throws CannotCompileException { final ConstPool constPool = classfile.getConstPool(); final String constructorSignature = "()V"; final MethodInfo constructorMethodInfo = new MethodInfo( constPool, MethodInfo.nameInit, constructorSignature ); final Bytecode code = new Bytecode( constPool, 0, 1 ); // aload_0 code.addAload( 0 ); // invokespecial code.addInvokespecial( BulkAccessor.class.getName(), MethodInfo.nameInit, constructorSignature ); // return code.addOpcode( Opcode.RETURN ); constructorMethodInfo.setCodeAttribute( code.toCodeAttribute() ); constructorMethodInfo.setAccessFlags( AccessFlag.PUBLIC ); classfile.addMethod( constructorMethodInfo ); }
Example #11
Source File: SpringMVCBinder.java From statefulj with Apache License 2.0 | 6 votes |
@Override protected void addEndpointMapping(CtMethod ctMethod, String method, String request) { MethodInfo methodInfo = ctMethod.getMethodInfo(); ConstPool constPool = methodInfo.getConstPool(); AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag); Annotation requestMapping = new Annotation(RequestMapping.class.getName(), constPool); ArrayMemberValue valueVals = new ArrayMemberValue(constPool); StringMemberValue valueVal = new StringMemberValue(constPool); valueVal.setValue(request); valueVals.setValue(new MemberValue[]{valueVal}); requestMapping.addMemberValue("value", valueVals); ArrayMemberValue methodVals = new ArrayMemberValue(constPool); EnumMemberValue methodVal = new EnumMemberValue(constPool); methodVal.setType(RequestMethod.class.getName()); methodVal.setValue(method); methodVals.setValue(new MemberValue[]{methodVal}); requestMapping.addMemberValue("method", methodVals); attr.addAnnotation(requestMapping); methodInfo.addAttribute(attr); }
Example #12
Source File: JavassistAdapter.java From panda with Apache License 2.0 | 6 votes |
@Override public boolean isPublic(@Nullable Object o) { if (o == null) { return false; } if (o instanceof ClassFile) { return AccessFlag.isPublic(((ClassFile) o).getAccessFlags()); } if (o instanceof MethodInfo) { return AccessFlag.isPublic(((MethodInfo) o).getAccessFlags()); } if (o instanceof FieldInfo) { return AccessFlag.isPublic(((FieldInfo) o).getAccessFlags()); } if (o instanceof Class) { return Modifier.isPublic(((Class) o).getModifiers()); } return false; }
Example #13
Source File: JClass.java From jadira with Apache License 2.0 | 6 votes |
public List<JConstructor> getConstructors() throws ClasspathAccessException { final List<JConstructor> retVal = new ArrayList<JConstructor>(); @SuppressWarnings("unchecked") final List<MethodInfo> methods = (List<MethodInfo>) getClassFile().getMethods(); for (MethodInfo next : methods) { if (next.isConstructor()) { retVal.add(JConstructor.getJConstructor(next, this, getResolver())); } } if (retVal.isEmpty()) { retVal.add(JDefaultConstructor.getJConstructor((MethodInfo)null, (JClass)this, getResolver())); } return retVal; }
Example #14
Source File: JavassistUtils.java From statefulj with Apache License 2.0 | 6 votes |
public static void addMethodAnnotations(CtMethod ctMethod, Method method) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { if (method != null) { MethodInfo methodInfo = ctMethod.getMethodInfo(); ConstPool constPool = methodInfo.getConstPool(); AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag); methodInfo.addAttribute(attr); for(java.lang.annotation.Annotation anno : method.getAnnotations()) { // If it's a Transition skip // TODO : Make this a parameterized set of Filters instead of hardcoding // Annotation clone = null; if (anno instanceof Transitions || anno instanceof Transition) { // skip } else { clone = cloneAnnotation(constPool, anno); attr.addAnnotation(clone); } } } }
Example #15
Source File: ClassHelper.java From TakinRPC with Apache License 2.0 | 6 votes |
/** * 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; }
Example #16
Source File: JavasisstUnitTest.java From tutorials with MIT License | 6 votes |
@Test public void givenLoadedClass_whenAddConstructorToClass_shouldCreateClassWithConstructor() throws NotFoundException, CannotCompileException, BadBytecode { // given ClassFile cf = ClassPool.getDefault().get("com.baeldung.javasisst.Point").getClassFile(); Bytecode code = new Bytecode(cf.getConstPool()); code.addAload(0); code.addInvokespecial("java/lang/Object", MethodInfo.nameInit, "()V"); code.addReturn(null); // when MethodInfo minfo = new MethodInfo(cf.getConstPool(), MethodInfo.nameInit, "()V"); minfo.setCodeAttribute(code.toCodeAttribute()); cf.addMethod(minfo); // then CodeIterator ci = code.toCodeAttribute().iterator(); List<String> operations = new LinkedList<>(); while (ci.hasNext()) { int index = ci.next(); int op = ci.byteAt(index); operations.add(Mnemonic.OPCODE[op]); } assertEquals(operations, Arrays.asList("aload_0", "invokespecial", "return")); }
Example #17
Source File: AbstractRestfulBinder.java From statefulj with Apache License 2.0 | 6 votes |
protected Annotation[] addIdParameter( CtMethod ctMethod, Class<?> idType, ClassPool cp) throws NotFoundException, CannotCompileException { // Clone the parameter Class // CtClass ctParm = cp.get(idType.getName()); // Add the parameter to the method // ctMethod.addParameter(ctParm); // Add the Parameter Annotations to the Method // MethodInfo methodInfo = ctMethod.getMethodInfo(); ConstPool constPool = methodInfo.getConstPool(); Annotation annot = new Annotation(getPathAnnotationClass().getName(), constPool); StringMemberValue valueVal = new StringMemberValue("id", constPool); annot.addMemberValue("value", valueVal); return new Annotation[]{ annot }; }
Example #18
Source File: ReflectionUtils.java From cola-cloud with MIT License | 6 votes |
private static String[] getMethodParamNames(CtMethod ctMethod) throws RuntimeException { CtClass ctClass = ctMethod.getDeclaringClass(); MethodInfo methodInfo = ctMethod.getMethodInfo(); CodeAttribute codeAttribute = methodInfo.getCodeAttribute(); LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag); if (attr == null) { throw new RuntimeException(ctClass.getName()); } try { String[] paramNames = new String[ctMethod.getParameterTypes().length]; int pos = Modifier.isStatic(ctMethod.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); } }
Example #19
Source File: ParamInfo.java From ModTheSpire with MIT License | 6 votes |
private static String findName(CtBehavior ctBehavior, int position) { MethodInfo methodInfo = ctBehavior.getMethodInfo2(); LocalVariableAttribute table = (LocalVariableAttribute) methodInfo.getCodeAttribute().getAttribute(LocalVariableAttribute.tag); if (table != null) { int j = 0; for (int i=0; i<table.tableLength(); ++i) { if (table.startPc(i) == 0) { if (j == 0 && !table.variableName(i).equals("this")) { ++j; // Skip to position 1 if `this` doesn't exist (static method) } if (j == position) { return table.variableName(i); } ++j; } } } return null; }
Example #20
Source File: CamelBinder.java From statefulj with Apache License 2.0 | 5 votes |
private void addConsumeAnnotation(CtMethod ctMethod, String uri) { MethodInfo methodInfo = ctMethod.getMethodInfo(); ConstPool constPool = methodInfo.getConstPool(); Annotation consume = new Annotation(Consume.class.getName(), constPool); StringMemberValue valueVal = new StringMemberValue(constPool); valueVal.setValue(uri); consume.addMemberValue("uri", valueVal); AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag); attr.addAnnotation(consume); methodInfo.addAttribute(attr); }
Example #21
Source File: AbstractRapidoidMojo.java From rapidoid with Apache License 2.0 | 5 votes |
private static boolean isMainMethod(MethodInfo method) { int flags = method.getAccessFlags(); return method.getName().equals("main") && Modifier.isPublic(flags) && Modifier.isStatic(flags) && U.eq(method.getDescriptor(), "([Ljava/lang/String;)V"); // TODO find more elegant solution }
Example #22
Source File: AbstractRapidoidMojo.java From rapidoid with Apache License 2.0 | 5 votes |
private static boolean hasMainMethod(ClassFile cls) { int flags = cls.getAccessFlags(); if (Modifier.isInterface(flags) || Modifier.isAnnotation(flags) || Modifier.isEnum(flags)) return false; for (Object m : cls.getMethods()) { if (m instanceof MethodInfo) { if (isMainMethod((MethodInfo) m)) return true; } } return false; }
Example #23
Source File: ClassFileJavassist.java From jbse with GNU General Public License v3.0 | 5 votes |
@Override public String getMethodGenericSignatureType(Signature methodSignature) throws MethodNotFoundException { final MethodInfo m = findMethodDeclaration(methodSignature); if (m == null) { throw new MethodNotFoundException(methodSignature.toString()); } final SignatureAttribute sa = (SignatureAttribute) m.getAttribute(SignatureAttribute.tag); return sa == null ? null : sa.getSignature(); }
Example #24
Source File: ClassFileJavassist.java From jbse with GNU General Public License v3.0 | 5 votes |
@Override public boolean isMethodPublic(Signature methodSignature) throws MethodNotFoundException { final MethodInfo m = findMethodDeclaration(methodSignature); if (m == null) { throw new MethodNotFoundException(methodSignature.toString()); } return Modifier.isPublic(AccessFlag.toModifier(m.getAccessFlags())); }
Example #25
Source File: ClassFileJavassist.java From jbse with GNU General Public License v3.0 | 5 votes |
@Override public boolean isMethodFinal(Signature methodSignature) throws MethodNotFoundException { final MethodInfo m = findMethodDeclaration(methodSignature); if (m == null) { throw new MethodNotFoundException(methodSignature.toString()); } return Modifier.isFinal(AccessFlag.toModifier(m.getAccessFlags())); }
Example #26
Source File: ClassFileJavassist.java From jbse with GNU General Public License v3.0 | 5 votes |
@Override public boolean isMethodVarargs(Signature methodSignature) throws MethodNotFoundException { final MethodInfo m = findMethodDeclaration(methodSignature); if (m == null) { throw new MethodNotFoundException(methodSignature.toString()); } return (AccessFlag.toModifier(m.getAccessFlags()) & Modifier.VARARGS) != 0; }
Example #27
Source File: ClassFileJavassist.java From jbse with GNU General Public License v3.0 | 5 votes |
@Override public boolean isMethodNative(Signature methodSignature) throws MethodNotFoundException { final MethodInfo m = findMethodDeclaration(methodSignature); if (m == null) { throw new MethodNotFoundException(methodSignature.toString()); } return Modifier.isNative(AccessFlag.toModifier(m.getAccessFlags())); }
Example #28
Source File: ClassFileJavassist.java From jbse with GNU General Public License v3.0 | 5 votes |
@Override public boolean isMethodAbstract(Signature methodSignature) throws MethodNotFoundException { final MethodInfo m = findMethodDeclaration(methodSignature); if (m == null) { throw new MethodNotFoundException(methodSignature.toString()); } return Modifier.isAbstract(AccessFlag.toModifier(m.getAccessFlags())); }
Example #29
Source File: JInterface.java From jadira with Apache License 2.0 | 5 votes |
public List<JMethod> getMethods() { final List<JMethod> retVal = new ArrayList<JMethod>(); @SuppressWarnings("unchecked") final List<MethodInfo> methods = (List<MethodInfo>) getClassFile().getMethods(); for (MethodInfo next : methods) { if (next.isMethod()) { retVal.add(JMethod.getJMethod(next, this, getResolver())); } } return retVal; }
Example #30
Source File: ClassFileJavassist.java From jbse with GNU General Public License v3.0 | 5 votes |
private CodeAttribute getMethodCodeAttribute(Signature methodSignature) throws MethodNotFoundException, MethodCodeNotFoundException { final MethodInfo m = findMethodDeclaration(methodSignature); if (m == null) { throw new MethodNotFoundException(methodSignature.toString()); } final CodeAttribute ca = m.getCodeAttribute(); if (ca == null) { throw new MethodCodeNotFoundException(methodSignature.toString()); } return ca; }