PJVM/Class.pm

filter: Statementstmt 100% 76 / 76 Branchbran 72.2% 13 / 18 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 5 totaltotal 89.5% 103 / 115 CC 12 SCAR 15.8
CC
12
Cov
94%
new_from_io
9.1
method
4.0
BEGIN
1.0
CRAP 4.8
« PJVM/Bytecode/Reader.pm ↑ Summary PJVM/Class/Method.pm »
1 package PJVM::Class;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use PJVM::Class::ConstantPool;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use PJVM::Class::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use PJVM::Class::Method;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Object::Tiny qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 constant_pool
12 access_flags
13 this_class
14 super_class
15 interfaces_classes
16 fields
17 methods
18 attributes
19 3 );
20  
21 3 ▶sub new_from_io {
Subroutine new_from_io: called CC=9 SCAR=22.0
Pod undocumented
22 3 my ($pkg, $io) = @_;
23
24 3 my $buff;
25
26 3 my $self = $pkg->new();
27
28 # Check that's it is actually a class file
29 3 read $io, $buff, 4;
30 3 ▶ die "Not a classfile" unless sprintf("%x", unpack("N", $buff)) eq "cafebabe";
Branch: 1 branch
BranchTrueFalseTotal
unless sprintf("%x", unpack("N", $buff)) eq "cafebabe" 0 3 3
31
32 # TODO: Blah blah blah.. version compliance crap, let's skip this for now and
33 # maybe sometime in the future do something with it.
34 3 read $io, $buff, 4;
35
36 # Constant pool count.. here lies our constants, ie class names etc.
37 3 my $cp = PJVM::Class::ConstantPool->new_from_io($io);
38 3 $self->{constant_pool} = $cp;
39  
40 # Access flags (you know, public etc...)
41 3 read $io, $buff, 2;
42 3 my $access_flags = unpack("n", $buff);
43 3 $self->{access_flags} = $access_flags,
44
45 # Name of class and parent
46 read $io, $buff, 4;
47 3 my ($this_class, $super_class) = unpack("nn", $buff);
48 $self->{this_class} = $this_class,
49 3 $self->{super_class} = $super_class,
50
51 # Read interfaces
52 read $io, $buff, 2;
53 3 my $interface_count = unpack("n", $buff);
54 3 my @interfaces;
55 3 ▶ if ($interface_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interface_count) 3 0 3
56 3 read $io, $buff, 2 * $interface_count;
57 3 @interfaces = unpack("n*", $buff);
58 }
59 3 $self->{interfaces_classes} = \@interfaces,
60
61 # Read fields
62 read $io, $buff, 2;
63 3 my $fields_count = unpack("n", $buff);
64 3 my @fields;
65 3 ▶ if ($fields_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields_count) 3 0 3
66 3 ▶ while ($fields_count--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields_count--) 9 3 12
67 9 push @fields, PJVM::Class::Field->new_from_io($io, $cp, $self);
68 }
69 }
70 3 $self->{fields} = \@fields,
71
72 # Read methods
73 read $io, $buff, 2;
74 3 my $methods_count = unpack("n", $buff);
75 3 my @methods;
76 3 ▶ if ($methods_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($methods_count) 3 0 3
77 3 ▶ while ($methods_count--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($methods_count--) 9 3 12
78 9 push @methods, PJVM::Class::Method->new_from_io($io, $cp, $self);
79 }
80 }
81 3 $self->{methods} = \@methods,
82
83 # Attributes
84 read $io, $buff, 2;
85 3 my $attributes_count = unpack("n", $buff);
86
87 # Read attributes
88 3 my @attributes;
89 3 ▶ if ($attributes_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes_count) 3 0 3
90 3 ▶ while ($attributes_count--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes_count--) 3 3 6
91 3 push @attributes, PJVM::Class::Attribute->new_from_io($io, $cp);
92 }
93 }
94 3 $self->{attributes} = \@attributes,
95
96 return $self;
97 }
98  
99 # TODO: Optimize since this will probablly be called mucho tiempo
100 1 ▶sub method {
Subroutine method: called CC=4 SCAR=13.9
Pod undocumented
101 1 my ($self, $name, $signature) = @_;
102  
103 1 for my $method (@{$self->methods}) {
104 3 ▶ if ($method->name eq $name && $method->signature eq $signature) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method->name eq $name and $method->signature eq $signature) 1 2 3
Condition66%2/3
$method->name eq $name and $method->signature eq $signature
!ll&&!rl&&r
201
MC/DC50%1/2
$method->name eq $name and $method->signature eq $signature
$method->name eq $name $method->signature eq $signature
Truth table66%2/3
A and B
A= $method->name eq $nameB= $method->signature eq $signature
ABresult
0X0
100
111
105 1 return $method;
106 }
107 }
108 }
109  
110 1 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
111 1 my $self = shift;
112 1 my $class = $self->constant_pool->get($self->this_class);
113 1 my $name = $self->constant_pool->get($class->name_index);
114
115 1 return $name->value;
116 }
117  
118 1 ▶sub super {
Subroutine super: called CC=1 SCAR=0.0
Pod undocumented
119 1 my $self = shift;
120 1 my $class = $self->constant_pool->get($self->super_class);
121 1 my $name = $self->constant_pool->get($class->name_index);
122
123 1 return $name->value;
124 }
125  
126 1 ▶sub interfaces {
Subroutine interfaces: called CC=1 SCAR=0.0
Pod undocumented
127 1 my $self = shift;
128 my @names = map {
129 1 my $class = $self->constant_pool->get($_);
130 1 my $name = $self->constant_pool->get($class->name_index);
131 1 $name->value;
132 1 } @{$self->interfaces_classes};
133
134 1 return @names;
135 }
136  
137 3 1;
138 __END__
« PJVM/Bytecode/Reader.pm ↑ Summary PJVM/Class/Method.pm »