-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUselessBoxing.html
More file actions
180 lines (154 loc) · 9.71 KB
/
UselessBoxing.html
File metadata and controls
180 lines (154 loc) · 9.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<!DOCTYPE HTML>
<html>
<head>
<title>Detecting Useless Boxings</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="/js/jquery.min.js"></script>
<script src="/js/skel.min.js"></script>
<script src="/js/skel-layers.min.js"></script>
<script src="/js/init.js"></script>
<script src="/js/highlight.pack.js"></script>
<link rel="stylesheet" href="/css/highlight/github.css" />
<noscript>
<link rel="stylesheet" href="/css/skel.css" />
<link rel="stylesheet" href="/css/style.css" />
<link rel="stylesheet" href="/css/style-xlarge.css" />
</noscript>
</head>
<body id="top">
<header id="header" class="skel-layers-fixed">
<h2><a href="/"><img src="/OPALLogo.png" width="25" height="25"> OPAL Project</a></h2>
<nav id="nav">
<ul>
<li><a href="/Publications.html">Publications</a></li>
<li><a href="/library/api/SNAPSHOT/org/opalj/index.html">ScalaDoc</a></li>
<li><a href="https://github.com/opalj/OPAL">Repository</a></li>
<li><a href="https://search.maven.org/#search%7Cga%7C1%7Cg%3A%22de.opal-project%22">Maven Central</a></li>
</ul>
</nav>
</header>
<section id="one" class="wrapper style1">
<div class="container">
<div class="row">
<div class="3u">
<nav>
<a href="/UsingOPAL.html">Using OPAL</a><br>
<br><b>Getting Started</b><br>
<a href="/tutorial/FixedPointAnalyses.html">Writing Fixed-Point Analyses</a><br>
<a href="/tutorial/CollaborativeAnalyses.html">Collaborative Analyses</a><br>
<a href="/tutorial/Lattices.html">Lattices</a><br>
<a href="/tutorial/Results.html">Results</a><br>
<a href="/tutorial/Schedulers.html">Schedulers</a><br>
<br><b>Further Tutorials</b><br>
<a href="/examples/ReadingClassFiles.html">Reading Class Files</a><br>
<a href="/examples/Projects.html">Loading Java Projects</a><br>
<a href="/examples/ClassHierarchy.html">Using the Class Hierarchy</a><br>
<a href="/examples/BytecodeEngineering.html">Engineering Java Bytecode</a><br>
<a href="/examples/TAC.html">3-Address Code/SSA Code</a><br>
<a href="/examples/WritingAnalyses.html">Writing Analyses</a><br>
<br><b>Tools</b><br>
<a href="/DeveloperTools.html">Developers Tools</a><br>
<a href="/Hermes.html">Hermes</a><br>
<a href="/Opium.html">OPIUM</a><br>
<br><b>Showcases</b><br>
<a href="/UselessBoxing.html">Detecting Useless Boxings</a><br>
<a href="/JDK8Dependencies.html">Package Dependencies in JDK8</a><br>
<a href="/JDK8Complexity.html">Complexity of the JDK8</a><br>
<br><b>Artifacts</b><br>
<a href="/Artifacts.html">Research Artifacts</a><br>
</nav>
<hr/>
<div class="hide-at-medium">
Please, report issues or problems in the documentation using <a href="https://github.com/opalj/OPAL/issues">OPAL's issue tracker</a>, by going to the <a href="https://gitter.im/OPAL-Project/Lobby?utm_source=share-link&utm_medium=link&utm_campaign=share-link">OPAL gitter chat room</a> or by writing an email to <a href="mailto:opal_at_st.informatik.tu-darmstadt.de">opal(at)st.informatik.tu-darmstadt.de</a>.
</div>
</div>
<div class="9u">
<h1>Useless Boxing</h1>
<p>
OPAL's standard bytecode representation provides an API that offers extensive support for pattern matching on Java bytecode. This greatly facilitates writing analyses that identify typical code smells.
</p>
For example, let's write an analysis to find instances of the following issue:
<pre><code class="java">double value = 1.0d;
//...
int i = (new Double(value)).intValue // << the issue
//Intended: int i = (int) value</code></pre>
I.e., let's find code where we wrap a primitive value and immediately unwrap it. (Such code generally hinders comprehensibility and wastes CPU cycles;
a real instance can be found in the <a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8-b132/com/sun/org/apache/xalan/internal/lib/ExsltMath.java#ExsltMath">OpenJDK 8</a>.)<br />
<br />
The complete(!) analysis to find the described bug pattern is shown next.
<pre><code class="scala">import org.opalj.br._
import org.opalj.br.instructions._
import org.opalj.br.reader.Java8Framework.ClassFiles
val project = ClassFiles(new java.io.File("/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib"))
val theMethods = Set(
"booleanValue","byteValue","charValue","shortValue",
"intValue","longValue","floatValue","doubleValue")
for {
(classFile,_) <- project.par // for all classes (let's do it in parallel)
method @ MethodWithBody(body) <- classFile.methods // for all non-abstract, non-native methods
pc <- body.matchPair { // find a sequence of two instructions where...
case (
INVOKESPECIAL(receiver1, _, _, TheArgument(parameterType: BaseType)),
INVOKEVIRTUAL(receiver2, name, NoArgumentMethodDescriptor(returnType: BaseType))
) => { (receiver1 eq receiver2) &&
receiver1.isPrimitiveTypeWrapper &&
theMethods.contains(name) }
case _ => false
}
} println (classFile.fqn +"{"+ method.toJava+"::"+ body.lineNumber(pc)+"}")</code></pre>
You can paste the analysis in the Scala REPL to directly execute it. In this case, you'll get – for the OpenJDK 8 – the following result:
<pre>
com/sun/org/apache/xalan/internal/lib/ExsltMath{double constant(java.lang.String,double)::Some(379)}
</pre>
On a notebook (Core i7, 2,3 GHz, 8GB, SSD) loading the JDK takes 2 to 3 seconds, the analysis itself takes roughly 0.04 seconds for the entire JDK.
<br />
<br />
</div>
</div>
</div>
</section>
<footer id="footer">
<div class="container">
<div class="row double">
<div class="6u">
<h3>Stay connected and get help!</h3>
<ul class="icons">
<li><a href="https://twitter.com/intent/follow?original_referer=http%3A%2F%2Fwww.opal-project.de%2F&ref_src=twsrc%5Etfw®ion=follow_link&screen_name=TheOpalProject&tw_p=followbutton" class="icon fa-twitter"><span class="label">Twitter</span></a></li>
<li><a href="https://gitter.im/OPAL-Project/Lobby?utm_source=share-link&utm_medium=link&utm_campaign=share-link" class="icon fa-users"><span class="label">Gitter</span></a></li>
<li><a href="https://trello.com/theopalproject" class="icon fa-trello"><span class="label">Trello</span></a></li>
<li><a href="https://stackoverflow.com/questions/tagged/opal-framework" class="icon fa-stack-overflow"><span class="label">Stackoverflow</span></a></li>
<li><a href="https://hub.docker.com/r/opalj/sbt_scala_opal" class="icon fa-cloud"><span class="label">Docker</span></a></li>
</ul>
<h3>Related</h3>
<ul class="alt">
<li><a href="https://www.openhub.net/p/OPAL-Project">OPAL on OpenHub</a></li>
</ul>
</div>
<div class="6u">
<h3>Supported By</h3>
<ul class="alt">
<li><a href="https://www.yourkit.com/"><img src="/images/yklogo.png" width="139", height="33"></a><br>
YourKit supports open source projects with its full-featured Java Profiler.
YourKit, LLC is the creator of <a href="https://www.yourkit.com/java/profiler/">YourKit Java Profiler</a>
an innovative and intelligent tool for profiling Java applications.
</li>
</ul>
</div>
</div>
<ul class="copyright">
<li>© The OPAL Project. All rights reserved.</li>
<li><a href="https://raw.githubusercontent.com/opalj/opal/master/LICENSE">License (BSD 2 Clause)</a></li>
<li><img src="/Athene.png" height="27" style="vertical-align:middle"></li>
<li>Design: <a href="https://templated.co">TEMPLATED</a></li>
</ul>
</div>
</footer>
</body>
<script>
hljs.configure({
tabReplace: ' ', // 4 spaces
languages: ["scala","java"]
})
hljs.highlightAll()
</script>
</html>