-
Notifications
You must be signed in to change notification settings - Fork 872
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix printMuzzleReferences gradle task (#3808)
* Fix printMuzzleReferences gradle task * suppress SystemOut warnings * bump gradle-plugins version
- Loading branch information
Mateusz Rzeszutek
authored
Aug 10, 2021
1 parent
beab394
commit bebb877
Showing
3 changed files
with
77 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
muzzle/src/main/java/io/opentelemetry/javaagent/tooling/muzzle/ReferencesPrinter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.tooling.muzzle; | ||
|
||
import static java.lang.System.lineSeparator; | ||
|
||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
import io.opentelemetry.javaagent.extension.muzzle.ClassRef; | ||
import io.opentelemetry.javaagent.extension.muzzle.FieldRef; | ||
import io.opentelemetry.javaagent.extension.muzzle.MethodRef; | ||
import io.opentelemetry.javaagent.extension.muzzle.Source; | ||
import java.util.ServiceLoader; | ||
|
||
@SuppressWarnings("SystemOut") | ||
public final class ReferencesPrinter { | ||
|
||
private static final String INDENT = " "; | ||
|
||
/** | ||
* For all {@link InstrumentationModule}s found in the current thread's context classloader this | ||
* method prints references returned by the {@link InstrumentationModule#getMuzzleReferences()} | ||
* method to the standard output. | ||
*/ | ||
public static void printMuzzleReferences() { | ||
for (InstrumentationModule instrumentationModule : | ||
ServiceLoader.load(InstrumentationModule.class)) { | ||
try { | ||
System.out.println(instrumentationModule.getClass().getName()); | ||
for (ClassRef ref : instrumentationModule.getMuzzleReferences().values()) { | ||
System.out.print(prettyPrint(ref)); | ||
} | ||
} catch (RuntimeException e) { | ||
String message = | ||
"Unexpected exception printing references for " | ||
+ instrumentationModule.getClass().getName(); | ||
System.out.println(message); | ||
throw new IllegalStateException(message, e); | ||
} | ||
} | ||
} | ||
|
||
private static String prettyPrint(ClassRef ref) { | ||
StringBuilder builder = new StringBuilder(INDENT).append(ref).append(lineSeparator()); | ||
if (!ref.getSources().isEmpty()) { | ||
builder.append(INDENT).append(INDENT).append("Sources:").append(lineSeparator()); | ||
for (Source source : ref.getSources()) { | ||
builder | ||
.append(INDENT) | ||
.append(INDENT) | ||
.append(INDENT) | ||
.append("at: ") | ||
.append(source) | ||
.append(lineSeparator()); | ||
} | ||
} | ||
for (FieldRef field : ref.getFields()) { | ||
builder.append(INDENT).append(INDENT).append(field).append(lineSeparator()); | ||
} | ||
for (MethodRef method : ref.getMethods()) { | ||
builder.append(INDENT).append(INDENT).append(method).append(lineSeparator()); | ||
} | ||
return builder.toString(); | ||
} | ||
|
||
private ReferencesPrinter() {} | ||
} |