Skip to content

Commit fca2c32

Browse files
committed
Improve support for Java 9+
1 parent 6bf8d35 commit fca2c32

19 files changed

+602
-935
lines changed

pom.xml

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<groupId>org.ehcache</groupId>
1414
<artifactId>sizeof</artifactId>
15-
<version>0.4.1-SNAPSHOT</version>
15+
<version>0.5-SNAPSHOT</version>
1616
<packaging>jar</packaging>
1717

1818
<name>Ehcache SizeOf Engine</name>
@@ -40,6 +40,11 @@
4040
</properties>
4141

4242
<dependencies>
43+
<dependency>
44+
<groupId>net.bytebuddy</groupId>
45+
<artifactId>byte-buddy-agent</artifactId>
46+
<version>1.12.9</version>
47+
</dependency>
4348
<dependency>
4449
<groupId>org.slf4j</groupId>
4550
<artifactId>slf4j-api</artifactId>
@@ -64,9 +69,9 @@
6469
<scope>test</scope>
6570
</dependency>
6671
<dependency>
67-
<groupId>org.ow2.asm</groupId>
68-
<artifactId>asm</artifactId>
69-
<version>6.0</version>
72+
<groupId>net.bytebuddy</groupId>
73+
<artifactId>byte-buddy</artifactId>
74+
<version>1.12.9</version>
7075
<scope>test</scope>
7176
</dependency>
7277
</dependencies>
@@ -92,7 +97,7 @@
9297
</plugin>
9398
<plugin>
9499
<artifactId>maven-failsafe-plugin</artifactId>
95-
<version>2.20.1</version>
100+
<version>2.22.2</version>
96101
</plugin>
97102
<plugin>
98103
<artifactId>maven-gpg-plugin</artifactId>
@@ -129,9 +134,9 @@
129134
</plugin>
130135

131136
<plugin>
132-
<groupId>org.codehaus.mojo</groupId>
133-
<artifactId>findbugs-maven-plugin</artifactId>
134-
<version>3.0.5</version>
137+
<groupId>com.github.spotbugs</groupId>
138+
<artifactId>spotbugs-maven-plugin</artifactId>
139+
<version>4.6.0.0</version>
135140
</plugin>
136141
<plugin>
137142
<groupId>org.jacoco</groupId>
@@ -188,52 +193,13 @@
188193
</goals>
189194
<configuration>
190195
<excludes>
191-
<exclude>src/hidden/**</exclude>
192196
<exclude>README.adoc</exclude>
193197
<exclude>**/*.txt</exclude>
194198
</excludes>
195199
</configuration>
196200
</execution>
197201
</executions>
198202
</plugin>
199-
<plugin>
200-
<groupId>org.codehaus.gmavenplus</groupId>
201-
<artifactId>gmavenplus-plugin</artifactId>
202-
<executions>
203-
<execution>
204-
<id>create-agent-jar</id>
205-
<phase>process-classes</phase>
206-
<goals>
207-
<goal>execute</goal>
208-
</goals>
209-
<configuration>
210-
<scripts>
211-
<script>
212-
def jarFile = new File(project.build.directory, "/classes/org/ehcache/sizeof/impl/sizeof-agent.jar")
213-
def agentClass = "/org/ehcache/sizeof/impl/SizeOfAgent.class"
214-
def agentDir = project.build.directory + "/agent-jar"
215-
def manifestDir = project.basedir.getAbsolutePath() + "/src/hidden/resources"
216-
ant.move(file: new File(project.build.outputDirectory, agentClass),
217-
tofile: new File(agentDir, agentClass))
218-
219-
ant.jar(destfile: jarFile, basedir: new File(agentDir).getAbsolutePath(), manifest: new File(manifestDir, "/META-INF/MANIFEST.MF"))
220-
221-
ant.delete(dir: new File(agentDir))
222-
</script>
223-
</scripts>
224-
</configuration>
225-
</execution>
226-
</executions>
227-
<dependencies>
228-
<dependency>
229-
<groupId>org.codehaus.groovy</groupId>
230-
<artifactId>groovy-all</artifactId>
231-
<!-- any version of Groovy \>= 1.5.0 should work here -->
232-
<version>2.4.12</version>
233-
<scope>runtime</scope>
234-
</dependency>
235-
</dependencies>
236-
</plugin>
237203
<plugin>
238204
<artifactId>maven-surefire-plugin</artifactId>
239205
<configuration>
@@ -275,8 +241,8 @@
275241
</executions>
276242
</plugin>
277243
<plugin>
278-
<groupId>org.codehaus.mojo</groupId>
279-
<artifactId>findbugs-maven-plugin</artifactId>
244+
<groupId>com.github.spotbugs</groupId>
245+
<artifactId>spotbugs-maven-plugin</artifactId>
280246
<configuration>
281247
<excludeFilterFile>${basedir}/findbugs-exclude-filter.xml</excludeFilterFile>
282248
</configuration>
@@ -309,6 +275,31 @@
309275
</plugins>
310276
</build>
311277

