List of usage examples for org.objectweb.asm.commons Method getName
public String getName()
From source file:erjang.beam.BuiltInFunction.java
License:Apache License
/** * @param m//from w w w . j a va2 s . co m */ public BuiltInFunction(java.lang.reflect.Method m) { this.javaMethod = m; this.owner = Type.getType(m.getDeclaringClass()); this.method = new Method(m.getName(), Type.getType(m.getReturnType()), Type.getArgumentTypes(m)); isVirtual = !Modifier.isStatic(m.getModifiers()); boolean p = false; for (Class c : m.getExceptionTypes()) { if (Pausable.class.equals(c)) { p = true; break; } } isPausable = p; }
From source file:io.datakernel.codegen.AsmBuilder.java
License:Apache License
/** * Creates a new method for a dynamic class. The method must be part of the provided interfaces or abstract class. * * @param methodName name of method/*from w w w.j av a 2s .c o m*/ * @param expression function which will be processed * @return changed AsmFunctionFactory */ public AsmBuilder<T> method(String methodName, Expression expression) { if (methodName.contains("(")) { Method method = Method.getMethod(methodName); return method(method, expression); } Method foundMethod = null; List<List<java.lang.reflect.Method>> listOfMethods = new ArrayList<>(); listOfMethods.add(asList(Object.class.getMethods())); for (Class<?> type : scope.getParentClasses()) { listOfMethods.add(asList(type.getMethods())); listOfMethods.add(asList(type.getDeclaredMethods())); } for (List<java.lang.reflect.Method> list : listOfMethods) { for (java.lang.reflect.Method m : list) { if (m.getName().equals(methodName)) { Method method = getMethod(m); if (foundMethod != null && !method.equals(foundMethod)) throw new IllegalArgumentException("Method " + method + " collides with " + foundMethod); foundMethod = method; } } } Preconditions.check(foundMethod != null, "Could not find method '" + methodName + "'"); return method(foundMethod, expression); }
From source file:io.datakernel.codegen.AsmBuilder.java
License:Apache License
/** * Returns a new class which is created in a dynamic way * * @param key key/*from ww w . j a v a 2 s. c o m*/ * @return completed class */ private Class<T> defineNewClass(AsmClassKey<T> key, String newClassName) { DefiningClassWriter cw = new DefiningClassWriter(classLoader); String className; if (newClassName == null) { className = DEFAULT_CLASS_NAME + COUNTER.incrementAndGet(); } else { className = newClassName; } Type classType = getType('L' + className.replace('.', '/') + ';'); // contains all classes (abstract and interfaces) final Set<Class<?>> parentClasses = scope.getParentClasses(); final String[] internalNames = new String[parentClasses.size()]; int pos = 0; for (Class<?> clazz : parentClasses) internalNames[pos++] = getInternalName(clazz); if (scope.getMainType().isInterface()) { cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, classType.getInternalName(), null, "java/lang/Object", internalNames); } else { cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, classType.getInternalName(), null, internalNames[0], Arrays.copyOfRange(internalNames, 1, internalNames.length)); } { Method m = getMethod("void <init> ()"); GeneratorAdapter g = new GeneratorAdapter(ACC_PUBLIC, m, null, null, cw); g.loadThis(); if (scope.getMainType().isInterface()) { g.invokeConstructor(getType(Object.class), m); } else { g.invokeConstructor(getType(scope.getMainType()), m); } g.returnValue(); g.endMethod(); } for (String field : fields.keySet()) { Class<?> fieldClass = fields.get(field); cw.visitField(ACC_PUBLIC, field, getType(fieldClass).getDescriptor(), null, null); } for (String field : staticFields.keySet()) { Class<?> fieldClass = staticFields.get(field); cw.visitField(ACC_PUBLIC + ACC_STATIC, field, getType(fieldClass).getDescriptor(), null, null); } for (Method m : expressionStaticMap.keySet()) { try { GeneratorAdapter g = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC + ACC_FINAL, m, null, null, cw); Context ctx = new Context(classLoader, g, classType, scope.getParentClasses(), Collections.EMPTY_MAP, scope.getStaticFields(), m.getArgumentTypes(), m, scope.getMethods(), scope.getStaticMethods()); Expression expression = expressionStaticMap.get(m); loadAndCast(ctx, expression, m.getReturnType()); g.returnValue(); g.endMethod(); } catch (Exception e) { throw new RuntimeException("Unable to implement " + m.getName() + m.getDescriptor(), e); } } for (Method m : expressionMap.keySet()) { try { GeneratorAdapter g = new GeneratorAdapter(ACC_PUBLIC + ACC_FINAL, m, null, null, cw); Context ctx = new Context(classLoader, g, classType, scope.getParentClasses(), scope.getFields(), scope.getStaticFields(), m.getArgumentTypes(), m, scope.getMethods(), scope.getStaticMethods()); Expression expression = expressionMap.get(m); loadAndCast(ctx, expression, m.getReturnType()); g.returnValue(); g.endMethod(); } catch (Exception e) { throw new RuntimeException("Unable to implement " + m.getName() + m.getDescriptor(), e); } } if (bytecodeSaveDir != null) { try (FileOutputStream fos = new FileOutputStream( bytecodeSaveDir.resolve(className + ".class").toFile())) { fos.write(cw.toByteArray()); } catch (IOException e) { throw new RuntimeException(e); } } cw.visitEnd(); Class<?> definedClass = classLoader.defineClass(className, key, cw.toByteArray()); logger.trace("Defined new {} for key {}", definedClass, key); return (Class<T>) definedClass; }
From source file:io.datakernel.codegen.ExpressionCallStaticSelf.java
License:Apache License
@Override public Type type(Context ctx) { List<Type> argumentTypes = new ArrayList<>(); for (Expression argument : arguments) { argumentTypes.add(argument.type(ctx)); }/*from w w w.ja v a2s .co m*/ Set<Method> methods = ctx.getStaticMethods(); for (Method m : methods) { if (m.getName().equals(methodName)) { if (m.getArgumentTypes().length == argumentTypes.size()) { Type[] methodTypes = m.getArgumentTypes(); boolean isSame = true; for (int i = 0; i < argumentTypes.size(); i++) { if (!methodTypes[i].equals(argumentTypes.get(i))) { isSame = false; break; } } if (isSame) { return m.getReturnType(); } } } } throw new RuntimeException(format("No method %s.%s(%s). %s", owner.type(ctx).getClassName(), methodName, (!argumentTypes.isEmpty() ? argsToString(argumentClasses(ctx, arguments)) : ""), exceptionInGeneratedClass(ctx))); }
From source file:net.wpm.codegen.ClassBuilder.java
License:Apache License
/** * Creates a new method for a dynamic class. The method must be part of the provided interfaces or abstract class. * * @param methodName name of method//ww w .j a v a 2 s .c om * @param expression function which will be processed * @return changed AsmFunctionFactory */ public ClassBuilder<T> method(String methodName, Expression expression) { if (methodName.contains("(")) { Method method = Method.getMethod(methodName); return method(method, expression); } Method foundMethod = null; List<List<java.lang.reflect.Method>> listOfMethods = new ArrayList<List<java.lang.reflect.Method>>(); listOfMethods.add(asList(Object.class.getMethods())); for (Class<?> type : scope.getParentClasses()) { listOfMethods.add(asList(type.getMethods())); listOfMethods.add(asList(type.getDeclaredMethods())); } for (List<java.lang.reflect.Method> list : listOfMethods) { for (java.lang.reflect.Method m : list) { if (m.getName().equals(methodName)) { Method method = getMethod(m); if (foundMethod != null && !method.equals(foundMethod)) throw new IllegalArgumentException("Method " + method + " collides with " + foundMethod); foundMethod = method; } } } Preconditions.check(foundMethod != null, "Could not find method '" + methodName + "'"); return method(foundMethod, expression); }
From source file:net.wpm.codegen.ClassBuilder.java
License:Apache License
/** * Returns a new class which is created in a dynamic way * * @param key key/* www . j a va 2 s.c om*/ * @return completed class */ private Class<T> defineNewClass(AsmClassKey<T> key, String newClassName) { DefiningClassWriter cw = new DefiningClassWriter(classLoader); String className; if (newClassName == null) { className = DEFAULT_CLASS_NAME + COUNTER.incrementAndGet(); } else { className = newClassName; } Type classType = getType('L' + className.replace('.', '/') + ';'); // contains all classes (abstract and interfaces) final Set<Class<?>> parentClasses = scope.getParentClasses(); final String[] internalNames = new String[parentClasses.size()]; int pos = 0; for (Class<?> clazz : parentClasses) internalNames[pos++] = getInternalName(clazz); if (scope.getMainType().isInterface()) { cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, classType.getInternalName(), null, "java/lang/Object", internalNames); } else { cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, classType.getInternalName(), null, internalNames[0], Arrays.copyOfRange(internalNames, 1, internalNames.length)); } { Method m = getMethod("void <init> ()"); GeneratorAdapter g = new GeneratorAdapter(ACC_PUBLIC, m, null, null, cw); if (constructor != null) { Context ctx = new Context(classLoader, g, classType, scope.getParentClasses(), scope.getFields(), scope.getStaticFields(), m.getArgumentTypes(), m, scope.getMethods(), scope.getStaticMethods()); loadAndCast(ctx, constructor, m.getReturnType()); } g.loadThis(); if (scope.getMainType().isInterface()) { g.invokeConstructor(getType(Object.class), m); } else { g.invokeConstructor(getType(scope.getMainType()), m); } g.returnValue(); g.endMethod(); } for (String field : fields.keySet()) { Class<?> fieldClass = fields.get(field); cw.visitField(ACC_PUBLIC, field, getType(fieldClass).getDescriptor(), null, null); } for (String field : staticFields.keySet()) { Class<?> fieldClass = staticFields.get(field); cw.visitField(ACC_PUBLIC + ACC_STATIC, field, getType(fieldClass).getDescriptor(), null, null); } for (String field : staticConstants.keySet()) { Class<?> fieldClass = staticConstants.get(field); cw.visitField(ACC_PUBLIC + ACC_STATIC + ACC_FINAL, field, getType(fieldClass).getDescriptor(), null, null); } for (Method m : staticMethods.keySet()) { try { GeneratorAdapter g = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC + ACC_FINAL, m, null, null, cw); Context ctx = new Context(classLoader, g, classType, scope.getParentClasses(), Collections.EMPTY_MAP, scope.getStaticFields(), m.getArgumentTypes(), m, scope.getMethods(), scope.getStaticMethods()); Expression expression = staticMethods.get(m); loadAndCast(ctx, expression, m.getReturnType()); g.returnValue(); g.endMethod(); } catch (Exception e) { throw new RuntimeException("Unable to implement " + m.getName() + m.getDescriptor(), e); } } for (Method m : methods.keySet()) { try { GeneratorAdapter g = new GeneratorAdapter(ACC_PUBLIC + ACC_FINAL, m, null, null, cw); Context ctx = new Context(classLoader, g, classType, scope.getParentClasses(), scope.getFields(), scope.getStaticFields(), m.getArgumentTypes(), m, scope.getMethods(), scope.getStaticMethods()); Expression expression = methods.get(m); loadAndCast(ctx, expression, m.getReturnType()); g.returnValue(); g.endMethod(); } catch (Exception e) { throw new RuntimeException("Unable to implement " + m.getName() + m.getDescriptor(), e); } } if (bytecodeSaveDir != null) { FileOutputStream fos = null; try { fos = new FileOutputStream(bytecodeSaveDir.resolve(className + ".class").toFile()); fos.write(cw.toByteArray()); fos.close(); } catch (IOException e) { try { if (fos != null) fos.close(); } catch (IOException ioe) { throw new RuntimeException(ioe); } throw new RuntimeException(e); } } cw.visitEnd(); Class<?> definedClass = classLoader.defineClass(className, key, cw.toByteArray()); logger.trace("Defined new {} for key {}", definedClass, key); return (Class<T>) definedClass; }
From source file:net.wpm.codegen.ExpressionCallStaticSelf.java
License:Apache License
@Override public Type type(Context ctx) { List<Type> argumentTypes = new ArrayList<Type>(); for (Expression argument : arguments) { argumentTypes.add(argument.type(ctx)); }/* w w w . j av a 2s .c o m*/ Set<Method> methods = ctx.getStaticMethods(); for (Method m : methods) { if (m.getName().equals(methodName)) { if (m.getArgumentTypes().length == argumentTypes.size()) { Type[] methodTypes = m.getArgumentTypes(); boolean isSame = true; for (int i = 0; i < argumentTypes.size(); i++) { if (!methodTypes[i].equals(argumentTypes.get(i))) { isSame = false; break; } } if (isSame) { return m.getReturnType(); } } } } throw new RuntimeException(format("No method %s.%s(%s). %s", owner.type(ctx).getClassName(), methodName, (!argumentTypes.isEmpty() ? argsToString(argumentClasses(ctx, arguments)) : ""), exceptionInGeneratedClass(ctx))); }
From source file:nl.hardijzer.fw.checkabstract.ClassInfo.java
License:Open Source License
private static String methodToString(Method m) { StringBuilder sb = new StringBuilder(); sb.append(typeToString(m.getReturnType())); sb.append(" "); sb.append(m.getName()); sb.append("("); boolean bFirst = true; for (Type t : m.getArgumentTypes()) { if (!bFirst) sb.append(", "); bFirst = false;// w w w. j a v a2 s. c o m sb.append(typeToString(t)); } sb.append(")"); return sb.toString(); }
From source file:org.apache.aries.proxy.impl.interfaces.InterfaceCombiningClassAdapter.java
License:Apache License
private void visitAbstractMethods(Class<?> clazz) { for (java.lang.reflect.Method m : clazz.getDeclaredMethods()) { int modifiers = m.getModifiers(); if ((modifiers & Modifier.ABSTRACT) != 0) { List<String> exceptions = new ArrayList<String>(); for (Class<?> c : m.getExceptionTypes()) { exceptions.add(Type.getInternalName(c)); }/*w ww .j a v a 2s.c o m*/ MethodVisitor visitor = visitMethod(modifiers, m.getName(), Method.getMethod(m).getDescriptor(), null, exceptions.toArray(new String[exceptions.size()])); if (visitor != null) visitor.visitEnd(); } } }
From source file:org.apache.commons.weaver.privilizer.ActionGenerator.java
License:Apache License
private static String generateName(final Method methd) { final StringBuilder buf = new StringBuilder(methd.getName()); if (methd.getArgumentTypes().length > 0) { buf.append("$$"); for (final Type arg : methd.getArgumentTypes()) { buf.append(arg.getDescriptor().replace("[", "arrayOf").replace('/', '_').replace(';', '$')); }//from ww w . ja va2s . c om } return buf.append("_ACTION").toString(); }