本文整理了Java中javassist.CtMethod
类的一些代码示例,展示了CtMethod
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。CtMethod
类的具体详情如下:
包路径:javassist.CtMethod
类名称:CtMethod
[英]An instance of CtMethod
represents a method.
See the super class CtBehavior
since a number of useful methods are in CtBehavior
. A number of useful factory methods are in CtNewMethod
.
[中]CtMethod
的实例表示一个方法。
请参阅超类CtBehavior
,因为CtBehavior
中有许多有用的方法。CtNewMethod
中有许多有用的工厂方法。
代码示例来源:origin: redisson/redisson
String name = m.getName();
if (m.getDeclaringClass() == clazz) {
if (Modifier.isNative(mod))
return;
if (Modifier.isFinal(mod)) {
mod &= ~Modifier.FINAL;
m2.setModifiers(mod);
if (Modifier.isFinal(mod))
return;
m2.setModifiers(mod);
clazz.addMethod(m2);
m2.setName(ClassMetaobject.methodPrefix + identifier
+ "_" + name);
= CtNewMethod.wrapped(m.getReturnType(), name,
m.getParameterTypes(), m.getExceptionTypes(),
body, ConstParameter.integer(identifier),
clazz);
wmethod.setModifiers(mod);
clazz.addMethod(wmethod);
代码示例来源:origin: redisson/redisson
private String getMethodString(CtMethod method) {
try {
return Modifier.toString(method.getModifiers()) + " "
+ method.getReturnType().getName() + " " + method.getName()
+ Descriptor.toString(method.getSignature()) + ";";
} catch (NotFoundException e) {
throw new RuntimeException(e);
}
}
代码示例来源:origin: com.zaxxer/HikariCP
private static void modifyProxyFactory() throws NotFoundException, CannotCompileException, IOException {
System.out.println("Generating method bodies for com.zaxxer.hikari.proxy.ProxyFactory");
String packageName = ProxyConnection.class.getPackage().getName();
CtClass proxyCt = classPool.getCtClass("com.zaxxer.hikari.pool.ProxyFactory");
for (CtMethod method : proxyCt.getMethods()) {
switch (method.getName()) {
case "getProxyConnection":
method.setBody("{return new " + packageName + ".HikariProxyConnection($$);}");
break;
case "getProxyStatement":
method.setBody("{return new " + packageName + ".HikariProxyStatement($$);}");
break;
case "getProxyPreparedStatement":
method.setBody("{return new " + packageName + ".HikariProxyPreparedStatement($$);}");
break;
case "getProxyCallableStatement":
method.setBody("{return new " + packageName + ".HikariProxyCallableStatement($$);}");
break;
case "getProxyResultSet":
method.setBody("{return new " + packageName + ".HikariProxyResultSet($$);}");
break;
default:
// unhandled method
break;
}
}
proxyCt.writeFile(genDirectory + "target/classes");
}
代码示例来源:origin: redisson/redisson
/**
* Constructs a control-flow analyzer for the given method.
*/
public ControlFlow(CtMethod method) throws BadBytecode {
this(method.getDeclaringClass(), method.getMethodInfo2());
}
代码示例来源:origin: redisson/redisson
public TransformCall(Transformer next, String oldMethodName,
CtMethod substMethod)
{
super(next);
methodname = oldMethodName;
methodDescriptor = substMethod.getMethodInfo2().getDescriptor();
classname = newClassname = substMethod.getDeclaringClass().getName();
newMethodname = substMethod.getName();
constPool = null;
newMethodIsPrivate = Modifier.isPrivate(substMethod.getModifiers());
}
代码示例来源:origin: redisson/redisson
public TransformCall(Transformer next, CtMethod origMethod,
CtMethod substMethod)
{
this(next, origMethod.getName(), substMethod);
classname = origMethod.getDeclaringClass().getName();
}
代码示例来源:origin: BroadleafCommerce/BroadleafCommerce
if (xformTemplates.containsKey(xformKey)) {
buildXFormVals.addAll(Arrays.asList(xformTemplates.get(xformKey).split(",")));
classPool = ClassPool.getDefault();
clazz = classPool.makeClass(new ByteArrayInputStream(classfileBuffer), false);
classPool = ClassPool.getDefault();
StringUtils.join(xformVals, ",")));
clazz.defrost();
ConstPool constantPool = classFile.getConstPool();
buildClassLevelAnnotations(classFile, templateFile, constantPool);
if (method.hasAnnotation(NonCopied.class)) {
logger.debug(String.format("Not adding method [%s]", method.getName()));
} else {
try {
CtClass[] paramTypes = method.getParameterTypes();
CtMethod originalMethod = clazz.getDeclaredMethod(method.getName(), paramTypes);
logger.debug(String.format("Removing method [%s]", method.getName()));
if (xformRenameMethodOverlaps != null && xformRenameMethodOverlaps[index]) {
originalMethod.setName(renameMethodPrefix + method.getName());
logger.debug(String.format("Adding method [%s]", method.getName()));
CtMethod copiedMethod = new CtMethod(method, clazz, null);
代码示例来源:origin: changmingxie/tcc-transaction
ConstPool constpool = mCtc.getClassFile().getConstPool();
AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
Annotation annot = new Annotation("org.mengyun.tcctransaction.api.Compensable", constpool);
enumMemberValue.setValue("SUPPORTS");
annot.addMemberValue("propagation", enumMemberValue);
annot.addMemberValue("confirmMethod", new StringMemberValue(ctMethod.getName(), constpool));
annot.addMemberValue("cancelMethod", new StringMemberValue(ctMethod.getName(), constpool));
ctMethod.getMethodInfo().addAttribute(attr);
throw new RuntimeException(e.getMessage(), e);
} catch (CannotCompileException e) {
throw new RuntimeException(e.getMessage(), e);
代码示例来源:origin: orbit/orbit
final CtClass cc = pool.makeClass(referenceFullName);
final CtClass ccInterface = pool.get(aInterface.getName());
final CtClass ccActorReference = pool.get(RemoteReference.class.getName());
cc.setSuperclass(ccActorReference);
cc.addInterface(ccInterface);
cc.addConstructor(CtNewConstructor.make(new CtClass[]{ pool.get(String.class.getName()) }, null, "{ super($1); }", cc));
for (final CtMethod m : ccInterface.getMethods())
if (!m.getDeclaringClass().isInterface() || !m.getReturnType().getName().equals(Task.class.getName()))
final boolean oneWay = m.hasAnnotation(OneWay.class);
final String methodName = m.getName();
final CtClass[] parameterTypes = m.getParameterTypes();
cc.addField(CtField.make("private static java.lang.reflect.Method " + methodReferenceField + " = null;", cc));
final CtMethod newMethod = CtNewMethod.make(m.getReturnType(), methodName,
parameterTypes, m.getExceptionTypes(),
"{ return super.invoke(" + lazyMethodReferenceInit + ", " + oneWay + ", " + methodId + ", $args); }",
cc);
代码示例来源:origin: redisson/redisson
public <T> Class<T> createStructClz( Class<T> clazz ) throws Exception {
CtMethod[] methods = orig.getMethods();
for (int i = 0; i < methods.length; i++) {
CtMethod method = methods[i];
final Class curClz = Class.forName( method.getDeclaringClass().getName() );
boolean allowed = ((method.getModifiers() & AccessFlag.ABSTRACT) == 0 ) &&
(method.getModifiers() & AccessFlag.NATIVE) == 0 &&
(method.getModifiers() & AccessFlag.FINAL) == 0 &&
( !method.getDeclaringClass().getName().equals(FSTStruct.class.getName())
||method.getName().equals("getFieldValues")) &&
! method.getDeclaringClass().getName().equals(Object.class.getName());
allowed &= method.getAnnotation(NoAssist.class) == null;
allowed &= (method.getModifiers() & AccessFlag.STATIC) == 0;
if ( allowed && (method.getModifiers() & AccessFlag.FINAL) != 0 && ! method.getDeclaringClass().getName().equals("java.lang.Object") ) {
throw new RuntimeException("final methods are not allowed for struct classes:"+method.getName());
if ( allowed && (method.getModifiers() & AccessFlag.PRIVATE) != 0 && ! method.getDeclaringClass().getName().equals("java.lang.Object")) {
throw new RuntimeException("private methods are not allowed for struct classes:"+method.getName());
method = new CtMethod(method,newClz,mp);
String methName = method.getName();
method.setBody(body.toString());
method.instrument( new ExprEditor() {
@Override
public void edit(FieldAccess f) throws CannotCompileException {
代码示例来源:origin: orbit/orbit
final CtClass cc = pool.makeClass(invokerFullName);
final String className = actorClass.getName();
final CtClass ccInterface = pool.get(className);
final CtClass ccActorInvoker = pool.get(ObjectInvoker.class.getName());
cc.setSuperclass(ccActorInvoker);
final CtMethod[] declaredMethods = ccInterface.getMethods();
invokerBody.append(" switch(methodId) { ");
if (!m.getReturnType().getName().equals(Task.class.getName())
|| !Modifier.isPublic(m.getModifiers())
|| Modifier.isStatic(m.getModifiers()))
final CtClass[] parameterTypes = m.getParameterTypes();
final String methodName = m.getName();
final int methodId = computeMethodId(methodName, parameterTypes);
+ ((parameterTypes.length > 0) ? "[]{" + parameterTypesStr + "}" : "[0]")
+ ");";
cc.addField(CtField.make(src, cc));
getMethodBody.append("case " + methodId + ": return " + methodField + ";");
代码示例来源:origin: opensourceBIM/BIMserver
private void build2(Class<? extends PublicInterface> interfaceClass, org.bimserver.shared.meta.SService sService) {
try {
CtClass reflectorImplClass = pool.makeClass(GENERATED_CLASSES_PACKAGE + "." + interfaceClass.getSimpleName() + "Reflector" + implementationCounter);
CtClass reflectorClass = pool.get(Reflector.class.getName());
CtClass interfaceCtClass = pool.get(interfaceClass.getName());
reflectorImplClass.addInterface(reflectorClass);
CtField reflectorField = new CtField(interfaceCtClass, "publicInterface", reflectorImplClass);
reflectorImplClass.addField(reflectorField);
CtConstructor constructor = new CtConstructor(new CtClass[] {interfaceCtClass}, reflectorImplClass);
StringBuilder sb = new StringBuilder();
reflectorImplClass.addConstructor(constructor);
sb.append("{");
sb.append("this.publicInterface = $1;");
parameters[2] = pool.get(Class.class.getName());
parameters[3] = pool.get(KeyValuePair.class.getName() + "[]");
CtMethod method = new CtMethod(pool.get(Object.class.getName()), "callMethod", parameters, reflectorImplClass);
method.setBody(methodBuilder.toString());
reflectorImplClass.addMethod(method);
代码示例来源:origin: apache/ignite
ClassPool cp = ClassPool.getDefault();
cp.insertClassPath(new ClassClassPath(ConfigVariationsTestSuiteBuilder.class));
CtClass cl = cp.makeClass(pkg + "." + clsName);
cl.setSuperclass(cp.get(cls.getName()));
ClassFile ccFile = cl.getClassFile();
ConstPool constpool = ccFile.getConstPool();
mtd.getMethodInfo().addAttribute(attr);
cl.addMethod(mtd);
return cl.toClass();
代码示例来源:origin: com.zaxxer/HikariCP
targetCt.setModifiers(Modifier.PUBLIC);
for (CtMethod method : superCt.getMethods()) {
if ((method.getModifiers() & Modifier.FINAL) == Modifier.FINAL) {
superSigs.add(method.getName() + method.getSignature());
CtClass intfCt = classPool.getCtClass(intf.getName());
targetCt.addInterface(intfCt);
for (CtMethod intfMethod : intfCt.getDeclaredMethods()) {
final String signature = intfMethod.getName() + intfMethod.getSignature();
CtMethod superMethod = superCt.getMethod(intfMethod.getName(), intfMethod.getSignature());
if ((superMethod.getModifiers() & Modifier.ABSTRACT) != Modifier.ABSTRACT && !isDefaultMethod(intf, intfMethod)) {
modifiedBody = modifiedBody.replace("((cast) ", "");
modifiedBody = modifiedBody.replace("delegate", "super");
modifiedBody = modifiedBody.replace("method", method.getName());
modifiedBody = "{ return ((cast) delegate).method($$); }".replace("method", method.getName()).replace("cast", primaryInterface.getName());
if (method.getReturnType() == CtClass.voidType) {
modifiedBody = modifiedBody.replace("return", "");
method.setBody(modifiedBody);
targetCt.addMethod(method);
targetCt.getClassFile().setMajorVersion(ClassFile.JAVA_8);
targetCt.writeFile(genDirectory + "target/classes");
代码示例来源:origin: Meituan-Dianping/Robust
if (isNeedInsertClass(ctClass.getName())) {
ctClass.setModifiers(AccessFlag.setPublic(ctClass.getModifiers()));
if (ctClass.isInterface() || ctClass.getDeclaredMethods().length < 1) {
ClassPool classPool = ctBehavior.getDeclaringClass().getClassPool();
CtClass type = classPool.getOrNull(Constants.INTERFACE_NAME);
CtField ctField = new CtField(type, Constants.INSERT_FIELD_NAME, ctClass);
ctField.setModifiers(AccessFlag.PUBLIC | AccessFlag.STATIC);
ctClass.addField(ctField);
if (ctBehavior.getMethodInfo().isMethod()) {
CtMethod ctMethod = (CtMethod) ctBehavior;
boolean isStatic = (ctMethod.getModifiers() & AccessFlag.STATIC) != 0;
CtClass returnType = ctMethod.getReturnType();
String returnTypeString = returnType.getName();
代码示例来源:origin: javalite/activejdbc
private void doInstrument(CtClass target) throws NotFoundException, CannotCompileException {
CtMethod[] modelMethods = modelClass.getDeclaredMethods();
CtMethod[] targetMethods = target.getDeclaredMethods();
CtMethod modelGetClass = modelClass.getDeclaredMethod("modelClass");
CtMethod newGetClass = CtNewMethod.copy(modelGetClass, target, null);
newGetClass.setBody("{ return " + target.getName() + ".class; }");
int modifiers = method.getModifiers();
if (Modifier.isStatic(modifiers)) {
if (targetHasMethod(targetMethods, method)) {
Logger.debug("Detected method: " + method.getName() + ", skipping delegate.");
} else {
CtMethod newMethod;
if (Modifier.isProtected(modifiers) || Modifier.isPublic(modifiers)) {
newMethod = CtNewMethod.copy(method, target, classMap);
newMethod.instrument(conv);
} else if ("modelClass".equals(method.getName())) {
newMethod = newGetClass;
} else {
for (Object attr : method.getMethodInfo().getAttributes()) {
if (attr instanceof SignatureAttribute) {
newMethod.getMethodInfo().addAttribute((SignatureAttribute) attr);
代码示例来源:origin: redisson/redisson
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(bout);
ClassFile classFile = clazz.getClassFile();
out.writeUTF(javaName);
CtMethod[] methods = clazz.getDeclaredMethods();
int classMods = clazz.getModifiers();
if ((classMods & Modifier.INTERFACE) != 0)
if (methods.length > 0)
int mods = method.getModifiers()
& (Modifier.PUBLIC | Modifier.PRIVATE
| Modifier.PROTECTED | Modifier.STATIC
| Modifier.NATIVE | Modifier.ABSTRACT | Modifier.STRICT);
if ((mods & Modifier.PRIVATE) == 0) {
out.writeUTF(method.getName());
out.writeInt(mods);
out.writeUTF(method.getMethodInfo2()
.getDescriptor().replace('/', '.'));
throw new CannotCompileException(e);
throw new CannotCompileException(e);
代码示例来源:origin: redisson/redisson
private static CtMethod delegator0(CtMethod delegate, CtClass declaring)
throws CannotCompileException, NotFoundException
MethodInfo deleInfo = delegate.getMethodInfo2();
String methodName = deleInfo.getName();
String desc = deleInfo.getDescriptor();
ConstPool cp = declaring.getClassFile2().getConstPool();
MethodInfo minfo = new MethodInfo(cp, methodName, desc);
minfo.setAccessFlags(deleInfo.getAccessFlags());
boolean isStatic = Modifier.isStatic(delegate.getModifiers());
CtClass deleClass = delegate.getDeclaringClass();
CtClass[] params = delegate.getParameterTypes();
int s;
if (isStatic) {
code.addReturn(delegate.getReturnType());
code.setMaxLocals(++s);
code.setMaxStack(s < 2 ? 2 : s); // for a 2-word return value
minfo.setCodeAttribute(code.toCodeAttribute());
return new CtMethod(minfo, declaring);
代码示例来源:origin: uber-common/jvm-profiler
ClassPool classPool = new ClassPool();
classPool.appendClassPath(new LoaderClassPath(loader));
final CtClass ctClass;
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(classfileBuffer)) {
ctClass = classPool.makeClass(byteArrayInputStream);
CtMethod[] ctMethods = ctClass.getDeclaredMethods();
for (CtMethod ctMethod : ctMethods) {
boolean enableDurationProfiling = durationProfilingFilter.matchMethod(ctClass.getName(), ctMethod.getName());
List<Integer> enableArgumentProfiler = argumentFilterProfilingFilter.matchMethod(ctClass.getName(), ctMethod.getName());
transformMethod(normalizedClassName, ctMethod, enableDurationProfiling, enableArgumentProfiler);
代码示例来源:origin: Meituan-Dianping/Robust
for (CtMethod ctMethod : invokeSuperMethodList) {
if ((ctMethod.getName().replaceAll("\\.", "/") + ctMethod.getSignature().subSequence(0, ctMethod.getSignature().indexOf(")") + 1)).equals(getMethodSignureInSmaliLine(line))) {
result = line.replace(Constants.SMALI_INVOKE_VIRTUAL_COMMAND, Constants.SMALI_INVOKE_SUPER_COMMAND);
try {
if (!ctMethod.getReturnType().isPrimitive()) {
returnType = "L" + ctMethod.getReturnType().getName().replaceAll("\\.", "/");
} else {
returnType = String.valueOf(((CtPrimitiveType) ctMethod.getReturnType()).getDescriptor());
String fullClassNameInSmali = ctMethod.getDeclaringClass().getClassPool().get(fullClassName).getSuperclass().getName().replaceAll("\\.", "/");
result = result.replace(result.substring(result.indexOf(PACKNAME_START) + 1, result.indexOf(PACKNAME_END)), fullClassNameInSmali);
result = result.substring(0, result.indexOf(")") + 1) + returnType;
if (!ctMethod.getReturnType().isPrimitive()) {
result += ";";
内容来源于网络,如有侵权,请联系作者删除!