278+
<profiles>
279+
<profile>
280+
<id>java9</id>
281+
<activation>
282+
<jdk>[9,)</jdk>
283+
</activation>
284+
<build>
285+
<plugins>
286+
<plugin>
287+
<artifactId>maven-surefire-plugin</artifactId>
288+
<configuration>
289+
<argLine>-Xms64m -Xmx64m --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.util.concurrent.locks=ALL-UNNAMED</argLine>
290+
</configuration>
291+
</plugin>
292+
<plugin>
293+
<artifactId>maven-failsafe-plugin</artifactId>
294+
<configuration>
295+
<argLine>-Xms64m -Xmx64m --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.util.concurrent.locks=ALL-UNNAMED</argLine>
296+
</configuration>
297+
</plugin>
298+
</plugins>
299+
</build>
300+
</profile>
301+
</profiles>
302+
312303
<scm>
313304
<url>https://github.com/ehcache/sizeof</url>
314305
<connection>scm:git:https://github.com/ehcache/sizeof.git</connection>

src/hidden/resources/META-INF/MANIFEST.MF

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright Terracotta, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ehcache.sizeof;
17+
18+
public interface ObjectSizer {
19+
20+
long sizeOf(Object obj);
21+
}

src/main/java/org/ehcache/sizeof/SizeOf.java

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,19 @@
1717

1818
import org.ehcache.sizeof.filters.CombinationSizeOfFilter;
1919
import org.ehcache.sizeof.filters.SizeOfFilter;
20-
import org.ehcache.sizeof.impl.AgentSizeOf;
21-
import org.ehcache.sizeof.impl.ReflectionSizeOf;
22-
import org.ehcache.sizeof.impl.UnsafeSizeOf;
20+
import org.ehcache.sizeof.impl.AgentSizer;
21+
import org.ehcache.sizeof.impl.ReflectionSizer;
22+
import org.ehcache.sizeof.impl.UnsafeSizer;
2323
import org.ehcache.sizeof.util.WeakIdentityConcurrentMap;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
27+
import java.util.List;
28+
import java.util.Objects;
29+
import java.util.function.Supplier;
30+
import java.util.stream.Stream;
31+
32+
import static java.util.stream.Collectors.toList;
2433

2534
/**
2635
* Abstract sizeOf for Java. It will rely on a proper sizeOf to measure sizes of entire object graphs
@@ -79,20 +88,11 @@ public static SizeOf newInstance(final SizeOfFilter... filters) {
7988
}
8089

8190
public static SizeOf newInstance(boolean bypassFlyweight, boolean cache, final SizeOfFilter... filters) {
82-
final SizeOfFilter filter = new CombinationSizeOfFilter(filters);
83-
try {
84-
return new AgentSizeOf(filter, cache, bypassFlyweight);
85-
} catch (UnsupportedOperationException e) {
86-
try {
87-
return new UnsafeSizeOf(filter, cache, bypassFlyweight);
88-
} catch (UnsupportedOperationException f) {
89-
try {
90-
return new ReflectionSizeOf(filter, cache, bypassFlyweight);
91-
} catch (UnsupportedOperationException g) {
92-
throw new UnsupportedOperationException("A suitable SizeOf engine could not be loaded: " + e + ", " + f + ", " + g);
93-
}
94-
}
95-
}
91+
return new DelegatingSizeOf(new CombinationSizeOfFilter(filters), cache, bypassFlyweight,
92+
AgentSizer::new,
93+
UnsafeSizer::new,
94+
ReflectionSizer::new
95+
);
9696
}
9797

9898
/**
@@ -133,4 +133,49 @@ public long visit(final Object object) {
133133
}
134134
}
135135
}
136+
137+
private static class DelegatingSizeOf extends SizeOf {
138+
139+
private static final Logger LOGGER = LoggerFactory.getLogger(DelegatingSizeOf.class);
140+
141+
private final List<ObjectSizer> sizers;
142+
143+
/**
144+
* Builds a new SizeOf that will filter fields according to the provided filter
145+
*
146+
* @param fieldFilter The filter to apply
147+
* @param caching whether to cache reflected fields
148+
* @param bypassFlyweight whether "Flyweight Objects" are to be ignored
149+
* @see SizeOfFilter
150+
*/
151+
public DelegatingSizeOf(SizeOfFilter fieldFilter, boolean caching, boolean bypassFlyweight, Supplier<ObjectSizer>... sizerFactories) {
152+
super(fieldFilter, caching, bypassFlyweight);
153+
this.sizers = Stream.of(sizerFactories).map(s -> {
154+
try {
155+
return s.get();
156+
} catch (Throwable t) {
157+
LOGGER.warn("Cannot use object sizing " + s, t);
158+
return null;
159+
}
160+
}).filter(Objects::nonNull).collect(toList());
161+
162+
if (sizers.isEmpty()) {
163+
throw new UnsupportedOperationException("A suitable sizing engine could not be loaded");
164+
}
165+
}
166+
167+
@Override
168+
public long sizeOf(Object obj) {
169+
170+
for (ObjectSizer sizer : sizers) {
171+
try {
172+
return sizer.sizeOf(obj);
173+
} catch (Throwable t) {
174+
LOGGER.warn("Failed to size {} with {}", obj, sizer);
175+
}
176+
}
177+
178+
throw new UnsupportedOperationException("Could not size " + obj);
179+
}
180+
}
136181
}

0 commit comments

Comments
 (0)