Skip to content

Commit

Permalink
style: run spotless over code
Browse files Browse the repository at this point in the history
  • Loading branch information
jumanji144 committed Dec 9, 2023
1 parent 88c6480 commit 84b6524
Show file tree
Hide file tree
Showing 97 changed files with 4,552 additions and 4,426 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import me.darknet.assembler.compile.JvmCompiler;
import me.darknet.assembler.compile.JvmCompilerOptions;
import me.darknet.assembler.compile.analysis.EmptyMethodAnalysisLookup;
import me.darknet.assembler.compile.analysis.MethodAnalysisLookup;
import me.darknet.assembler.compiler.Compiler;
import me.darknet.assembler.compiler.CompilerOptions;
import me.darknet.assembler.helper.Processor;
Expand All @@ -31,10 +30,15 @@ public class CompileCommand implements Runnable {
@CommandLine.Option(names = { "-s", "--source" }, description = "Source code", paramLabel = "code")
private Optional<String> sourceCode;

@CommandLine.Option(names = { "-ov", "--overlay" }, description = "Overlay class file\nRequired for non-class code", paramLabel = "file")
@CommandLine.Option(
names = { "-ov",
"--overlay" }, description = "Overlay class file\nRequired for non-class code", paramLabel = "file"
)
private Optional<File> overlay;

@CommandLine.Option(names = { "-at", "--annotation-target" }, description = "Annotation target", paramLabel = "target")
@CommandLine.Option(
names = { "-at", "--annotation-target" }, description = "Annotation target", paramLabel = "target"
)
private Optional<String> annotationTarget;

@CommandLine.Option(
Expand All @@ -56,17 +60,15 @@ private void configureCompiler() {
default -> throw new UnsupportedOperationException("Unknown target: " + MainCommand.target);
}

options.version(bytecodeVersion)
.overlay(new JavaClassRepresentation(overlay.map(file -> {
try {
return Files.readAllBytes(file.toPath());
} catch (IOException e) {
System.err.println("Failed to read overlay file: " + e.getMessage());
System.exit(1);
return null;
}
}).orElse(null), EmptyMethodAnalysisLookup.instance()))
.annotationPath(annotationTarget.orElse(null));
options.version(bytecodeVersion).overlay(new JavaClassRepresentation(overlay.map(file -> {
try {
return Files.readAllBytes(file.toPath());
} catch (IOException e) {
System.err.println("Failed to read overlay file: " + e.getMessage());
System.exit(1);
return null;
}
}).orElse(null), EmptyMethodAnalysisLookup.instance())).annotationPath(annotationTarget.orElse(null));
}

private void validateAst(List<ASTElement> ast) {
Expand All @@ -76,15 +78,16 @@ private void validateAst(List<ASTElement> ast) {
}

switch (ast.get(0).type()) {
case CLASS -> {}
case CLASS -> {
}
case METHOD, FIELD -> {
if(overlay.isEmpty()) {
if (overlay.isEmpty()) {
System.err.println("Overlay is required for non-class code");
System.exit(1);
}
}
case ANNOTATION -> {
if(overlay.isEmpty() || annotationTarget.isEmpty()) {
if (overlay.isEmpty() || annotationTarget.isEmpty()) {
System.err.println("Overlay and annotation target are required for annotation code");
System.exit(1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

/**
* @param classFile
* Raw class file.
* Raw class file.
* @param analysisLookup
* Lookup to get method stack analysis information for declared methods.
* Lookup to get method stack analysis information for
* declared methods.
*/
public record JavaClassRepresentation(byte[] classFile, MethodAnalysisLookup analysisLookup) implements ClassRepresentation {
public record JavaClassRepresentation(byte[] classFile, MethodAnalysisLookup analysisLookup)
implements ClassRepresentation {
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ int correctFlags(ClassFileView classFileView) {

builder.setVersion(blwOptions.version);

if(blwOptions.overlay != null)
if (blwOptions.overlay != null)
applyOverlay(collector, builder, blwOptions.overlay.classFile());
if (collector.hasErr()) {
return new Result<>(null, collector.getErrors());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package me.darknet.assembler.compile;

import dev.xdark.blw.version.JavaVersion;
import me.darknet.assembler.compile.analysis.jvm.JvmAnalysisEngineFactory;
import me.darknet.assembler.compile.analysis.VariableNameLookup;
import me.darknet.assembler.compile.analysis.jvm.JvmAnalysisEngine;
import me.darknet.assembler.compile.analysis.jvm.JvmAnalysisEngineFactory;
import me.darknet.assembler.compile.analysis.jvm.TypedJvmAnalysisEngine;
import me.darknet.assembler.compiler.ClassRepresentation;
import me.darknet.assembler.compiler.CompilerOptions;
Expand Down Expand Up @@ -76,7 +76,7 @@ public String annotationPath() {

@Override
public JvmCompilerOptions overlay(ClassRepresentation representation) {
if(!(representation instanceof JavaClassRepresentation))
if (!(representation instanceof JavaClassRepresentation))
throw new IllegalArgumentException("ClassRepresentation must be a JavaClassRepresentation");
this.overlay = (JavaClassRepresentation) representation;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,43 @@
* <p/>
* Common cases:
* <ul>
* <li>{@link #getCause()} {@code instanceof} {@link FrameMergeException}</li>
* <li>{@link #getCause()} is any exception raised when executing an instruction in the analysis engine</li>
* <li>Analysis forking exceeded capacity, short circuited to prevent excessive computation</li>
* <li>Branch target is unknown, cannot correctly complete analysis</li>
* <li>{@link #getCause()} {@code instanceof} {@link FrameMergeException}</li>
* <li>{@link #getCause()} is any exception raised when executing an instruction
* in the analysis engine</li>
* <li>Analysis forking exceeded capacity, short circuited to prevent excessive
* computation</li>
* <li>Branch target is unknown, cannot correctly complete analysis</li>
* </ul>
*/
public class AnalysisException extends SimulationException {
private final CodeElement element;

public AnalysisException(@Nullable CodeElement element, @Nullable Throwable cause, @NotNull String message) {
super(message, cause);
this.element = element;
}

public AnalysisException(@NotNull Throwable cause, @NotNull String message) {
this(null, cause, message);
}

public AnalysisException(@NotNull CodeElement element, @NotNull String message) {
this(element, null, message);
}

public AnalysisException(@NotNull CodeElement element, @NotNull Throwable cause) {
this(element, cause, cause.getMessage());
}

public AnalysisException(@NotNull String message) {
this(null, null, message);
}

/**
* @return Element linked to the failure. May be {@code null} in some cases.
*/
@Nullable
public CodeElement getElement() {
return element;
}
private final CodeElement element;

public AnalysisException(@Nullable CodeElement element, @Nullable Throwable cause, @NotNull String message) {
super(message, cause);
this.element = element;
}

public AnalysisException(@NotNull Throwable cause, @NotNull String message) {
this(null, cause, message);
}

public AnalysisException(@NotNull CodeElement element, @NotNull String message) {
this(element, null, message);
}

public AnalysisException(@NotNull CodeElement element, @NotNull Throwable cause) {
this(element, cause, cause.getMessage());
}

public AnalysisException(@NotNull String message) {
this(null, null, message);
}

/**
* @return Element linked to the failure. May be {@code null} in some cases.
*/
@Nullable
public CodeElement getElement() {
return element;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,47 @@
* Stack analysis results for a single method.
*/
public interface AnalysisResults {
/**
* @param index
* Key.
*
* @return Frame at index, or {@code null} if not present.
*/
@Nullable
default Frame getFrame(int index) {
return frames().get(index);
}
/**
* @param index
* Key.
*
* @return Frame at index, or {@code null} if not present.
*/
@Nullable
default Frame getFrame(int index) {
return frames().get(index);
}

/**
* Map of instruction offsets to method stack frames.
* Keys are equal to the indices of items within the {@link ASTCode#instructions()}.
*
* @return Navigable map of instruction offsets to frame information.
*/
@NotNull
NavigableMap<Integer, Frame> frames();
/**
* Map of instruction offsets to method stack frames. Keys are equal to the
* indices of items within the {@link ASTCode#instructions()}.
*
* @return Navigable map of instruction offsets to frame information.
*/
@NotNull
NavigableMap<Integer, Frame> frames();

/**
* Map of instruction offsets to method stack frames.
* Keys are equal to the indices of items within the {@link ASTCode#instructions()}.
* Keys will always align with {@code return} and {@code athrow} instructions.
*
* @return Navigable map of terminal instruction offsets to frame information.
*/
@NotNull
NavigableMap<Integer, Frame> terminalFrames();
/**
* Map of instruction offsets to method stack frames. Keys are equal to the
* indices of items within the {@link ASTCode#instructions()}. Keys will always
* align with {@code return} and {@code athrow} instructions.
*
* @return Navigable map of terminal instruction offsets to frame information.
*/
@NotNull
NavigableMap<Integer, Frame> terminalFrames();

/**
* @return The exception thrown when handling method flow analysis.
* Will be {@code null} if analysis completed without problems.
*/
@Nullable
AnalysisException getAnalysisFailure();
/**
* @return The exception thrown when handling method flow analysis. Will be
* {@code null} if analysis completed without problems.
*/
@Nullable
AnalysisException getAnalysisFailure();

/**
* @param analysisFailure
* The exception thrown when handling method flow analysis.
*/
void setAnalysisFailure(@Nullable AnalysisException analysisFailure);
/**
* @param analysisFailure
* The exception thrown when handling method flow
* analysis.
*/
void setAnalysisFailure(@Nullable AnalysisException analysisFailure);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,38 @@
import org.jetbrains.annotations.NotNull;

public class AnalysisUtils {
/**
* Dummy type used as a placeholder for null.
*/
public static final ObjectType NULL = Types.instanceTypeFromDescriptor("null");
/**
* Dummy type used as a placeholder for null.
*/
public static final ObjectType NULL = Types.instanceTypeFromDescriptor("null");

/**
* @param checker
* Inheritance checker to use for determining common super-types.
* @param a
* Some type.
* @param b
* Some type.
*
* @return Common type between the two.
*/
@NotNull
public static ClassType commonType(@NotNull InheritanceChecker checker, @NotNull ClassType a, @NotNull ClassType b) {
if (a instanceof PrimitiveType ap && b instanceof PrimitiveType bp) {
return bp.widen(ap.widen(bp));
} else if (a instanceof ObjectType ao && b instanceof ObjectType bo) {
if (ao.equals(bo)) return ao;
/**
* @param checker
* Inheritance checker to use for determining common super-types.
* @param a
* Some type.
* @param b
* Some type.
*
* @return Common type between the two.
*/
@NotNull
public static ClassType commonType(@NotNull InheritanceChecker checker, @NotNull ClassType a,
@NotNull ClassType b) {
if (a instanceof PrimitiveType ap && b instanceof PrimitiveType bp) {
return bp.widen(ap.widen(bp));
} else if (a instanceof ObjectType ao && b instanceof ObjectType bo) {
if (ao.equals(bo))
return ao;

String commonType = checker.getCommonSuperclass(ao.internalName(), bo.internalName());
String commonType = checker.getCommonSuperclass(ao.internalName(), bo.internalName());

if (commonType == null) return Types.OBJECT;
else return Types.objectTypeFromInternalName(commonType);
} else {
return Types.OBJECT;
}
}
if (commonType == null)
return Types.OBJECT;
else
return Types.objectTypeFromInternalName(commonType);
} else {
return Types.OBJECT;
}
}
}
Loading

0 comments on commit 84b6524

Please sign in to comment.