public AbstractParserGenerator(final String parseStateClass, final String resultClass, final String constructorDescriptor, String existingClassName) { this.parseStateClass = parseStateClass; this.resultClass = resultClass; this.existingClassName = existingClassName; parseStateDescriptor = DescriptorUtils.makeDescriptor(parseStateClass); httpExchangeDescriptor = DescriptorUtils.makeDescriptor(resultClass); this.constructorDescriptor = constructorDescriptor; }
public static Class<?> toClass(ClassFile ct, ClassLoader loader, ProtectionDomain domain) { if (MAGIC_IN_PROGRESS.get()) { try { final Class<?> originalProxyClass = loader.loadClass(ct.getName()); try { ByteArrayDataOutputStream out = new ByteArrayDataOutputStream(); ct.write(out); Map<Class<?>, byte[]> reloadMap = new HashMap<Class<?>, byte[]>(); reloadMap.put(originalProxyClass, out.getBytes()); // TODO : is this standard way how to reload class? PluginManager.getInstance().hotswap(reloadMap); return originalProxyClass; } catch (Exception e) { throw new RuntimeException(e); } } catch (ClassNotFoundException e) { //it has not actually been loaded yet return ClassFileUtils.toClass(ct, loader, domain); } } return ClassFileUtils.toClass(ct, loader, domain); }
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(); }
} 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 { ca.iload(index); Boxing.boxIfNessesary(ca, DescriptorUtils.makeDescriptor(type)); invoke += (Character.toUpperCase(typeName.charAt(0)) + typeName.substring(1)); ret = DescriptorUtils.makeDescriptor(returnType);
private static void fieldAttribute(Type type, StringBuilder builder) { if (type instanceof Class<?>) { classType((Class<?>) type, builder); } else if (type instanceof ParameterizedType) { ParameterizedType ptype = (ParameterizedType) type; parametizedType(ptype, builder); } else if (type instanceof WildcardType) { WildcardType ptype = (WildcardType) type; wildcardType(ptype, builder); } }
@Override public void writeData(ByteArrayDataOutputStream stream) throws IOException { stream.writeInt(2); // data length stream.writeShort(signatureIndex); } }
public LazySize writeSize() throws IOException { LazySizeImpl sv = new LazySizeImpl(this.written); sizes.add(sv); writeInt(0); return sv; }
@SuppressWarnings("unchecked") private static <T> Class<T> getBound(Type[] bounds) { if (bounds.length == 0) { return (Class<T>) Object.class; } else { return getTypeParamBoundRawType(bounds[0]); } }
@Override public void writeData(ByteArrayDataOutputStream stream) throws IOException { stream.writeShort(typeIndex); stream.writeShort(valueIndex); }
/** * ClassTypeSignature base * * @param clazz * @param builder */ private static String classTypeBase(String className) { return 'L' + encodeClassName(className); }
private void setupLocalVariables(final CodeAttribute c) { c.setupFrame(DescriptorUtils.makeDescriptor(existingClassName + CLASS_NAME_SUFFIX), DescriptorUtils.makeDescriptor(ByteBuffer.class), parseStateDescriptor, httpExchangeDescriptor, "I", "I", HTTP_STRING_DESCRIPTOR, DescriptorUtils.makeDescriptor(StringBuilder.class), "[B"); }
private static void fieldAttribute(Type type, StringBuilder builder) { if (type instanceof Class<?>) { classType((Class<?>) type, builder); } else if (type instanceof ParameterizedType) { ParameterizedType ptype = (ParameterizedType) type; parametizedType(ptype, builder); } else if (type instanceof WildcardType) { WildcardType ptype = (WildcardType) type; wildcardType(ptype, builder); } }
@Override public void writeData(ByteArrayDataOutputStream stream) throws IOException { stream.writeInt(2); // data length stream.writeShort(signatureIndex); } }
@SuppressWarnings("unchecked") private static <T> Class<T> getBound(Type[] bounds) { if (bounds.length == 0) { return (Class<T>) Object.class; } else { return getTypeParamBoundRawType(bounds[0]); } }
protected void createStateMachine(final String[] originalItems, final String className, final ClassFile file, final ClassMethod sctor, final AtomicInteger fieldCounter, final String methodName, final CustomStateMachine stateMachine, boolean expectNewline) { //list of all states except the initial final List<State> allStates = new ArrayList<State>(); final State initial = new State((byte) 0, ""); for (String value : originalItems) { addStates(initial, value, allStates); } //we want initial to be number 0 final AtomicInteger stateCounter = new AtomicInteger(-1); setupStateNo(initial, stateCounter, fieldCounter); for (State state : allStates) { setupStateNo(state, stateCounter, fieldCounter); createStateField(state, file, sctor.getCodeAttribute()); } final int noStates = stateCounter.get(); final ClassMethod handle = file.addMethod(Modifier.PROTECTED | Modifier.FINAL, methodName, "V", DescriptorUtils.makeDescriptor(ByteBuffer.class), parseStateDescriptor, httpExchangeDescriptor); handle.addCheckedExceptions(BAD_REQUEST_EXCEPTION); writeStateMachine(className, file, handle.getCodeAttribute(), initial, allStates, noStates, stateMachine, expectNewline); }
private static void fieldAttribute(Type type, StringBuilder builder) { if (type instanceof Class<?>) { classType((Class<?>) type, builder); } else if (type instanceof ParameterizedType) { ParameterizedType ptype = (ParameterizedType) type; parametizedType(ptype, builder); } else if (type instanceof WildcardType) { WildcardType ptype = (WildcardType) type; wildcardType(ptype, builder); } }
@Override public void writeData(ByteArrayDataOutputStream stream) throws IOException { stream.writeInt(2); // data length stream.writeShort(signatureIndex); } }
c.getfield(parseStateClass, "stringBuilder", DescriptorUtils.makeDescriptor(StringBuilder.class)); c.astore(STATE_STRING_BUILDER_VAR); c.dup();
/** * returns the type descriptor for the class * * @return */ public String getDescriptor() { return DescriptorUtils.makeDescriptor(name); }
public static String[] parameterDescriptors(Class<?>[] parameters) { String[] ret = new String[parameters.length]; for (int i = 0; i < ret.length; ++i) { ret[i] = DescriptorUtils.makeDescriptor(parameters[i]); } return ret; }