Skip to content

Commit fd1dbc4

Browse files
authored
Update Gradle and add Spotless / GJF (#20)
* Add Spotless / GJF * Update Gradle
1 parent 8468a38 commit fd1dbc4

17 files changed

+442
-420
lines changed

build.gradle

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
apply plugin: 'java-library'
2-
apply plugin: 'application'
3-
apply plugin: 'eclipse'
1+
plugins {
2+
id 'java-library'
3+
id 'application'
4+
id 'eclipse'
5+
id("com.diffplug.spotless") version "6.25.0"
6+
}
7+
8+
java {
9+
toolchain {
10+
languageVersion = JavaLanguageVersion.of(11)
11+
}
12+
}
413

5-
sourceCompatibility = 11
614
version = '0.1'
715
jar {
816
manifest {
@@ -40,3 +48,10 @@ if (project.hasProperty('mainClass')) {
4048
// use a default
4149
mainClassName = "com.ibm.wala.examples.drivers.PDFTypeHierarchy"
4250
}
51+
52+
spotless {
53+
java {
54+
// apply a specific flavor of google-java-format
55+
googleJavaFormat()
56+
}
57+
}

gradle/wrapper/gradle-wrapper.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME

src/main/java/com/ibm/wala/examples/analysis/SimpleThreadEscapeAnalysis.java

+64-65
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@
1111
*******************************************************************************/
1212
package com.ibm.wala.examples.analysis;
1313

14-
import java.io.File;
15-
import java.io.IOException;
16-
import java.util.Collection;
17-
import java.util.Iterator;
18-
import java.util.Properties;
19-
import java.util.Set;
20-
import java.util.jar.JarFile;
21-
2214
import com.ibm.wala.classLoader.ArrayClass;
2315
import com.ibm.wala.classLoader.IClass;
2416
import com.ibm.wala.classLoader.IField;
@@ -46,36 +38,39 @@
4638
import com.ibm.wala.util.WalaException;
4739
import com.ibm.wala.util.collections.HashSetFactory;
4840
import com.ibm.wala.util.intset.OrdinalSet;
41+
import java.io.File;
42+
import java.io.IOException;
43+
import java.util.Collection;
44+
import java.util.Iterator;
45+
import java.util.Properties;
46+
import java.util.Set;
47+
import java.util.jar.JarFile;
4948

5049
/**
51-
* <P>
52-
* A simple thread-level escape analysis: this code computes the set of classes of which some instance may be accessed
53-
* by some thread other than the one that created it.
54-
* </P>
55-
*
56-
* <P>
57-
* The algorithm is not very bright; it is based on the observation that there are only three ways for an object to pass
58-
* from one thread to another.
50+
* A simple thread-level escape analysis: this code computes the set of classes of which some
51+
* instance may be accessed by some thread other than the one that created it.
52+
*
53+
* <p>The algorithm is not very bright; it is based on the observation that there are only three
54+
* ways for an object to pass from one thread to another.
55+
*
5956
* <UL>
60-
* <LI> The object is stored into a static variable.
61-
* <LI> The object is stored into an instance field of a Thread
62-
* <LI> The object is reachable from a field of another escaping object.
57+
* <LI>The object is stored into a static variable.
58+
* <LI>The object is stored into an instance field of a Thread
59+
* <LI>The object is reachable from a field of another escaping object.
6360
* </UL>
64-
* </P>
65-
*
66-
* <P>
67-
* This observation is implemented in the obvious way:
61+
*
62+
* <p>This observation is implemented in the obvious way:
63+
*
6864
* <OL>
69-
* <LI> All static fields are collected
70-
* <LI> All Thread constructor parameters are collected
71-
* <LI> The points-to sets of these values represent the base set of escapees.
72-
* <LI> All object reachable from fields of these objects are added
73-
* <LI> This process continues until a fixpoint is reached
74-
* <LI> The abstract objects in the points-to sets are converted to types
75-
* <LI> This set of types is returned
65+
* <LI>All static fields are collected
66+
* <LI>All Thread constructor parameters are collected
67+
* <LI>The points-to sets of these values represent the base set of escapees.
68+
* <LI>All object reachable from fields of these objects are added
69+
* <LI>This process continues until a fixpoint is reached
70+
* <LI>The abstract objects in the points-to sets are converted to types
71+
* <LI>This set of types is returned
7672
* </OL>
77-
* </P>
78-
*
73+
*
7974
* @author Julian Dolby
8075
*/
8176
public class SimpleThreadEscapeAnalysis extends AbstractAnalysisEngine {
@@ -85,7 +80,8 @@ public class SimpleThreadEscapeAnalysis extends AbstractAnalysisEngine {
8580
private final String applicationMainClass;
8681

8782
/**
88-
* The two input parameters define the program to analyze: the jars of .class files and the main class to start from.
83+
* The two input parameters define the program to analyze: the jars of .class files and the main
84+
* class to start from.
8985
*/
9086
public SimpleThreadEscapeAnalysis(Set<JarFile> applicationJarFiles, String applicationMainClass) {
9187
this.applicationJarFiles = applicationJarFiles;
@@ -94,12 +90,13 @@ public SimpleThreadEscapeAnalysis(Set<JarFile> applicationJarFiles, String appli
9490

9591
@Override
9692
protected CallGraphBuilder getCallGraphBuilder(
97-
IClassHierarchy cha, AnalysisOptions options, IAnalysisCacheView cache) {
93+
IClassHierarchy cha, AnalysisOptions options, IAnalysisCacheView cache) {
9894
return Util.makeZeroCFABuilder(Language.JAVA, options, cache, cha, scope);
9995
}
10096

10197
/**
102-
* Given a root path, add it to the set if it is a jar, or traverse it recursively if it is a directory.
98+
* Given a root path, add it to the set if it is a jar, or traverse it recursively if it is a
99+
* directory.
103100
*/
104101
private void collectJars(File f, Set<JarFile> result) throws IOException {
105102
if (f.isDirectory()) {
@@ -114,9 +111,7 @@ private void collectJars(File f, Set<JarFile> result) throws IOException {
114111
}
115112
}
116113

117-
/**
118-
* Collect the set of JarFiles that constitute the system libraries of the running JRE.
119-
*/
114+
/** Collect the set of JarFiles that constitute the system libraries of the running JRE. */
120115
private JarFile[] getSystemJars() throws IOException {
121116
String javaHomePath = "garbage";
122117
Set<JarFile> jarFiles = HashSetFactory.make();
@@ -148,12 +143,12 @@ private JarFile[] getSystemJars() throws IOException {
148143
}
149144

150145
/**
151-
* Take the given set of JarFiles that constitute the program, and return a set of Module files as expected by the
152-
* WALA machinery.
146+
* Take the given set of JarFiles that constitute the program, and return a set of Module files as
147+
* expected by the WALA machinery.
153148
*/
154149
private Set<JarFileModule> getModuleFiles() {
155150
Set<JarFileModule> result = HashSetFactory.make();
156-
for (Iterator<JarFile> jars = applicationJarFiles.iterator(); jars.hasNext();) {
151+
for (Iterator<JarFile> jars = applicationJarFiles.iterator(); jars.hasNext(); ) {
157152
result.add(new JarFileModule(jars.next()));
158153
}
159154

@@ -162,11 +157,12 @@ private Set<JarFileModule> getModuleFiles() {
162157

163158
/**
164159
* The heart of the analysis.
160+
*
165161
* @throws CancelException
166162
* @throws IllegalArgumentException
167163
*/
168-
public Set<IClass> gatherThreadEscapingClasses() throws IOException, ClassHierarchyException, IllegalArgumentException,
169-
CancelException {
164+
public Set<IClass> gatherThreadEscapingClasses()
165+
throws IOException, ClassHierarchyException, IllegalArgumentException, CancelException {
170166

171167
//
172168
// set the application to analyze
@@ -226,7 +222,7 @@ public Set<IClass> gatherThreadEscapingClasses() throws IOException, ClassHierar
226222
// 1) static fields
227223
for (IClass cls : cha) {
228224
Collection<IField> staticFields = cls.getDeclaredStaticFields();
229-
for (Iterator<IField> sfs = staticFields.iterator(); sfs.hasNext();) {
225+
for (Iterator<IField> sfs = staticFields.iterator(); sfs.hasNext(); ) {
230226
IField sf = sfs.next();
231227
if (sf.getFieldTypeReference().isReferenceType()) {
232228
escapeAnalysisRoots.add(heapModel.getPointerKeyForStaticField(sf));
@@ -240,21 +236,21 @@ public Set<IClass> gatherThreadEscapingClasses() throws IOException, ClassHierar
240236
// reachable from fields of types in these pointer keys, and all
241237
// Thread objects must be constructed somewhere)
242238
Collection<IClass> threads = cha.computeSubClasses(TypeReference.JavaLangThread);
243-
for (Iterator<IClass> clss = threads.iterator(); clss.hasNext();) {
239+
for (Iterator<IClass> clss = threads.iterator(); clss.hasNext(); ) {
244240
IClass cls = clss.next();
245-
for (Iterator<? extends IMethod> ms = cls.getDeclaredMethods().iterator(); ms.hasNext();) {
241+
for (Iterator<? extends IMethod> ms = cls.getDeclaredMethods().iterator(); ms.hasNext(); ) {
246242
IMethod m = ms.next();
247243
if (m.isInit()) {
248244
Set<CGNode> nodes = cg.getNodes(m.getReference());
249-
for (Iterator<CGNode> ns = nodes.iterator(); ns.hasNext();) {
245+
for (Iterator<CGNode> ns = nodes.iterator(); ns.hasNext(); ) {
250246
CGNode n = ns.next();
251247
escapeAnalysisRoots.add(heapModel.getPointerKeyForLocal(n, 1));
252248
}
253249
}
254250
}
255251
}
256252

257-
//
253+
//
258254
// compute escaping types: all types flowing to escaping roots and
259255
// all types transitively reachable through their fields.
260256
//
@@ -263,10 +259,10 @@ public Set<IClass> gatherThreadEscapingClasses() throws IOException, ClassHierar
263259
//
264260
// pass 1: get abstract objects (instance keys) for escaping locations
265261
//
266-
for (Iterator<PointerKey> rts = escapeAnalysisRoots.iterator(); rts.hasNext();) {
262+
for (Iterator<PointerKey> rts = escapeAnalysisRoots.iterator(); rts.hasNext(); ) {
267263
PointerKey root = rts.next();
268264
OrdinalSet<InstanceKey> objects = pa.getPointsToSet(root);
269-
for (Iterator<InstanceKey> objs = objects.iterator(); objs.hasNext();) {
265+
for (Iterator<InstanceKey> objs = objects.iterator(); objs.hasNext(); ) {
270266
InstanceKey obj = objs.next();
271267
escapingInstanceKeys.add(obj);
272268
}
@@ -278,15 +274,15 @@ public Set<IClass> gatherThreadEscapingClasses() throws IOException, ClassHierar
278274
Set<InstanceKey> newKeys = HashSetFactory.make();
279275
do {
280276
newKeys.clear();
281-
for (Iterator<InstanceKey> keys = escapingInstanceKeys.iterator(); keys.hasNext();) {
277+
for (Iterator<InstanceKey> keys = escapingInstanceKeys.iterator(); keys.hasNext(); ) {
282278
InstanceKey key = keys.next();
283279
IClass type = key.getConcreteType();
284280
if (type.isReferenceType()) {
285281
if (type.isArrayClass()) {
286282
if (((ArrayClass) type).getElementClass() != null) {
287283
PointerKey fk = heapModel.getPointerKeyForArrayContents(key);
288284
OrdinalSet<InstanceKey> fobjects = pa.getPointsToSet(fk);
289-
for (Iterator<InstanceKey> fobjs = fobjects.iterator(); fobjs.hasNext();) {
285+
for (Iterator<InstanceKey> fobjs = fobjects.iterator(); fobjs.hasNext(); ) {
290286
InstanceKey fobj = fobjs.next();
291287
if (!escapingInstanceKeys.contains(fobj)) {
292288
newKeys.add(fobj);
@@ -295,12 +291,12 @@ public Set<IClass> gatherThreadEscapingClasses() throws IOException, ClassHierar
295291
}
296292
} else {
297293
Collection<IField> fields = type.getAllInstanceFields();
298-
for (Iterator<IField> fs = fields.iterator(); fs.hasNext();) {
294+
for (Iterator<IField> fs = fields.iterator(); fs.hasNext(); ) {
299295
IField f = fs.next();
300296
if (f.getFieldTypeReference().isReferenceType()) {
301297
PointerKey fk = heapModel.getPointerKeyForInstanceField(key, f);
302298
OrdinalSet<InstanceKey> fobjects = pa.getPointsToSet(fk);
303-
for (Iterator<InstanceKey> fobjs = fobjects.iterator(); fobjs.hasNext();) {
299+
for (Iterator<InstanceKey> fobjs = fobjects.iterator(); fobjs.hasNext(); ) {
304300
InstanceKey fobj = fobjs.next();
305301
if (!escapingInstanceKeys.contains(fobj)) {
306302
newKeys.add(fobj);
@@ -318,7 +314,7 @@ public Set<IClass> gatherThreadEscapingClasses() throws IOException, ClassHierar
318314
// get set of types from set of instance keys
319315
//
320316
Set<IClass> escapingTypes = HashSetFactory.make();
321-
for (Iterator<InstanceKey> keys = escapingInstanceKeys.iterator(); keys.hasNext();) {
317+
for (Iterator<InstanceKey> keys = escapingInstanceKeys.iterator(); keys.hasNext(); ) {
322318
InstanceKey key = keys.next();
323319
escapingTypes.add(key.getConcreteType());
324320
}
@@ -327,27 +323,31 @@ public Set<IClass> gatherThreadEscapingClasses() throws IOException, ClassHierar
327323
}
328324

329325
/**
330-
* This main program shows one example use of thread escape analysis: producing a set of fields to be monitored for a
331-
* dynamic race detector. The idea is that any field might have a race with two exceptions: final fields do not have
332-
* races since there are no writes to them, and volatile fields have atomic read and write semantics provided by the
333-
* VM. Hence, this piece of code produces a list of all other fields.
326+
* This main program shows one example use of thread escape analysis: producing a set of fields to
327+
* be monitored for a dynamic race detector. The idea is that any field might have a race with two
328+
* exceptions: final fields do not have races since there are no writes to them, and volatile
329+
* fields have atomic read and write semantics provided by the VM. Hence, this piece of code
330+
* produces a list of all other fields.
331+
*
334332
* @throws CancelException
335333
* @throws IllegalArgumentException
336334
*/
337-
public static void main(String[] args) throws IOException, ClassHierarchyException, IllegalArgumentException, CancelException {
335+
public static void main(String[] args)
336+
throws IOException, ClassHierarchyException, IllegalArgumentException, CancelException {
338337
String mainClassName = args[0];
339338

340339
Set<JarFile> jars = HashSetFactory.make();
341340
for (int i = 1; i < args.length; i++) {
342341
jars.add(new JarFile(args[i]));
343342
}
344343

345-
Set<IClass> escapingTypes = (new SimpleThreadEscapeAnalysis(jars, mainClassName)).gatherThreadEscapingClasses();
344+
Set<IClass> escapingTypes =
345+
(new SimpleThreadEscapeAnalysis(jars, mainClassName)).gatherThreadEscapingClasses();
346346

347-
for (Iterator<IClass> types = escapingTypes.iterator(); types.hasNext();) {
347+
for (Iterator<IClass> types = escapingTypes.iterator(); types.hasNext(); ) {
348348
IClass cls = types.next();
349349
if (!cls.isArrayClass()) {
350-
for (Iterator<IField> fs = cls.getAllFields().iterator(); fs.hasNext();) {
350+
for (Iterator<IField> fs = cls.getAllFields().iterator(); fs.hasNext(); ) {
351351
IField f = fs.next();
352352
if (!f.isVolatile() && !f.isFinal()) {
353353
System.err.println(f.getReference());
@@ -356,5 +356,4 @@ public static void main(String[] args) throws IOException, ClassHierarchyExcepti
356356
}
357357
}
358358
}
359-
360-
}
359+
}

0 commit comments

Comments
 (0)