/** * Adds serialVersionUID if one does not already exist. Call this before * modifying a class to maintain serialization compatability. */ public static void setSerialVersionUID(CtClass clazz) throws CannotCompileException, NotFoundException { // check for pre-existing field. try { clazz.getDeclaredField("serialVersionUID"); return; } catch (NotFoundException e) {} // check if the class is serializable. if (!isSerializable(clazz)) return; // add field with default value. CtField field = new CtField(CtClass.longType, "serialVersionUID", clazz); field.setModifiers(Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL); clazz.addField(field, calculateDefault(clazz) + "L"); }
} else { try { CtField ctField = clazz.getDeclaredField(field.getName()); String originalSignature = ctField.getSignature(); String mySignature = field.getSignature();
/** * Adds serialVersionUID if one does not already exist. Call this before * modifying a class to maintain serialization compatability. */ public static void setSerialVersionUID(CtClass clazz) throws CannotCompileException, NotFoundException { // check for pre-existing field. try { clazz.getDeclaredField("serialVersionUID"); return; } catch (NotFoundException e) {} // check if the class is serializable. if (!isSerializable(clazz)) return; // add field with default value. CtField field = new CtField(CtClass.longType, "serialVersionUID", clazz); field.setModifiers(Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL); clazz.addField(field, calculateDefault(clazz) + "L"); }
fname = "_cflow$" + i++; try { cc.getDeclaredField(fname);
fname = "_cflow$" + i++; try { cc.getDeclaredField(fname);
public static boolean hasField(final CtClass ctclass, final String fieldName) { try { // 检查字段是否已经定义 CtField field = ctclass.getDeclaredField(fieldName); if (field != null) { return true; } return false; } catch (NotFoundException e) { return false; } }
/** * Return whether or not the class has a field with the given name * @param theClass the class to inspect * @param theField the name of the field to look for * @return true if the class contains the field, false otherwise */ private static boolean hasField(CtClass theClass, String theField) { try { return theClass.getDeclaredField(theField) != null; } catch (NotFoundException e) { return false; } }
public synchronized boolean isInstrumented() { if(this.isInstrumented==null) { try { this.c.getDeclaredField("VUL_CLS_INS"); this.isInstrumented = new Boolean(true); } catch(NotFoundException e) { this.isInstrumented = new Boolean(false); } } return this.isInstrumented.booleanValue(); }
public MutableFieldInfo getDeclaredField(String fieldName) { synchronized (fields) { MutableFieldInfo field = fields.get(fieldName); if (field != null) return field; } if (fieldArray != null) return null; try { CtField field = ctClass.getDeclaredField(fieldName); if (field == null) return null; return generateFieldInfo(field); } catch (NotFoundException e) { return null; } }
public <T> JaxrsEndpointApiInterfaceCtClassBuilder removeField(final String fieldName) throws NotFoundException { // 检查字段是否已经定义 if(!JavassistUtils.hasField(declaring, fieldName)) { return this; } declaring.removeField(declaring.getDeclaredField(fieldName)); return this; }
public JaxrsEndpointApiCtClassBuilder removeField(final String fieldName) throws NotFoundException { // 检查字段是否已经定义 if(!JavassistUtils.hasField(declaring, fieldName)) { return this; } declaring.removeField(declaring.getDeclaredField(fieldName)); return this; }
public <T> JaxwsEndpointApiCtClassBuilder removeField(final String fieldName) throws NotFoundException { // 检查字段是否已经定义 if(!JavassistUtils.hasField(declaring, fieldName)) { return this; } declaring.removeField(declaring.getDeclaredField(fieldName)); return this; }
public JaxwsEndpointApiInterfaceCtClassBuilder removeField(final String fieldName) throws NotFoundException { // 检查字段是否已经定义 if(!JavassistUtils.hasField(declaring, fieldName)) { return this; } declaring.removeField(declaring.getDeclaredField(fieldName)); return this; }
private CtFieldBuilder(CtClass declaring, CtClass fieldClass, String fieldName, String fieldValue) throws CannotCompileException { // 检查字段是否已经定义 if(!JavassistUtils.hasField(declaring, fieldName)) { /** 添加属性字段 */ field = new CtField(fieldClass, fieldName, declaring); field.setModifiers(Modifier.PROTECTED); //新增Field declaring.addField(field, "\"" + fieldValue + "\""); } else { try { field = declaring.getDeclaredField(fieldName); } catch (NotFoundException e) { } } }
private String addAdvicesUpdatedForJoinpointField(String infoName) throws NotFoundException, CannotCompileException { String updatedAdvicesName = updatedAdvicesName(infoName); try { genInstanceAdvisor.getDeclaredField(updatedAdvicesName); } catch(NotFoundException e) { //Field did not exist - add it CtField updatedAdvice = new CtField(CtClass.booleanType, updatedAdvicesName, genInstanceAdvisor); updatedAdvice.setModifiers(Modifier.PROTECTED); genInstanceAdvisor.addField(updatedAdvice); } return updatedAdvicesName; }
private CtClass transform(CtClass ctClass) throws CannotCompileException, NotFoundException { CtField field = ctClass.getDeclaredField("locator", "Lorg/xml/sax/Locator;"); ctClass.removeField(field); CtMethod getterMethod = ctClass.getDeclaredMethod("getLocator"); getterMethod.setBody(null); CtMethod setterMethod = ctClass.getDeclaredMethod("setLocator"); setterMethod.setBody(null); return ctClass; } }
/** * Adds a ConstructorInfo field to the passed in class */ protected String addConstructorInfoField(int modifiers, CtClass addTo, String infoName, CtField.Initializer init) throws NotFoundException, CannotCompileException { //Instrumentor claspool will be null during hotswap, in which case we //already will have created the field if (instrumentor.getClassPool() != null) { try { addTo.getDeclaredField(infoName); return infoName; } catch(NotFoundException e) { } TransformerCommon.addInfoField(instrumentor, CONSTRUCTOR_INFO_CLASS_NAME, infoName, modifiers, addTo, addInfoAsWeakReference(), init, markInfoAsSynthetic()); } return infoName; }
static private void removeField(CtClass ctClass, String name) throws CannotCompileException, NotFoundException { CtField field = ctClass.getDeclaredField(name); ctClass.removeField(field); CtMethod getterMethod = ctClass.getDeclaredMethod(formatMethodName("get", name)); getterMethod.setBody(null); CtMethod setterMethod = ctClass.getDeclaredMethod(formatMethodName("set", name)); setterMethod.setBody("throw new UnsupportedOperationException();"); }
static private void removeListField(CtClass ctClass, String name) throws CannotCompileException, NotFoundException { CtField field = ctClass.getDeclaredField(name, "Ljava/util/List;"); ctClass.removeField(field); CtMethod testMethod = ctClass.getDeclaredMethod(formatMethodName("has", name)); testMethod.setBody("return false;"); CtMethod getterMethod = ctClass.getDeclaredMethod(formatMethodName("get", name)); getterMethod.setBody("throw new UnsupportedOperationException();"); }
private void compileField(JavaField field, AnnotationDocletTag tag, CtClass clazz) throws Exception { CtField ctField = clazz.getDeclaredField(field.getName()); FieldInfo minfo = ctField.getFieldInfo(); javassist.bytecode.annotation.Annotation info = AnnotationInfoCreator.createAnnotationInfo(pool, minfo.getConstPool(), tag.getAnnotation()); AnnotationsAttribute visible = (AnnotationsAttribute) minfo.getAttribute(AnnotationsAttribute.visibleTag); if (visible == null) { visible = new AnnotationsAttribute(minfo.getConstPool(), AnnotationsAttribute.visibleTag); minfo.addAttribute(visible); } visible.addAnnotation(info); }