Skip to content

Commit

Permalink
Improve support for Java 9+
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdennis committed Apr 20, 2022
1 parent 6bf8d35 commit fca2c32
Show file tree
Hide file tree
Showing 19 changed files with 602 additions and 935 deletions.
89 changes: 40 additions & 49 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>org.ehcache</groupId>
<artifactId>sizeof</artifactId>
<version>0.4.1-SNAPSHOT</version>
<version>0.5-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Ehcache SizeOf Engine</name>
Expand Down Expand Up @@ -40,6 +40,11 @@
</properties>

<dependencies>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>1.12.9</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand All @@ -64,9 +69,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>6.0</version>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.12.9</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -92,7 +97,7 @@
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.20.1</version>
<version>2.22.2</version>
</plugin>
<plugin>
<artifactId>maven-gpg-plugin</artifactId>
Expand Down Expand Up @@ -129,9 +134,9 @@
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.5</version>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>4.6.0.0</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
Expand Down Expand Up @@ -188,52 +193,13 @@
</goals>
<configuration>
<excludes>
<exclude>src/hidden/**</exclude>
<exclude>README.adoc</exclude>
<exclude>**/*.txt</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<executions>
<execution>
<id>create-agent-jar</id>
<phase>process-classes</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<scripts>
<script>
def jarFile = new File(project.build.directory, "/classes/org/ehcache/sizeof/impl/sizeof-agent.jar")
def agentClass = "/org/ehcache/sizeof/impl/SizeOfAgent.class"
def agentDir = project.build.directory + "/agent-jar"
def manifestDir = project.basedir.getAbsolutePath() + "/src/hidden/resources"
ant.move(file: new File(project.build.outputDirectory, agentClass),
tofile: new File(agentDir, agentClass))

ant.jar(destfile: jarFile, basedir: new File(agentDir).getAbsolutePath(), manifest: new File(manifestDir, "/META-INF/MANIFEST.MF"))

ant.delete(dir: new File(agentDir))
</script>
</scripts>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<!-- any version of Groovy \>= 1.5.0 should work here -->
<version>2.4.12</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
Expand Down Expand Up @@ -275,8 +241,8 @@
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<configuration>
<excludeFilterFile>${basedir}/findbugs-exclude-filter.xml</excludeFilterFile>
</configuration>
Expand Down Expand Up @@ -309,6 +275,31 @@
</plugins>
</build>

<profiles>
<profile>
<id>java9</id>
<activation>
<jdk>[9,)</jdk>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Xms64m -Xmx64m --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.util.concurrent.locks=ALL-UNNAMED</argLine>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<argLine>-Xms64m -Xmx64m --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.util.concurrent.locks=ALL-UNNAMED</argLine>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<scm>
<url>https://github.com/ehcache/sizeof</url>
<connection>scm:git:https://github.com/ehcache/sizeof.git</connection>
Expand Down
3 changes: 0 additions & 3 deletions src/hidden/resources/META-INF/MANIFEST.MF

This file was deleted.

21 changes: 21 additions & 0 deletions src/main/java/org/ehcache/sizeof/ObjectSizer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright Terracotta, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ehcache.sizeof;

public interface ObjectSizer {

long sizeOf(Object obj);
}
79 changes: 62 additions & 17 deletions src/main/java/org/ehcache/sizeof/SizeOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,19 @@

import org.ehcache.sizeof.filters.CombinationSizeOfFilter;
import org.ehcache.sizeof.filters.SizeOfFilter;
import org.ehcache.sizeof.impl.AgentSizeOf;
import org.ehcache.sizeof.impl.ReflectionSizeOf;
import org.ehcache.sizeof.impl.UnsafeSizeOf;
import org.ehcache.sizeof.impl.AgentSizer;
import org.ehcache.sizeof.impl.ReflectionSizer;
import org.ehcache.sizeof.impl.UnsafeSizer;
import org.ehcache.sizeof.util.WeakIdentityConcurrentMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

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

public static SizeOf newInstance(boolean bypassFlyweight, boolean cache, final SizeOfFilter... filters) {
final SizeOfFilter filter = new CombinationSizeOfFilter(filters);
try {
return new AgentSizeOf(filter, cache, bypassFlyweight);
} catch (UnsupportedOperationException e) {
try {
return new UnsafeSizeOf(filter, cache, bypassFlyweight);
} catch (UnsupportedOperationException f) {
try {
return new ReflectionSizeOf(filter, cache, bypassFlyweight);
} catch (UnsupportedOperationException g) {
throw new UnsupportedOperationException("A suitable SizeOf engine could not be loaded: " + e + ", " + f + ", " + g);
}
}
}
return new DelegatingSizeOf(new CombinationSizeOfFilter(filters), cache, bypassFlyweight,
AgentSizer::new,
UnsafeSizer::new,
ReflectionSizer::new
);
}

/**
Expand Down Expand Up @@ -133,4 +133,49 @@ public long visit(final Object object) {
}
}
}

private static class DelegatingSizeOf extends SizeOf {

private static final Logger LOGGER = LoggerFactory.getLogger(DelegatingSizeOf.class);

private final List<ObjectSizer> sizers;

/**
* Builds a new SizeOf that will filter fields according to the provided filter
*
* @param fieldFilter The filter to apply
* @param caching whether to cache reflected fields
* @param bypassFlyweight whether "Flyweight Objects" are to be ignored
* @see SizeOfFilter
*/
public DelegatingSizeOf(SizeOfFilter fieldFilter, boolean caching, boolean bypassFlyweight, Supplier<ObjectSizer>... sizerFactories) {
super(fieldFilter, caching, bypassFlyweight);
this.sizers = Stream.of(sizerFactories).map(s -> {
try {
return s.get();
} catch (Throwable t) {
LOGGER.warn("Cannot use object sizing " + s, t);
return null;
}
}).filter(Objects::nonNull).collect(toList());

if (sizers.isEmpty()) {
throw new UnsupportedOperationException("A suitable sizing engine could not be loaded");
}
}

@Override
public long sizeOf(Object obj) {

for (ObjectSizer sizer : sizers) {
try {
return sizer.sizeOf(obj);
} catch (Throwable t) {
LOGGER.warn("Failed to size {} with {}", obj, sizer);
}
}

throw new UnsupportedOperationException("Could not size " + obj);
}
}
}
Loading

0 comments on commit fca2c32

Please sign in to comment.