Module: Muml_Operation

Includes:
Mjava_Annotation
Defined in:
lib/ontomde-uml2-java/java.rb,
lib/ontomde-uml2-java/java.rb,
lib/ontomde-uml2-java/java.rb,
lib/ontomde-uml2-java/groovy.rb,
lib/ontomde-uml2-java/javadoc.rb,
lib/ontomde-uml2-java/security.rb,
lib/ontomde-uml2-java/operationAsClass.rb

Instance Method Summary collapse

Methods included from Mjava_Annotation

#java_writeAnnotation

Instance Method Details

returns a javadoc link suitable for @see and @link.



56
57
58
59
60
61
62
63
64
65
# File 'lib/ontomde-uml2-java/javadoc.rb', line 56

def java_javadocLink(location=nil)
  o=umlx_owner_one
  if location==o
    #same class
    return "##{java_Name}"
  else
    #different class
    return "#{o.java_qualifiedName}##{java_Name}"
  end
end

#java_writeObject

write java operation skeleton. (Uses java_writeMethodModifiers, java_writeMethodReturnType, java_writeMethodParameters, java_writeMethodThrows, java_writeMethodBody)



847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
# File 'lib/ontomde-uml2-java/java.rb', line 847

def java_write
  write("\n")
  java_writeJavadoc
  java_writeAnnotation
  java_writeMethodModifiers

  if(umlx_isConstructor?)
    write("#{umlx_class_one.java_Name}")
  else
    java_writeMethodReturnType
    write("#{java_Name}")
  end
  java_writeMethodParameters
  java_writeMethodThrows
  java_writeMethodBody
end

#java_writeAbstractObject

write abstract keyword if necessary.



888
889
890
# File 'lib/ontomde-uml2-java/java.rb', line 888

def java_writeAbstract
  write("abstract ") if uml_isAbstract?
end

#java_writeJavadocObject

write operation javadoc comment.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ontomde-uml2-java/javadoc.rb', line 69

def java_writeJavadoc
  return unless context[:javadoc]
  write("    /**\n");
  hasDoc=false

  #	if !java_getter_property.empty? && !java_getter_property_one.uml_isDerived?
  #		"getter for property {@link #{java_getter_property_one.java_javadocLink}}".java_writeJavadoc(self)
  #	end
  #	if !java_setter_property.empty? && !java_setter_property_one.uml_isDerived?
  #		"setter for property {@link #{java_setter_property_one.java_javadocLink}}".java_writeJavadoc(self)
  #	end

  p=java_getter_property_one0
  if !p.nil?
    oe=p.umlx_otherEnd
    if !oe.nil? && oe.umlx_isNavigable?
      g=oe.java_AttributeGlobalGetter_one0
      if !g.nil?
        "This property is part of a bi-navigable association {@link #{g.java_javadocLink(self)}}<br/>".java_writeJavadoc(self)
      end
    end
  end


  if !uml_ownedComment.empty?
    #write("* @return the \n");
    uml_ownedComment.each { |comment|
      hasDoc=true
      comment.uml_body_one.java_writeJavadoc(self)
    }

  elsif !java_getter_property.empty?
    java_getter_property_one.uml_ownedComment.each { |comment|
      hasDoc=true
      comment.uml_body_one.java_writeJavadoc(self)
    }
  else
    #nop
  end
  #if(!hasDoc)
  #	"TODO: document this element in source UML model".java_writeJavadoc(self)
  #end

  uml_ownedParameter.each { |p|
    c=""
    p.uml_ownedComment.each { |comment|
      c="#{c} #{comment.uml_body_one}"
    }
    c=" self-explanatory"
    #next if c.empty? &&  p.uml_direction_one.isReturn?
    "#{p.uml_direction_one.isReturn? ? %{@return} : %{@param}} #{p.java_Name unless p.uml_direction_one.isReturn?}#{c}".java_writeJavadoc(self)
  }

  if !java_setter_property.empty?
    hasDoc=true
    p=java_setter_property_one
    if !p.java_AttributeGlobalGetter.empty?
      "@see #{p.java_AttributeGlobalGetter_one.java_javadocLink}".java_writeJavadoc(self)
    end
  end

  write("    */\n")
end

#java_writeMethodBodyObject

Write the body of a java method.

NOTE:

  • The default generated operation throws a RuntimeException

  • mtk_protected is used to allow inline java code modification (with Eclipse)



920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
# File 'lib/ontomde-uml2-java/java.rb', line 920

def java_writeMethodBody
  if uml_isAbstract? || context[:java_interface]
    write(";\n")
  else
    write("{");
    mtk_protected(Mrdf_Resource::NOREVERSE) {
      if ! java_code_one0.nil?
        write java_code
      else
        java_writeMethodBodyNotImplemented
      end
    }
    write("}\n");
  end
