Skip to content

Commit

Permalink
Add optional setup method to JvmClassTransformer interface
Browse files Browse the repository at this point in the history
Some transformers may want to do one-time setup operations, so this is a new place for that to happen in besides the constructor.
  • Loading branch information
Col-E committed Dec 6, 2024
1 parent f0e9437 commit fab9408
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
public class StaticValueCollectionTransformer implements JvmClassTransformer {
private final Map<String, StaticValues> classValues = new ConcurrentHashMap<>();
private final Map<String, EffectivelyFinalFields> classFinals = new ConcurrentHashMap<>();
private final Map<Workspace, InheritanceGraph> graphCache = new IdentityHashMap<>();
private final InheritanceGraphService graphService;
private final WorkspaceManager workspaceManager;
private InheritanceGraph inheritanceGraph;

@Inject
public StaticValueCollectionTransformer(@Nonnull WorkspaceManager workspaceManager, @Nonnull InheritanceGraphService graphService) {
Expand All @@ -66,15 +66,17 @@ public ReValue getStaticValue(@Nonnull String className, @Nonnull String fieldNa
return values.get(fieldName, fieldDesc);
}

@Override
public void setup(@Nonnull JvmTransformerContext context, @Nonnull Workspace workspace) {
inheritanceGraph = workspace == workspaceManager.getCurrent() ?
graphService.getCurrentWorkspaceInheritanceGraph() :
graphService.newInheritanceGraph(workspace);
}

@Override
public void transform(@Nonnull JvmTransformerContext context, @Nonnull Workspace workspace,
@Nonnull WorkspaceResource resource, @Nonnull JvmClassBundle bundle,
@Nonnull JvmClassInfo classInfo) throws TransformationException {
// TODO: Instead of a map, we should make a workspace setup call first
InheritanceGraph inheritanceGraph = graphCache.computeIfAbsent(workspace, w -> w == workspaceManager.getCurrent() ?
graphService.getCurrentWorkspaceInheritanceGraph() :
graphService.newInheritanceGraph(workspace));

StaticValues valuesContainer = new StaticValues();
EffectivelyFinalFields finalContainer = new EffectivelyFinalFields();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
* @author Matt Coley
*/
public interface JvmClassTransformer {
/**
* Used to do any workspace-scope setup actions before transformations occur.
*
* @param context
* Transformation context for access to other transformers and recording class changes.
* @param workspace
* Workspace containing classes to transform.
*/
default void setup(@Nonnull JvmTransformerContext context, @Nonnull Workspace workspace) {}

/**
* Implementations can {@link #dependencies() depend on other transformers} and access them
* via {@link JvmTransformerContext#getJvmTransformer(Class)}. This may be useful in cases where you want to have
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ public TransformResult transformJvm(@Nonnull List<Class<? extends JvmClassTransf
WorkspaceResource resource = workspace.getPrimaryResource();
ResourcePathNode resourcePath = PathNodes.resourcePath(workspace, resource);
JvmTransformerContext context = new JvmTransformerContext(workspace, resource, queue.transformers);
for (JvmClassTransformer transformer : queue.transformers) {
try {
transformer.setup(context, workspace);
} catch (Throwable t) {
// If setup fails, abort the transformation
String message = "Transformer '" + transformer.name() + "' failed on setup";
logger.error(message, t);
throw new TransformationException(message, t);
}
}
resource.jvmClassBundleStreamRecursive().forEach(bundle -> {
BundlePathNode bundlePathNode = resourcePath.child(bundle);
for (JvmClassTransformer transformer : queue.transformers) {
Expand Down

0 comments on commit fab9408

Please sign in to comment.