ca.aload(0); ca.ldc(idlName); ca.getstatic(asm.getName(), strategyField, StubStrategy.class); ca.iconst(0); ca.anewarray(Object.class.getName()); ca.iconst(paramTypes.length); ca.anewarray(Object.class.getName()); int index = 1; for (int j = 0; j < paramTypes.length; j++) { ca.dup(); ca.iconst(j); if (!type.isPrimitive()) { ca.aload(index); } else if (type.equals(double.class)) { ca.dload(index); Boxing.boxDouble(ca); index++; } else if (type.equals(long.class)) { ca.lload(index); Boxing.boxLong(ca); index++; } else if (type.equals(float.class)) { ca.fload(index); Boxing.boxFloat(ca); } else {
c.aload(BYTE_BUFFER_VAR); c.invokevirtual(ByteBuffer.class.getName(), "hasRemaining", "()Z"); final BranchEnd nonZero = c.ifne(); c.iconst(0); c.returnInstruction(); c.branchEnd(nonZero); c.aload(PARSE_STATE_VAR); c.dup(); c.getfield(parseStateClass, "stringBuilder", DescriptorUtils.makeDescriptor(StringBuilder.class)); c.astore(STATE_STRING_BUILDER_VAR); c.dup(); c.getfield(parseStateClass, "parseState", "I"); c.dup(); c.istore(CURRENT_STATE_VAR); BranchEnd optimizationEnd = c.ifeq(); c.dup(); c.getfield(parseStateClass, "pos", "I"); c.istore(STATE_POS_VAR); c.dup(); c.getfield(parseStateClass, "current", HTTP_STRING_DESCRIPTOR); c.astore(STATE_CURRENT_VAR); c.getfield(parseStateClass, "currentBytes", "[B"); c.astore(STATE_CURRENT_BYTES_VAR); c.iload(CURRENT_STATE_VAR);
@Override public void handleOtherToken(final CodeAttribute c) { c.aload(HTTP_RESULT); c.swap(); c.dup(); c.invokestatic(CONNECTORS_CLASS, "verifyToken", "(" + HTTP_STRING_DESCRIPTOR + ")V"); c.invokevirtual(resultClass, "setRequestMethod", "(" + HTTP_STRING_DESCRIPTOR + ")" + HTTP_EXCHANGE_DESCRIPTOR); c.pop(); }
b.aload(0); b.getfield(classMethod.getClassFile().getName(), PRIVATE_METHOD_HANDLER_FIELD_NAME, DescriptorUtils.makeDescriptor(MethodHandler.class)); b.aload(0); b.aconstNull(); b.iconst(methodInfo.getParameterTypes().length); b.anewarray(Object.class.getName()); int localVariableCount = 1; for (int i = 0; i < methodInfo.getParameterTypes().length; ++i) { String typeString = methodInfo.getParameterTypes()[i]; b.dup(); // duplicate the array reference b.iconst(i); b.aastore(); if (isWide(typeString)) { localVariableCount = localVariableCount + 2; } else { b.invokeinterface(MethodHandler.class.getName(), INVOKE_METHOD_NAME, LJAVA_LANG_OBJECT, new String[] { LJAVA_LANG_OBJECT, LJAVA_LANG_REFLECT_METHOD, LJAVA_LANG_REFLECT_METHOD, "[" + LJAVA_LANG_OBJECT }); if (methodInfo.getReturnType().equals(BytecodeUtils.VOID_CLASS_DESCRIPTOR)) { } else if (isPrimitive(methodInfo.getReturnType())) { Boxing.unbox(b, methodInfo.getReturnType()); } else { b.checkcast(BytecodeUtils.getName(methodInfo.getReturnType()));
final CodeAttribute b = classMethod.getCodeAttribute(); b.aload(0); // load this int localVariables = 1; int actualDelegateParameterPosition = 0; for (int i = 0; i < initializerMethodInfo.getMethod().getParameterTypes().length; ++i) { if (i == delegateParameterPosition) { Class<?> type = initializerMethodInfo.getMethod().getParameterTypes()[i]; BytecodeUtils.addLoadInstruction(b, DescriptorUtils.makeDescriptor(type), localVariables); if (type == long.class || type == double.class) { localVariables = localVariables + 2; b.invokespecial(classMethod.getClassFile().getSuperclass(), initializerMethodInfo.getName(), initializerMethodInfo.getDescriptor()); b.aload(0); // load this b.aload(actualDelegateParameterPosition); // load the delegate b.invokevirtual(classMethod.getClassFile().getName(), INIT_MH_METHOD_NAME, "(" + LJAVA_LANG_OBJECT + ")" + BytecodeUtils.VOID_CLASS_DESCRIPTOR); b.returnInstruction();
protected void generateSetMethodHandlerBody(ClassMethod method) { final CodeAttribute b = method.getCodeAttribute(); b.aload(0); b.aload(1); b.checkcast(getMethodHandlerType()); b.putfield(method.getClassFile().getName(), METHOD_HANDLER_FIELD_NAME, DescriptorUtils.makeDescriptor(getMethodHandlerType())); b.returnInstruction(); }
private void createAbstractMethodCode(ClassMethod classMethod, MethodInformation method, ClassMethod staticConstructor) { if ((delegateField != null) && (!Modifier.isPrivate(delegateField.getModifiers()))) { // Call the corresponding method directly on the delegate final CodeAttribute b = classMethod.getCodeAttribute(); // load the delegate field b.aload(0); b.getfield(classMethod.getClassFile().getName(), delegateField.getName(), DescriptorUtils.makeDescriptor(delegateField.getType())); // load the parameters b.loadMethodParameters(); // invoke the delegate method b.invokeinterface(delegateField.getType().getName(), method.getName(), method.getDescriptor()); // return the value if applicable b.returnInstruction(); } else { if (!Modifier.isPrivate(method.getMethod().getModifiers())) { // if it is a parameter injection point we need to initialize the // injection point then handle the method with the method handler // this is slightly different to a normal method handler call, as we pass // in a TargetInstanceBytecodeMethodResolver. This resolver uses the // method handler to call getTargetClass to get the correct class type to // resolve the method with, and then resolves this method invokeMethodHandler(classMethod, method, true, targetInstanceBytecodeMethodResolver, staticConstructor); } else { // if the delegate is private we need to use the method handler createInterceptorBody(classMethod, method, staticConstructor); } } }
private void addInitMethod(final String declaringClass, final String methodName, final String[] parameterTypes, String weldMethodName, ClassFile classFile) { ClassMethod initMethod = classFile.addMethod(AccessFlag.of(AccessFlag.PRIVATE, AccessFlag.STATIC), weldMethodName, LJAVA_LANG_REFLECT_METHOD); final CodeAttribute code = initMethod.getCodeAttribute(); BytecodeUtils.pushClassType(code, declaringClass); // now we have the class on the stack code.ldc(methodName); // now we need to load the parameter types into an array code.iconst(parameterTypes.length); code.anewarray(Class.class.getName()); for (int i = 0; i < parameterTypes.length; ++i) { code.dup(); // duplicate the array reference code.iconst(i); // now load the class object String type = parameterTypes[i]; BytecodeUtils.pushClassType(code, type); // and store it in the array code.aastore(); } code.invokestatic(DefaultBytecodeMethodResolver.class.getName(), "getMethod", "(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;"); code.returnInstruction(); }
c.aload(PARSE_STATE_VAR); c.getfield(parseStateClass, "leftOver", "B"); c.dup(); final BranchEnd end = c.ifne(); c.pop(); c.aload(BYTE_BUFFER_VAR); c.invokevirtual(ByteBuffer.class.getName(), "get", "()B"); BranchEnd cont = c.gotoInstruction(); c.branchEnd(end); c.aload(PARSE_STATE_VAR); c.iconst(0); c.putfield(parseStateClass, "leftOver", "B"); c.branchEnd(cont); c.aload(BYTE_BUFFER_VAR); c.invokevirtual(ByteBuffer.class.getName(), "get", "()B"); c.dup(); final Set<BranchEnd> tokenEnds = new LinkedHashSet<>(); final Set<BranchEnd> badTokenEnds = new LinkedHashSet<>(); final Map<State, BranchEnd> ends = new IdentityHashMap<State, BranchEnd>(); for (State state : currentState.next.values()) { c.iconst(state.value); ends.put(state, c.ifIcmpeq()); c.dup(); c.iconst(':');
/** * Adds two constructors to the class that call each other in order to bypass * the JVM class file verifier. * <p/> * This would result in a stack overflow if they were actually called, * however the proxy is directly created without calling the constructor */ private void addConstructorsForBeanWithPrivateConstructors(ClassFile proxyClassType) { ClassMethod ctor = proxyClassType.addMethod(AccessFlag.PUBLIC, INIT_METHOD_NAME, BytecodeUtils.VOID_CLASS_DESCRIPTOR, LJAVA_LANG_BYTE); CodeAttribute b = ctor.getCodeAttribute(); b.aload(0); b.aconstNull(); b.aconstNull(); b.invokespecial(proxyClassType.getName(), INIT_METHOD_NAME, "(" + LJAVA_LANG_BYTE + LJAVA_LANG_BYTE + ")" + BytecodeUtils.VOID_CLASS_DESCRIPTOR); b.returnInstruction(); ctor = proxyClassType.addMethod(AccessFlag.PUBLIC, INIT_METHOD_NAME, BytecodeUtils.VOID_CLASS_DESCRIPTOR, LJAVA_LANG_BYTE, LJAVA_LANG_BYTE); b = ctor.getCodeAttribute(); b.aload(0); b.aconstNull(); b.invokespecial(proxyClassType.getName(), INIT_METHOD_NAME, "(" + LJAVA_LANG_BYTE + ")" + BytecodeUtils.VOID_CLASS_DESCRIPTOR); b.returnInstruction(); }
private void createStateField(final State state, final ClassFile file, final CodeAttribute sc) { if (state.fieldName != null) { file.addField(AccessFlag.STATIC | AccessFlag.FINAL | AccessFlag.PRIVATE, state.fieldName, "[B"); sc.ldc(state.terminalState); sc.ldc("ISO-8859-1"); sc.invokevirtual(String.class.getName(), "getBytes", "(Ljava/lang/String;)[B"); sc.putstatic(file.getName(), state.fieldName, "[B"); sc.aload(CONSTRUCTOR_HTTP_STRING_MAP_VAR); if (state.terminalState != null) { sc.ldc(state.terminalState); } else { sc.ldc(state.soFar); sc.invokeinterface(Map.class.getName(), "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); sc.dup(); BranchEnd end = sc.ifnull(); sc.checkcast(HTTP_STRING_CLASS); sc.putstatic(file.getName(), state.httpStringFieldName, HTTP_STRING_DESCRIPTOR); BranchEnd done = sc.gotoInstruction(); sc.branchEnd(end); sc.pop(); sc.newInstruction(HTTP_STRING_CLASS); sc.dup(); if (state.terminalState != null) { sc.ldc(state.terminalState); } else { sc.ldc(state.soFar); sc.invokespecial(HTTP_STRING_CLASS, "<init>", "(Ljava/lang/String;)V");
@Override protected void addSerializationSupport(ClassFile proxyClassType) { final Class<Exception>[] exceptions = new Class[]{ObjectStreamException.class}; final ClassMethod writeReplace = proxyClassType.addMethod(AccessFlag.PRIVATE, "writeReplace", LJAVA_LANG_OBJECT); writeReplace.addCheckedExceptions(exceptions); CodeAttribute b = writeReplace.getCodeAttribute(); b.newInstruction(SerializableClientProxy.class.getName()); b.dup(); b.aload(0); b.getfield(proxyClassType.getName(), BEAN_ID_FIELD, BeanIdentifier.class); b.aload(0); b.getfield(proxyClassType.getName(), CONTEXT_ID_FIELD, String.class); b.invokespecial(SerializableClientProxy.class.getName(), INIT_METHOD_NAME, "(" + LBEAN_IDENTIFIER + LJAVA_LANG_STRING + ")" + BytecodeUtils.VOID_CLASS_DESCRIPTOR); b.returnInstruction(); }
@Override public void getDeclaredMethod(final ClassMethod classMethod, final String declaringClass, final String methodName, final String[] parameterTypes, ClassMethod staticConstructor) { String weldMemberName = WELD_MEMBER_PREFIX + METHOD_COUNT.incrementAndGet(); staticConstructor.getClassFile().addField(AccessFlag.PRIVATE | AccessFlag.STATIC, weldMemberName, LJAVA_LANG_REFLECT_METHOD); final CodeAttribute code = staticConstructor.getCodeAttribute(); addInitMethod(declaringClass, methodName, parameterTypes, weldMemberName, staticConstructor.getClassFile()); code.invokestatic(staticConstructor.getClassFile().getName(), weldMemberName, "()Ljava/lang/reflect/Method;"); code.putstatic(classMethod.getClassFile().getName(), weldMemberName, LJAVA_LANG_REFLECT_METHOD); CodeAttribute methodCode = classMethod.getCodeAttribute(); methodCode.getstatic(classMethod.getClassFile().getName(), weldMemberName, LJAVA_LANG_REFLECT_METHOD); }
final ClassMethod ctor = asm.addMethod(Modifier.PUBLIC, "<init>", "V"); ctor.getCodeAttribute().aload(0); ctor.getCodeAttribute().invokespecial(superclass.getName(), "<init>", "()V"); ctor.getCodeAttribute().returnInstruction(); asm.addField(Modifier.PRIVATE + Modifier.STATIC, ID_FIELD_NAME, String[].class); final CodeAttribute idMethod = asm.addMethod(Modifier.PUBLIC + Modifier.FINAL, "_ids", "[Ljava/lang/String;").getCodeAttribute(); idMethod.getstatic(stubClassName, ID_FIELD_NAME, "[Ljava/lang/String;"); idMethod.returnInstruction(); final CodeAttribute clinit = asm.addMethod(Modifier.STATIC, "<clinit>", "V").getCodeAttribute(); clinit.iconst(ids.length); clinit.anewarray(String.class.getName()); for (int i = 0; i < ids.length; i++) { clinit.dup(); clinit.iconst(i); clinit.ldc(ids[i]); clinit.aastore(); clinit.putstatic(stubClassName, ID_FIELD_NAME, "[Ljava/lang/String;"); clinit.invokestatic(stubClassName, init(methodIndex), "()V"); clinit.returnInstruction();
private void createDelegateToSuper(ClassMethod classMethod, MethodInformation method, String className) { CodeAttribute b = classMethod.getCodeAttribute(); // first generate the invokespecial call to the super class method b.aload(0); b.loadMethodParameters(); b.invokespecial(className, method.getName(), method.getDescriptor()); b.returnInstruction(); }
public byte[] createTokenizer(final String[] httpVerbs, String[] httpVersions, String[] standardHeaders) { final String className = existingClassName + CLASS_NAME_SUFFIX; final ClassFile file = new ClassFile(className, existingClassName); final ClassMethod ctor = file.addMethod(AccessFlag.PUBLIC, "<init>", "V", DescriptorUtils.parameterDescriptors(constructorDescriptor)); ctor.getCodeAttribute().aload(0); ctor.getCodeAttribute().loadMethodParameters(); ctor.getCodeAttribute().invokespecial(existingClassName, "<init>", constructorDescriptor); ctor.getCodeAttribute().returnInstruction(); final ClassMethod sctor = file.addMethod(AccessFlag.PUBLIC | AccessFlag.STATIC, "<clinit>", "V"); final AtomicInteger fieldCounter = new AtomicInteger(1); sctor.getCodeAttribute().invokestatic(existingClassName, "httpStrings", "()" + DescriptorUtils.makeDescriptor(Map.class)); sctor.getCodeAttribute().astore(CONSTRUCTOR_HTTP_STRING_MAP_VAR); createStateMachines(httpVerbs, httpVersions, standardHeaders, className, file, sctor, fieldCounter); sctor.getCodeAttribute().returnInstruction(); return file.toBytecode(); }
@Override void doWork(CodeAttribute b, ClassMethod classMethod) { if (Modifier.isPrivate(classMethod.getAccessFlags())) { // Weld cannot use invokespecial to invoke a private method from the superclass invokePrivateMethodHandler(b, classMethod, methodInfo, staticConstructor); } else { // build the bytecode that invokes the super class method directly b.aload(0); // create the method invocation b.loadMethodParameters(); b.invokespecial(methodInfo.getDeclaringClass(), methodInfo.getName(), methodInfo.getDescriptor()); } // leave the result on top of the stack }
protected void generateGetMethodHandlerBody(ClassMethod method) { final CodeAttribute b = method.getCodeAttribute(); b.aload(0); getMethodHandlerField(method.getClassFile(), b); b.returnInstruction(); }