end

#java_writeMethodBodyNotImplementedObject

writes default code for a method not implemented



937
938
939
940
941
942
# File 'lib/ontomde-uml2-java/java.rb', line 937

def java_writeMethodBodyNotImplemented
  write <<END
//TODO: implement method body
throw new java.lang.RuntimeException("[operation \\"#{java_Name}\\"not implemented]");
END
end

#java_writeMethodModifiersObject

write java method modifiers. (Uses java_writeAbstract, java_visibilityModifiers, java_writeStaticModifiers) Visibility is not generated for interfaces (to make checkstyle happy) Example:

  • abstract public

  • public static



870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
# File 'lib/ontomde-uml2-java/java.rb', line 870

def java_writeMethodModifiers


  if !umlx_class_one.kind_of?(Muml_Interface)
    uml_visibility.each { |v|
      write(v.java_visibilityModifiers)
    }
  end
  java_writeStaticModifier
  java_writeAbstract
  write "synchronized "if java_isSynchronised?

  #java_writeModifiers
  #abstract synchronized ...
  #TODO uml_classifier_isAbstract
end

#java_writeMethodParametersObject

write method parameters

Example:

  • ()

  • (int x)

  • (pack1.Class1 xyz, String z)



965
966
967
968
969
970
971
972
973
974
975
# File 'lib/ontomde-uml2-java/java.rb', line 965

def java_writeMethodParameters
  write("(")
  sep=""
  uml_ownedParameter.each { |op|
    next if op.uml_direction_one.isReturn?
    write(sep)
    sep=","
    op.java_write
  }
  write(")")
end

#java_writeMethodReturnTypeObject

write method return type (Uses java_writeParameterType)



894
895
896
# File 'lib/ontomde-uml2-java/java.rb', line 894

def java_writeMethodReturnType
  java_writeParameterType(umlx_returnResult_one0)
end

#java_writeMethodThrowsObject

write method throw clause.

(Uses java_qualifiedName )

Example:

  • throws java.lang.Exception



949
950
951
952
953
954
955
956
957
# File 'lib/ontomde-uml2-java/java.rb', line 949

def java_writeMethodThrows
  sep=" throws "

  uml_raisedException.each { |exception|
    write(sep);
    write(exception.java_qualifiedName)
    sep=","
  }
end

#java_writeParameterType(fp) ⇒ Object

write java type parameter (without the name)

Example:

  • void

  • int

  • pack1.Class1



904
905
906
907
908
909
910
911
912
913
914
# File 'lib/ontomde-uml2-java/java.rb', line 904

def java_writeParameterType(fp)
  if (fp.nil?)
    write("void ")
  else
    fp.java_write
    #uml_isException (!! a ne pas confondre avec une exception)
    #uml_isOrdered
    #uml_isStream
    #uml_isUnique
  end
end

#umlx_implementMethodAsGroovy!Object

implement a method as groovy code.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ontomde-uml2-java/groovy.rb', line 18

def umlx_implementMethodAsGroovy!
  code="groovy.lang.Binding binding = new groovy.lang.Binding();\n"          
  code=code+"// binding java variables to groovy variables\n"
  uml_ownedParameter.each { |p|
    tpl=JavaMapping.instance.getTemplate(p.uml_type_one)
    c=tpl.getGroovyCast(p)
    if(c.nil?)
      code=code+%{//TODO: binding.setVariable("#{p.java_Name}", #{p.java_Name} );\n}
    else
      code=code+%{//TODO: binding.setVariable("#{p.java_Name}",);\n}
    end    
  }
  code=code+"\n//running groovy \n"
        
  code=code+%{groovy.lang.GroovyShell shell = new groovy.lang.GroovyShell(binding);\n}
  code=code+%{Object value = shell.evaluate( "println 'Hello World!'; "+ getGroovyCode);\n}
  
  code=code+"\n//retrieving return value from groovy (if any)\n"
  
  r=umlx_returnResult_one0
  if !r.nil?
    tpl=JavaMapping.instance.getTemplate(r.uml_type_one)
    code=code+%{\nthrow(new java.lang.RuntimeException("process return value");\n}
  else
    code=code+%{\n//noting to return\n}
  end
  
  #assert value.equals(new Integer(20));
  #assert binding.getVariable("x").equals(new Integer(123));    
  
end

#umlx_isSecured?Boolean

return true if element is marked as secured.

Returns:

  • (Boolean)


12
13
14
# File 'lib/ontomde-uml2-java/security.rb', line 12

def umlx_isSecured?
  return umlx_hasStereotype?("Secured")
end