Class: CodeModels::Java::Parser

Inherits:
CodeModels::JavaParserWrapper::ParserJavaWrapper
  • Object
show all
Defined in:
lib/codemodels/java/parser.rb

Defined Under Namespace

Classes: MyBasicTransformationFactory

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



35
36
37
38
39
# File 'lib/codemodels/java/parser.rb', line 35

def initialize()
  super()
  @transformer.factory = MyBasicTransformationFactory.new
  @transformer.factory.target_module = CodeModels::Java
end

Instance Method Details

#containment_pos(node) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/codemodels/java/parser.rb', line 41

def containment_pos(node)
  container = node.eContainer
  children  = node.eContainer.send(node.eContainingFeature)
  if children.respond_to?(:each)
    children.each_with_index do |c,i|
      return i if c==node
    end
    raise "Not found"
  else
    raise "Not found" unless children==node
    0
  end
end

#corresponding_node(model_element, node_tree) ⇒ Object

node tree contains the original



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/codemodels/java/parser.rb', line 56

def corresponding_node(model_element,node_tree)
  return node_tree unless model_element.eContainer
  corresponding_parent_node = corresponding_node(model_element.eContainer,node_tree)
  containment_pos = containment_pos(model_element)
  containing_feat = model_element.eContainingFeature

  children = corresponding_parent_node.send(containing_feat)
  if children.respond_to?(:each)
    children[containment_pos]
  else
    children
  end
end

#corresponding_node_from_code(model_element, code) ⇒ Object



77
78
79
80
81
82
# File 'lib/codemodels/java/parser.rb', line 77

def corresponding_node_from_code(model_element,code)
  sis = ByteArrayInputStream.new(code.to_java_bytes)
  node_tree = JavaParser.parse(sis)
  sis.close
  corresponding_node(model_element,node_tree)
end

#get_corresponding_metaclass(node) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/codemodels/java/parser.rb', line 88

def get_corresponding_metaclass(node)
  node_class = node.class
  name = CodeModels::JavaParserWrapper::Utils.simple_java_class_name(node_class)
  name = "#{(node.operator.name).proper_capitalize}BinaryExpr" if name=='BinaryExpr'
  if node.class.to_s=='Java::JapaParserAstBody::MethodDeclaration'
    if node.parent.class.to_s=='Java::JapaParserAstExpr::ObjectCreationExpr'
      name = 'ClassMethodDeclaration'
    elsif node.parent.class.to_s=='Java::JapaParserAstBody::EnumDeclaration'
      name = 'ClassMethodDeclaration' 
    elsif node.parent.interface?        
      name = 'InterfaceMethodDeclaration'
    else
      name = 'ClassMethodDeclaration'
    end
  end
  return Java.const_get(name)
end

#internal_parse_artifact(artifact) ⇒ Object



84
85
86
# File 'lib/codemodels/java/parser.rb', line 84

def internal_parse_artifact(artifact)
  node_to_model(node_tree_from_code(artifact.code))
end

#node_tree_from_code(code) ⇒ Object



70
71
72
73
74
75
# File 'lib/codemodels/java/parser.rb', line 70

def node_tree_from_code(code)
  sis = ByteArrayInputStream.new(code.to_java_bytes)
  node_tree = JavaParser.parse(sis)
  sis.close
  node_tree
end