String sig = sa.getSignature(); out.println("signature: " + sig); try { String s; if (kind == 'c') s = SignatureAttribute.toClassSignature(sig).toString(); else if (kind == 'm') s = SignatureAttribute.toMethodSignature(sig).toString(); else s = SignatureAttribute.toFieldSignature(sig).toString();
/** * Makes a copy. Class names are replaced according to the * given <code>Map</code> object. * * @param newCp the constant pool table used by the new copy. * @param classnames pairs of replaced and substituted * class names. */ public AttributeInfo copy(ConstPool newCp, Map classnames) { return new SignatureAttribute(newCp, getSignature()); }
/** * Returns the generic signature indicated by <code>signature_index</code>. * * @see #toClassSignature(String) * @see #toMethodSignature(String) * @see #toFieldSignature(String) */ public String getSignature() { return getConstPool().getUtf8Info(ByteArray.readU16bit(get(), 0)); }
void renameClass(Map classnames) { String sig = renameClass(getSignature(), classnames); setSignature(sig); }
private static ClassType parseClassType(String sig, Cursor c) throws BadBytecode { if (sig.charAt(c.position) == 'L') return parseClassType2(sig, c, null); else throw error(sig); }
public void setGenericSignature(String sig) { ClassFile cf = getClassFile(); SignatureAttribute sa = new SignatureAttribute(cf.getConstPool(), sig); cf.addAttribute(sa); }
private static String inferMethodTypeName(CtMethod method) { try { if ( method.getMethodInfo2().getAttribute( SignatureAttribute.tag ) == null ) { return method.getReturnType().getName(); } return inferGenericTypeName( method.getReturnType(), SignatureAttribute.toMethodSignature( method.getGenericSignature() ).getReturnType() ); } catch (BadBytecode ignore) { return null; } catch (NotFoundException e) { return null; } }
private static ObjectType parseObjectType(String sig, Cursor c, boolean dontThrow) throws BadBytecode { int i; int begin = c.position; switch (sig.charAt(begin)) { case 'L' : return parseClassType2(sig, c, null); case 'T' : i = c.indexOf(sig, ';'); return new TypeVariable(sig, begin + 1, i); case '[' : return parseArray(sig, c); default : if (dontThrow) return null; else throw error(sig); } }
private String transformMethodSignature(SignatureAttribute signatureAttribute) throws BadBytecode { String originalSig = signatureAttribute.getSignature(); SignatureAttribute.MethodSignature methodSignature = SignatureAttribute.toMethodSignature(originalSig); SignatureAttribute.ClassType returnType = new SignatureAttribute.ClassType("javax.ws.rs.core.Response"); SignatureAttribute.MethodSignature replacementSignature = new SignatureAttribute.MethodSignature( methodSignature.getTypeParameters(), methodSignature.getParameterTypes(), returnType, methodSignature.getExceptionTypes()); return replacementSignature.encode(); } }
/** * Returns the generic signature of the field. * It represents a type including type variables. * * @see SignatureAttribute#toFieldSignature(String) * @since 3.17 */ public String getGenericSignature() { SignatureAttribute sa = (SignatureAttribute)fieldInfo.getAttribute(SignatureAttribute.tag); return sa == null ? null : sa.getSignature(); }
public static SignatureAttribute.ObjectType getFieldSignature(CtField field) { if (field == null) throw new IllegalArgumentException("Null method/constructor"); SignatureAttribute signature = (SignatureAttribute) field.getFieldInfo2().getAttribute(SignatureAttribute.tag); if (signature == null) return null; String sig = signature.getSignature(); try { return SignatureAttribute.toFieldSignature(sig); } catch (BadBytecode e) { throw new IllegalStateException(e); } }
private void ensureVersion(CtClass cc) throws NotFoundException, CannotCompileException, BadBytecode { CtMethod ccms[] = cc.getDeclaredMethods(); if (!javassist.Modifier.isAbstract(cc.getModifiers())) { String gsig = cc.getGenericSignature(); ClassSignature csig = SignatureAttribute.toClassSignature(gsig); // Warning: this is not doing proper parent (superclass/interfaces) traversal TypeArgument ta = getTypeArg(csig.getSuperClass().getTypeArguments(), "Lwater/api/rest/Version"); if (ta!=null && !hasExisting("getVersion", "()"+ta.getType().encode(), ccms) ) { String typeName = ta.toString(); String valueName = getValueFromType(typeName); //cc.addMethod(CtNewMethod.make("public "+typeName+" getVersion() {" + cc.addMethod(CtNewMethod.make("public water.api.rest.Version getVersion() {" + " return "+valueName+";" + "}",cc)); } } } private String getValueFromType(String typeName) {
private static MethodSignature parseMethodSig(String sig) throws BadBytecode { Cursor cur = new Cursor(); TypeParameter[] tp = parseTypeParams(sig, cur); if (sig.charAt(cur.position++) != '(') throw error(sig); ArrayList params = new ArrayList(); while (sig.charAt(cur.position) != ')') { Type t = parseType(sig, cur); params.add(t); } cur.position++; Type ret = parseType(sig, cur); int sigLen = sig.length(); ArrayList exceptions = new ArrayList(); while (cur.position < sigLen && sig.charAt(cur.position) == '^') { cur.position++; ObjectType t = parseObjectType(sig, cur, false); if (t instanceof ArrayType) throw error(sig); exceptions.add(t); } Type[] p = (Type[])params.toArray(new Type[params.size()]); ObjectType[] ex = (ObjectType[])exceptions.toArray(new ObjectType[exceptions.size()]); return new MethodSignature(tp, p, ret, ex); }
private void replaceGenericResponseMethods(CtClass clazz) throws NotFoundException, CannotCompileException, BadBytecode { for (CtMethod method : clazz.getMethods()) { if (method.getReturnType().subtypeOf(getGenericResponseClass())) { MethodInfo methodInfo = method.getMethodInfo(); CtMethod replacementMethod = CtNewMethod .abstractMethod( getResponseClass(), method.getName(), method.getParameterTypes(), method.getExceptionTypes(), clazz); for (Object attributeInfo : methodInfo.getAttributes()) { if (attributeInfo instanceof SignatureAttribute) { String sig = transformMethodSignature((SignatureAttribute) attributeInfo); replacementMethod.getMethodInfo().addAttribute( new SignatureAttribute(((SignatureAttribute) attributeInfo).getConstPool(), sig)); } else { replacementMethod.getMethodInfo().addAttribute((AttributeInfo) attributeInfo); } } clazz.removeMethod(method); clazz.addMethod(replacementMethod); } } }
/** * Makes a localVariableType from a given signature. * @return LocalVariableType * @throws RuntimeException if the signature could not be parsed. */ public static LocalVariableType parse(String signature) { int dimensions = 0; for(int i = 0; i < signature.length(); i++) { if(signature.charAt(i) == '[') dimensions++; else break; } try { javassist.bytecode.SignatureAttribute.Type objectType = SignatureAttribute.toFieldSignature(signature); if(objectType instanceof ArrayType) objectType = ((ArrayType) objectType).getComponentType(); if(objectType instanceof ClassType) { String typeName = ((ClassType) objectType).getName(); return new LocalVariableType(signature, addArrayTypeInfo(typeName, dimensions), typeName, false, dimensions); } throw new RuntimeException("not a class ?"); } catch(Exception e) { // not a class String typeName = primitiveSymbols.get("" + signature.charAt(dimensions)); if(typeName == null) throw new RuntimeException("unknown signature: " + signature, e); return new LocalVariableType(signature, addArrayTypeInfo(typeName, dimensions), typeName, true, dimensions); } }
/** * Sets <code>signature_index</code> to the index of the given generic signature, * which is added to a constant pool. * * @param sig new signature. * @since 3.11 */ public void setSignature(String sig) { int index = getConstPool().addUtf8Info(sig); ByteArray.write16bit(index, info, 0); }
/** * Parses the given signature string as a type signature. * The type signature is either the field type signature or a base type * descriptor including <code>void</code> type. * * @throws BadBytecode thrown when a syntactical error is found. * @since 3.18 */ public static Type toTypeSignature(String sig) throws BadBytecode { try { return parseType(sig, new Cursor()); } catch (IndexOutOfBoundsException e) { throw error(sig); } }
/** * Parses the given signature string as a field type signature. * * @param sig the signature string obtained from the <code>SignatureAttribute</code> * of a <code>FieldInfo</code>. * @return the field type signature. * @throws BadBytecode thrown when a syntactical error is found. * @see #getSignature() * @since 3.5 */ public static ObjectType toFieldSignature(String sig) throws BadBytecode { try { return parseObjectType(sig, new Cursor(), false); } catch (IndexOutOfBoundsException e) { throw error(sig); } }
/** * Parses the given signature string as a class signature. * * @param sig the signature obtained from the <code>SignatureAttribute</code> * of a <code>ClassFile</code>. * @return a tree-like data structure representing a class signature. It provides * convenient accessor methods. * @throws BadBytecode thrown when a syntactical error is found. * @see #getSignature() * @since 3.5 */ public static ClassSignature toClassSignature(String sig) throws BadBytecode { try { return parseSig(sig); } catch (IndexOutOfBoundsException e) { throw error(sig); } }
/** * Parses the given signature string as a method type signature. * * @param sig the signature obtained from the <code>SignatureAttribute</code> * of a <code>MethodInfo</code>. * @return @return a tree-like data structure representing a method signature. It provides * convenient accessor methods. * @throws BadBytecode thrown when a syntactical error is found. * @see #getSignature() * @since 3.5 */ public static MethodSignature toMethodSignature(String sig) throws BadBytecode { try { return parseMethodSig(sig); } catch (IndexOutOfBoundsException e) { throw error(sig); } }