Class: Rubernate::Bytecode::ProxyClass

Inherits:
Object
  • Object
show all
Defined in:
lib/rubernate/bytecode.rb

Overview

Representation of the generated class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ct_class) ⇒ ProxyClass

Returns a new instance of ProxyClass.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rubernate/bytecode.rb', line 49

def initialize(ct_class)
  self.ct_class = ct_class
  self.frozen = false
            
  java_import 'javassist.bytecode.ClassFile'
  java_import 'javassist.bytecode.AnnotationsAttribute'
  java_import 'javassist.bytecode.annotation.Annotation'
  java_import 'javassist.bytecode.annotation.StringMemberValue'
  
  self.class_file = self.ct_class.getClassFile
  self.const_pool = self.class_file.getConstPool
  
  ann_attr = AnnotationsAttribute.new(self.const_pool, AnnotationsAttribute.visibleTag)
  ann = Annotation.new("javax.persistence.Entity",self.const_pool)
  ann.add_member_value "name", StringMemberValue.new(self.ct_class.name,self.const_pool)
  

  ann_attr.set_annotation ann          
  self.class_file.addAttribute(ann_attr)                    
end

Instance Attribute Details

#class_fileObject

Returns the value of attribute class_file.



47
48
49
# File 'lib/rubernate/bytecode.rb', line 47

def class_file
  @class_file
end

#clazzObject

Returns the value of attribute clazz.



47
48
49
# File 'lib/rubernate/bytecode.rb', line 47

def clazz
  @clazz
end

#const_poolObject

Returns the value of attribute const_pool.



47
48
49
# File 'lib/rubernate/bytecode.rb', line 47

def const_pool
  @const_pool
end

#ct_classObject

Returns the value of attribute ct_class.



47
48
49
# File 'lib/rubernate/bytecode.rb', line 47

def ct_class
  @ct_class
end

#frozenObject

Returns the value of attribute frozen.



47
48
49
# File 'lib/rubernate/bytecode.rb', line 47

def frozen
  @frozen
end

Instance Method Details

#add_annotation(field, name) ⇒ Object

adds full qulified java annotation to a field @param javassist field @param field name



73
74
75
76
77
78
79
# File 'lib/rubernate/bytecode.rb', line 73

def add_annotation(field,name)
  ann_attr = AnnotationsAttribute.new(self.const_pool, AnnotationsAttribute.visibleTag)
  ann = Annotation.new(name,self.const_pool)
  
  ann_attr.set_annotation ann
  field.getFieldInfo.addAttribute ann_attr
end

#add_parameter(type, name) ⇒ Object

This wild add a parammeter to given class and generates te setter

@param java type @Param name of the field @return Generated field



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rubernate/bytecode.rb', line 93

def add_parameter(type,name)
   java_import 'javassist.CtField'
   java_import 'javassist.CtMethod'           
   java_import 'javassist.Modifier'
   java_import 'javax.persistence.Basic'
         
   field = CtField.make("private #{type} #{name};",self.ct_class)
   self.ct_class.add_field(field)      
   
   get = CtMethod.make(" public #{type} get#{name.capitalize}(){ return #{name}; }",self.ct_class)
   set = CtMethod.make(" public void set#{name.capitalize}(#{type} #{name}){ this.#{name} = #{name}; }",self.ct_class)          
   self.ct_class.add_method(get)
   self.ct_class.add_method(set)
   
   return field
end

#add_simple(type, name) ⇒ Object

add simple parameter to a field (see #add_parameter)



83
84
85
# File 'lib/rubernate/bytecode.rb', line 83

def add_simple(type,name)
  add_parameter(Rubernate::Bytecode.mapped_types[type],name)
end

#to_classObject

@return java byte code generated class



111
112
113
# File 'lib/rubernate/bytecode.rb', line 111

def to_class          
  self.ct_class.to_class
end