public static boolean isAssignable(CtField thisCtField, String targetClassName) { try { return isAssignable( thisCtField.getType(), targetClassName ); } catch (NotFoundException e) { // keep going } return false; }
InheritanceMetadata inheritanceMetadata = new InheritanceMetadata( inherited, visible, readerName, writerName ); if ( CtClass.booleanType.equals( persistentField.getType() ) ) { return new PrimitiveAttributeTypeDescriptor( inheritanceMetadata, Boolean.TYPE ); else if ( CtClass.byteType.equals( persistentField.getType() )) { return new PrimitiveAttributeTypeDescriptor( inheritanceMetadata, Byte.TYPE ); else if ( CtClass.charType.equals( persistentField.getType() ) ) { return new PrimitiveAttributeTypeDescriptor( inheritanceMetadata, Character.TYPE ); else if ( CtClass.shortType.equals( persistentField.getType() ) ) { return new PrimitiveAttributeTypeDescriptor( inheritanceMetadata, Short.TYPE ); else if ( CtClass.intType.equals( persistentField.getType() ) ) { return new PrimitiveAttributeTypeDescriptor( inheritanceMetadata, Integer.TYPE ); else if ( CtClass.longType.equals( persistentField.getType() ) ) { return new PrimitiveAttributeTypeDescriptor( inheritanceMetadata, Long.TYPE ); else if ( CtClass.doubleType.equals( persistentField.getType() ) ) { return new PrimitiveAttributeTypeDescriptor( inheritanceMetadata, Double.TYPE ); else if ( CtClass.floatType.equals( persistentField.getType() ) ) { return new PrimitiveAttributeTypeDescriptor( inheritanceMetadata, Float.TYPE ); return new ObjectAttributeTypeDescriptor( inheritanceMetadata, persistentField.getType() );
private static String inferFieldTypeName(CtField field) { try { if ( field.getFieldInfo2().getAttribute( SignatureAttribute.tag ) == null ) { return field.getType().getName(); } return inferGenericTypeName( field.getType(), SignatureAttribute.toTypeSignature( field.getGenericSignature() ) ); } catch (BadBytecode ignore) { return null; } catch (NotFoundException e) { return null; } }
private static RuntimeException barf( CtClass ct, CtField fld ) throws NotFoundException { return new RuntimeException(ct.getSimpleName()+"."+fld.getName()+" of type "+(fld.getType().getSimpleName())+": Serialization not implemented; does not extend Iced or DTask"); }
@Override boolean filter(CtField ctf) throws NotFoundException { API api = null; try { api = (API) ctf.getAnnotation(API.class); } catch( ClassNotFoundException ex) { throw new NotFoundException("getAnnotations throws ", ex); } return api != null && (api.json() || !isInput(ctf.getType(), api)); } });
private void handleCompositeField(CtClass managedCtClass, CtField persistentField, CtMethod fieldWriter) throws NotFoundException, CannotCompileException { if ( !enhancementContext.isCompositeClass( persistentField.getType() ) || !PersistentAttributesHelper.hasAnnotation( persistentField, Embedded.class ) ) { return; } // make sure to add the CompositeOwner interface addCompositeOwnerInterface( managedCtClass ); // cleanup previous owner fieldWriter.insertBefore( String.format( "if (%1$s != null) { ((%2$s) %1$s).%3$s(\"%1$s\"); }%n", persistentField.getName(), CompositeTracker.class.getName(), EnhancerConstants.TRACKER_COMPOSITE_CLEAR_OWNER ) ); // trigger track changes fieldWriter.insertAfter( String.format( "if (%1$s != null) { ((%2$s) %1$s).%4$s(\"%1$s\", (%3$s) this); }%n" + "%5$s(\"%1$s\");", persistentField.getName(), CompositeTracker.class.getName(), CompositeOwner.class.getName(), EnhancerConstants.TRACKER_COMPOSITE_SET_OWNER, EnhancerConstants.TRACKER_CHANGER_NAME ) ); }
private void processFields(CtClass clazz) throws CannotCompileException, NotFoundException { CtField[] fs = clazz.getDeclaredFields(); for (int i = 0; i < fs.length; ++i) { CtField f = fs[i]; int mod = f.getModifiers(); if ((mod & Modifier.PUBLIC) != 0 && (mod & Modifier.FINAL) == 0) { mod |= Modifier.STATIC; String name = f.getName(); CtClass ftype = f.getType(); CtMethod wmethod = CtNewMethod.wrapped(ftype, readPrefix + name, readParam, null, trapRead, ConstParameter.string(name), clazz); wmethod.setModifiers(mod); clazz.addMethod(wmethod); CtClass[] writeParam = new CtClass[2]; writeParam[0] = classPool.get("java.lang.Object"); writeParam[1] = ftype; wmethod = CtNewMethod.wrapped(CtClass.voidType, writePrefix + name, writeParam, null, trapWrite, ConstParameter.string(name), clazz); wmethod.setModifiers(mod); clazz.addMethod(wmethod); } } }
@Override public void edit(FieldAccess f) throws CannotCompileException { try { if ( ! f.isStatic() ) { CtClass type = null; type = f.getField().getType(); FSTClazzInfo.FSTFieldInfo fieldInfo = clInfo.getFieldInfo(f.getFieldName(), null); if ( fieldInfo == null ) { return; } if ( f.isReader() ) { structGen.defineStructReadAccess(f, type, fieldInfo); } else if ( f.isWriter() ) { structGen.defineStructWriteAccess(f, type, fieldInfo); } } } catch (Exception e) { e.printStackTrace(); } } });
private boolean addDocIfAPI( StringBuilder sb, CtField ctf, CtClass cc, boolean first ) throws NotFoundException, CannotCompileException { String name = ctf.getName(); Object[] as; try { as = ctf.getAnnotations(); } catch( ClassNotFoundException ex) { throw new NotFoundException("getAnnotations throws ", ex); } API api = null; for(Object o : as) if(o instanceof API) api = (API) o; if( api != null ) { String help = api.help(); int min = api.since(); int max = api.until(); if( min < 1 || min > 1000000 ) throw new CannotCompileException("Found field '"+name+"' but 'since' < 1 or 'since' > 1000000"); if( max < min || (max > 1000000 && max != Integer.MAX_VALUE) ) throw new CannotCompileException("Found field '"+name+"' but 'until' < "+min+" or 'until' > 1000000"); if( first ) first = false; else sb.append(","); boolean input = isInput(ctf.getType(), api); sb.append("new water.api.DocGen$FieldDoc(\""+name+"\",\""+help+"\","+min+","+max+","+ctf.getType().getName()+".class,"+input+","+api.required()+",water.api.ParamImportance."+api.importance()+",water.api.Direction."+api.direction()+",\""+api.path()+"\","+ api.type().getName()+".class,\""+api.valid()+"\", \""+api.enabled()+"\",\""+api.visible()+"\")"); } return first; }
private void processFields(CtClass clazz) throws CannotCompileException, NotFoundException { CtField[] fs = clazz.getDeclaredFields(); for (int i = 0; i < fs.length; ++i) { CtField f = fs[i]; int mod = f.getModifiers(); if ((mod & Modifier.PUBLIC) != 0 && (mod & Modifier.FINAL) == 0) { mod |= Modifier.STATIC; String name = f.getName(); CtClass ftype = f.getType(); CtMethod wmethod = CtNewMethod.wrapped(ftype, readPrefix + name, readParam, null, trapRead, ConstParameter.string(name), clazz); wmethod.setModifiers(mod); clazz.addMethod(wmethod); CtClass[] writeParam = new CtClass[2]; writeParam[0] = classPool.get("java.lang.Object"); writeParam[1] = ftype; wmethod = CtNewMethod.wrapped(CtClass.voidType, writePrefix + name, writeParam, null, trapWrite, ConstParameter.string(name), clazz); wmethod.setModifiers(mod); clazz.addMethod(wmethod); } } }
private int makeFieldInitializer(Bytecode code, CtClass[] parameters) throws CannotCompileException, NotFoundException { int stacksize = 0; Javac jv = new Javac(code, this); try { jv.recordParams(parameters, false); } catch (CompileError e) { throw new CannotCompileException(e); } for (FieldInitLink fi = fieldInitializers; fi != null; fi = fi.next) { CtField f = fi.field; if (!Modifier.isStatic(f.getModifiers())) { int s = fi.init.compile(f.getType(), f.getName(), code, parameters, jv); if (stacksize < s) stacksize = s; } } return stacksize; }
private void modifyClassConstructor(ClassFile cf) throws CannotCompileException, NotFoundException { if (fieldInitializers == null) return; Bytecode code = new Bytecode(cf.getConstPool(), 0, 0); Javac jv = new Javac(code, this); int stacksize = 0; boolean doInit = false; for (FieldInitLink fi = fieldInitializers; fi != null; fi = fi.next) { CtField f = fi.field; if (Modifier.isStatic(f.getModifiers())) { doInit = true; int s = fi.init.compileIfStatic(f.getType(), f.getName(), code, jv); if (stacksize < s) stacksize = s; } } if (doInit) // need an initializer for static fileds. modifyClassConstructor(cf, code, stacksize, 0); }
@Override public void edit(FieldAccess f) throws CannotCompileException { try { if ( ! f.isStatic() ) { CtClass type = null; type = f.getField().getType(); FSTClazzInfo.FSTFieldInfo fieldInfo = clInfo.getFieldInfo(f.getFieldName(), null); if ( fieldInfo == null ) { return; } if ( f.isReader() ) { structGen.defineStructReadAccess(f, type, fieldInfo); } else if ( f.isWriter() ) { structGen.defineStructWriteAccess(f, type, fieldInfo); } } } catch (Exception e) { e.printStackTrace(); } } });
private int makeFieldInitializer(Bytecode code, CtClass[] parameters) throws CannotCompileException, NotFoundException { int stacksize = 0; Javac jv = new Javac(code, this); try { jv.recordParams(parameters, false); } catch (CompileError e) { throw new CannotCompileException(e); } for (FieldInitLink fi = fieldInitializers; fi != null; fi = fi.next) { CtField f = fi.field; if (!Modifier.isStatic(f.getModifiers())) { int s = fi.init.compile(f.getType(), f.getName(), code, parameters, jv); if (stacksize < s) stacksize = s; } } return stacksize; }
private void modifyClassConstructor(ClassFile cf) throws CannotCompileException, NotFoundException { if (fieldInitializers == null) return; Bytecode code = new Bytecode(cf.getConstPool(), 0, 0); Javac jv = new Javac(code, this); int stacksize = 0; boolean doInit = false; for (FieldInitLink fi = fieldInitializers; fi != null; fi = fi.next) { CtField f = fi.field; if (Modifier.isStatic(f.getModifiers())) { doInit = true; int s = fi.init.compileIfStatic(f.getType(), f.getName(), code, jv); if (stacksize < s) stacksize = s; } } if (doInit) // need an initializer for static fileds. modifyClassConstructor(cf, code, stacksize, 0); }
private synchronized CtClass javassistLoadClass( CtClass cc ) throws NotFoundException, CannotCompileException, BadBytecode { if( cc.isFrozen() ) return cc; // serialize parent javassistLoadClass(cc.getSuperclass()); // Serialize enums first, since we need the raw_enum function for this class for( CtField ctf : cc.getDeclaredFields() ) { CtClass base = ctf.getType(); while( base.isArray() ) base = base.getComponentType(); if( base.subclassOf(_enum) && base != cc ) javassistLoadClass(base); } CtClass ccr = addSerializationMethods(cc); ccr.freeze(); return ccr; }
code.addGetstatic(Bytecode.THIS, fieldName, fieldType); code.addReturn(field.getType());
try { ConstPool cp = getClassFile2().getConstPool(); int index = init.getConstantValue(cp, f.getType()); if (index != 0) { f.getFieldInfo2().addAttribute(new ConstantAttribute(cp, index));