Refine search
/** * Adds a new <code>CONSTANT_Class_info</code> structure. * * <p>This also adds a <code>CONSTANT_Utf8_info</code> structure * for storing the class name. * * @return the index of the added entry. */ public int addClassInfo(CtClass c) { if (c == THIS) return thisClassInfo; else if (!c.isArray()) return addClassInfo(c.getName()); else { // an array type is recorded in the hashtable with // the key "[L<classname>;" instead of "<classname>". // // note: toJvmName(toJvmName(c)) is equal to toJvmName(c). return addClassInfo(Descriptor.toJvmName(c)); } }
protected int match(int c, int pos, CodeIterator iterator, int typedesc, ConstPool cp) throws BadBytecode { if (newIndex == 0) { int nt = cp.addNameAndTypeInfo(cp.addUtf8Info(newMethodname), typedesc); int ci = cp.addClassInfo(newClassname); if (c == INVOKEINTERFACE) newIndex = cp.addInterfaceMethodrefInfo(ci, nt); else { if (newMethodIsPrivate && c == INVOKEVIRTUAL) iterator.writeByte(INVOKESPECIAL, pos); newIndex = cp.addMethodrefInfo(ci, nt); } constPool = cp; } iterator.write16bit(newIndex, pos + 1); return pos; } }
/** * Returns the field accessed by this expression. */ public CtField getField() throws NotFoundException { CtClass cc = getCtClass(); int index = iterator.u16bitAt(currentPos + 1); ConstPool cp = getConstPool(); return cc.getField(cp.getFieldrefName(index), cp.getFieldrefType(index)); }
private static String fieldInfo(ConstPool pool, int index) { return "#" + index + " = Field " + pool.getFieldrefClassName(index) + "." + pool.getFieldrefName(index) + "(" + pool.getFieldrefType(index) + ")"; }
/** * Reads the <code>class_index</code> field of the * <code>CONSTANT_InterfaceMethodref_info</code> structure * at the given index. * * @return the name of the class at that <code>class_index</code>. */ public String getInterfaceMethodrefClassName(int index) { MemberrefInfo minfo = (MemberrefInfo)getItem(index); return getClassInfo(minfo.classIndex); }
private static String interfaceMethodInfo(ConstPool pool, int index) { return "#" + index + " = Method " + pool.getInterfaceMethodrefClassName(index) + "." + pool.getInterfaceMethodrefName(index) + "(" + pool.getInterfaceMethodrefType(index) + ")"; }
final ConstPool constPool = aCtClass.getClassFile().getConstPool(); final ClassPool classPool = aCtClass.getClassPool(); if ( methodName.startsWith( "$$_hibernate_" ) || methodInfo.getCodeAttribute() == null ) { continue; String fieldName = constPool.getFieldrefName( itr.u16bitAt( index + 1 ) ); String fieldClassName = constPool.getClassInfo( constPool.getFieldrefClass( itr.u16bitAt( index + 1 ) ) ); CtClass targetCtClass = classPool.getCtClass( fieldClassName ); int fieldReaderMethodIndex = constPool.addMethodrefInfo( constPool.addClassInfo( fieldClassName ), EnhancerConstants.PERSISTENT_FIELD_READER_PREFIX + fieldName, "()" + constPool.getFieldrefType( itr.u16bitAt( index + 1 ) ) ); itr.writeByte( Opcode.INVOKEVIRTUAL, index ); int fieldWriterMethodIndex = constPool.addMethodrefInfo( constPool.addClassInfo( fieldClassName ), EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX + fieldName, "(" + constPool.getFieldrefType( itr.u16bitAt( index + 1 ) ) + ")V" ); itr.writeByte( Opcode.INVOKEVIRTUAL, index ); methodInfo.getCodeAttribute().setAttribute( MapMaker.make( classPool, methodInfo ) );
protected void enhanceAttributesAccess( CtClass managedCtClass, IdentityHashMap<String, PersistentAttributeAccessMethods> attributeDescriptorMap) { final ConstPool constPool = managedCtClass.getClassFile().getConstPool(); final ClassPool classPool = managedCtClass.getClassPool(); for ( Object oMethod : managedCtClass.getClassFile().getMethods() ) { final MethodInfo methodInfo = (MethodInfo) oMethod; final String methodName = methodInfo.getName(); if ( methodName.startsWith( "$$_hibernate_" ) || methodInfo.getCodeAttribute() == null ) { continue; final CodeIterator itr = methodInfo.getCodeAttribute().iterator(); while ( itr.hasNext() ) { final int index = itr.next(); final int op = itr.byteAt( index ); if ( op != Opcode.PUTFIELD && op != Opcode.GETFIELD ) { continue; if ( !managedCtClass.getName().equals( constPool.getFieldrefClassName( itr.u16bitAt( index + 1 ) ) ) ) { continue; final String fieldName = constPool.getFieldrefName( itr.u16bitAt( index + 1 ) ); final PersistentAttributeAccessMethods attributeMethods = attributeDescriptorMap.get( fieldName ); methodInfo.getCodeAttribute().setAttribute( MapMaker.make( classPool, methodInfo ) );
/** * Constructs an annotation that can be accessed through the interface * represented by <code>clazz</code>. The values of the members are * not specified. * * @param cp the constant pool table. * @param clazz the interface. * @throws NotFoundException when the clazz is not found */ public Annotation(ConstPool cp, CtClass clazz) throws NotFoundException { // todo Enums are not supported right now. this(cp.addUtf8Info(Descriptor.of(clazz.getName())), cp); if (!clazz.isInterface()) throw new RuntimeException( "Only interfaces are allowed for Annotation creation."); CtMethod methods[] = clazz.getDeclaredMethods(); if (methods.length > 0) { members = new LinkedHashMap(); } for (int i = 0; i < methods.length; i++) { CtClass returnType = methods[i].getReturnType(); addMemberValue(methods[i].getName(), createMemberValue(cp, returnType)); } }
cc.checkModify(); ConstPool cp = methodInfo.getConstPool(); CodeAttribute ca = methodInfo.getCodeAttribute(); CodeIterator iterator = ca.iterator(); Bytecode b = new Bytecode(cp, ca.getMaxStack(), ca.getMaxLocals()); b.setStackDepth(1); Javac jv = new Javac(b, cc); ca.setMaxLocals(locals); int len = iterator.getCodeLength(); int pos = iterator.append(b.get()); ca.getExceptionTable().add(getStartPosOfBody(ca), len, len, cp.addClassInfo(exceptionType)); iterator.append(b.getExceptionTable(), pos); methodInfo.rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile2());
private ExceptionInfo[] buildExceptionInfo(MethodInfo method) { ConstPool constPool = method.getConstPool(); ClassPool classes = clazz.getClassPool(); ExceptionTable table = method.getCodeAttribute().getExceptionTable(); ExceptionInfo[] exceptions = new ExceptionInfo[table.size()]; for (int i = 0; i < table.size(); i++) { int index = table.catchType(i); Type type; try { type = index == 0 ? Type.THROWABLE : Type.get(classes.get(constPool.getClassInfo(index))); } catch (NotFoundException e) { throw new IllegalStateException(e.getMessage()); } exceptions[i] = new ExceptionInfo(table.startPc(i), table.endPc(i), table.handlerPc(i), type); } return exceptions; }
thisClass.getClassFile(); // to call checkModify(). ConstPool constPool = getConstPool(); int pos = currentPos; int index = iterator.u16bitAt(pos + 1); int c = iterator.byteAt(pos); if (c == INVOKEINTERFACE) { opcodeSize = 5; classname = constPool.getInterfaceMethodrefClassName(index); methodname = constPool.getInterfaceMethodrefName(index); signature = constPool.getInterfaceMethodrefType(index); classname = constPool.getMethodrefClassName(index); methodname = constPool.getMethodrefName(index); signature = constPool.getMethodrefType(index); ClassPool cp = thisClass.getClassPool(); CodeAttribute ca = iterator.get(); try { CtClass[] params = Descriptor.getParameterTypes(signature, cp); CtClass retType = Descriptor.getReturnType(signature, cp); int paramVar = ca.getMaxLocals(); jc.recordParams(classname, params, true, paramVar, withinStatic());
/** * Returns true if the constructor (or static initializer) * is the default one. This method returns true if the constructor * takes some arguments but it does not perform anything except * calling <code>super()</code> (the no-argument constructor of * the super class). */ public boolean isEmpty() { CodeAttribute ca = getMethodInfo2().getCodeAttribute(); if (ca == null) return false; // native or abstract?? // they are not allowed, though. ConstPool cp = ca.getConstPool(); CodeIterator it = ca.iterator(); try { int pos, desc; int op0 = it.byteAt(it.next()); return op0 == Opcode.RETURN // empty static initializer || (op0 == Opcode.ALOAD_0 && it.byteAt(pos = it.next()) == Opcode.INVOKESPECIAL && (desc = cp.isConstructor(getSuperclassName(), it.u16bitAt(pos + 1))) != 0 && "()V".equals(cp.getUtf8Info(desc)) && it.byteAt(it.next()) == Opcode.RETURN && !it.hasNext()); } catch (BadBytecode e) {} return false; }
throws CannotCompileException declaringClass.checkModify(); ConstPool cp = methodInfo.getConstPool(); CodeAttribute ca = methodInfo.getCodeAttribute(); if (ca == null) throw new CannotCompileException("no method body"); LocalVariableAttribute va = (LocalVariableAttribute)ca.getAttribute( LocalVariableAttribute.tag); if (va == null) { va = new LocalVariableAttribute(cp); ca.getAttributes().add(va); int maxLocals = ca.getMaxLocals(); String desc = Descriptor.of(type); va.addEntry(0, ca.getCodeLength(), cp.addUtf8Info(name), cp.addUtf8Info(desc), maxLocals); ca.setMaxLocals(maxLocals + Descriptor.dataSize(desc));
private void addParameter2(int where, CtClass type, String desc) throws BadBytecode CodeAttribute ca = methodInfo.getCodeAttribute(); if (ca != null) { int size = 1; char typeDesc = 'L'; int classInfo = 0; if (type.isPrimitive()) { CtPrimitiveType cpt = (CtPrimitiveType)type; size = cpt.getDataSize(); classInfo = methodInfo.getConstPool().addClassInfo(type); ca.insertLocalVar(where, size); LocalVariableAttribute va = (LocalVariableAttribute)ca.getAttribute(LocalVariableAttribute.tag); if (va != null) va.shiftIndex(where, size); = (LocalVariableTypeAttribute)ca.getAttribute(LocalVariableTypeAttribute.tag); if (lvta != null) lvta.shiftIndex(where, size);
public int transform(CtClass tclazz, int pos, CodeIterator iterator, ConstPool cp) throws BadBytecode int c = iterator.byteAt(pos); if (c == PUTFIELD || c == PUTSTATIC) { int index = iterator.u16bitAt(pos + 1); String typedesc = isField(tclazz.getClassPool(), cp, fieldClass, fieldname, isPrivate, index); if (typedesc != null) { if (c == PUTSTATIC) { CodeAttribute ca = iterator.get(); iterator.move(pos); char c0 = typedesc.charAt(0); iterator.writeByte(DUP_X2, pos + 1); iterator.writeByte(POP, pos + 2); ca.setMaxStack(ca.getMaxStack() + 2); iterator.writeByte(ACONST_NULL, pos); iterator.writeByte(SWAP, pos + 1); ca.setMaxStack(ca.getMaxStack() + 1); int mi = cp.addClassInfo(methodClassname); String type = "(Ljava/lang/Object;" + typedesc + ")V"; int methodref = cp.addMethodrefInfo(mi, methodName, type); iterator.writeByte(INVOKESTATIC, pos); iterator.write16bit(methodref, pos + 1);
private void evalNewObjectArray(int pos, CodeIterator iter, Frame frame) throws BadBytecode { // Convert to x[] format Type type = resolveClassInfo(constPool.getClassInfo(iter.u16bitAt(pos + 1))); String name = type.getCtClass().getName(); int opcode = iter.byteAt(pos); int dimensions; if (opcode == MULTIANEWARRAY) { dimensions = iter.byteAt(pos + 3); } else { name = name + "[]"; dimensions = 1; } while (dimensions-- > 0) { verifyAssignable(Type.INTEGER, simplePop(frame)); } simplePush(getType(name), frame); }
CannotCompileException thisClass.getClassFile(); // to call checkModify(). ConstPool constPool = getConstPool(); int pos = currentPos; index = iterator.u16bitAt(pos + 1); desc = constPool.getClassInfo(index); if (desc.startsWith("[")) desc = "[" + desc; index = iterator.u16bitAt(currentPos + 1); desc = constPool.getClassInfo(index); dim = iterator.byteAt(currentPos + 3); codeLength = 4; throw new RuntimeException("bad opcode: " + opcode); retType = Descriptor.toCtClass(desc, thisClass.getClassPool()); params[i] = CtClass.intType; int paramVar = ca.getMaxLocals(); jc.recordParams(javaLangObject, params, true, paramVar, withinStatic());
private int addFieldrefInfo(CtField f, FieldInfo finfo) { ConstPool cp = bytecode.getConstPool(); String cname = f.getDeclaringClass().getName(); int ci = cp.addClassInfo(cname); String name = finfo.getName(); String type = finfo.getDescriptor(); return cp.addFieldrefInfo(ci, name, type); }
if (!isConstructor()) return; CodeAttribute ca = getCodeAttribute(); byte[] code = ca.getCode(); CodeIterator iterator = ca.iterator(); int pos = iterator.skipSuperConstructor(); if (pos >= 0) { // not this() ConstPool cp = constPool; int mref = ByteArray.readU16bit(code, pos + 1); int nt = cp.getMethodrefNameAndType(mref); int sc = cp.addClassInfo(superclass); int mref2 = cp.addMethodrefInfo(sc, nt); ByteArray.write16bit(mref2, code, pos + 1);