-
Notifications
You must be signed in to change notification settings - Fork 7
/
WasteInCharArraysQuery.java
175 lines (148 loc) · 6.05 KB
/
WasteInCharArraysQuery.java
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
/*******************************************************************************
* Copyright (c) 2008, 2020 SAP AG and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* SAP AG - initial API and implementation
* Chris Grindstaff
* Andrew Johnson (IBM Corporation) - help
*******************************************************************************/
package org.eclipse.mat.inspections;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.eclipse.mat.SnapshotException;
import org.eclipse.mat.collect.ArrayInt;
import org.eclipse.mat.internal.Messages;
import org.eclipse.mat.query.IQuery;
import org.eclipse.mat.query.IResult;
import org.eclipse.mat.query.annotations.Argument;
import org.eclipse.mat.query.annotations.CommandName;
import org.eclipse.mat.query.annotations.HelpUrl;
import org.eclipse.mat.query.annotations.Icon;
import org.eclipse.mat.snapshot.ISnapshot;
import org.eclipse.mat.snapshot.extension.Subject;
import org.eclipse.mat.snapshot.model.IArray;
import org.eclipse.mat.snapshot.model.IClass;
import org.eclipse.mat.snapshot.model.IObject;
import org.eclipse.mat.snapshot.query.ObjectListResult;
import org.eclipse.mat.util.IProgressListener;
@Subject("char[]")
@CommandName("waste_in_char_arrays")
@Icon("/META-INF/icons/waste.gif")
@HelpUrl("/org.eclipse.mat.ui.help/reference/inspections/component_report.html#ref_inspections_component_report__strings")
public class WasteInCharArraysQuery implements IQuery
{
@Argument
public ISnapshot snapshot;
@Argument
public int minimumWaste = 50;
public IResult execute(IProgressListener listener) throws Exception
{
InspectionAssert.heapFormatIsNot(snapshot, "DTFJ-PHD"); //$NON-NLS-1$
ArrayInt result = new ArrayInt();
Collection<IClass> classes = snapshot.getClassesByName("char[]", false); //$NON-NLS-1$
if (classes != null)
for (IClass clasz : classes)
{
int[] objectIds = clasz.getObjectIds();
listener.beginTask(Messages.WasteInCharArraysQuery_CheckingCharArrays, objectIds.length / 10);
for (int ii = 0; ii < objectIds.length; ii++)
{
if (ii % 10 == 0)
{
if (listener.isCanceled())
throw new IProgressListener.OperationCanceledException();
listener.worked(1);
}
int id = objectIds[ii];
// ignore if object is too small anyways
IArray array = (IArray) snapshot.getObject(id);
int length = array.getLength();
if (length < minimumWaste)
continue;
if (hasWaste(id, length))
result.add(id);
}
}
return new ObjectListResult.Inbound(snapshot, result.toArray());
}
private boolean hasWaste(int charArrayId, int length) throws SnapshotException
{
int[] inbounds = snapshot.getInboundRefererIds(charArrayId);
List<Fragment> fragments = null;
for (int inbound : inbounds)
{
// if inbounds contain other types than strings,
// then waste is not defined
if (!isString(inbound))
return false;
IObject string = snapshot.getObject(inbound);
Integer offset = (Integer) string.resolveValue("offset"); //$NON-NLS-1$
// E.g. JDK7u6
if (offset == null)
offset = 0;
Integer count = (Integer) string.resolveValue("count"); //$NON-NLS-1$
// E.g. JDK7u6
if (count == null)
count = length - offset;
// string length already uses enough of the char[]
if (length - count < minimumWaste)
return false;
// if there is only one string (and enough waste), include it
if (inbounds.length == 1)
return true;
if (fragments == null)
{
fragments = new ArrayList<Fragment>();
fragments.add(new Fragment(offset, offset + count));
}
else
{
boolean isMerged = false;
for (Fragment fragment : fragments)
{
if (offset >= fragment.start && offset <= fragment.end)
{
fragment.end = Math.max(offset + count, fragment.end);
isMerged = true;
break;
}
else if (offset + count >= fragment.start && offset + count <= fragment.end)
{
fragment.start = offset;
fragment.end = Math.max(offset + count, fragment.end);
isMerged = true;
break;
}
}
if (!isMerged)
fragments.add(new Fragment(offset, offset + count));
}
}
if (fragments != null)
{
for (Fragment ff : fragments)
length -= (ff.end - ff.start);
}
return length > minimumWaste;
}
private boolean isString(int inbound) throws SnapshotException
{
IClass clazz = snapshot.getClassOf(inbound);
return "java.lang.String".equals(clazz.getName()); //$NON-NLS-1$
}
private static class Fragment
{
int start;
int end;
public Fragment(int start, int end)
{
this.start = start;
this.end = end;
}
}
}