me.ruebner.jvisualizer.backend.vm.structure.Method Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jvisualizer Show documentation
Show all versions of jvisualizer Show documentation
This project aims to provide an easy tool to visualize data flow and objects within the JVM. It is intended for
students that are starting to learn programming with Java.
The newest version!
package me.ruebner.jvisualizer.backend.vm.structure;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.sun.jdi.AbsentInformationException;
import com.sun.jdi.ClassNotLoadedException;
import me.ruebner.jvisualizer.backend.vm.types.Type;
import me.ruebner.jvisualizer.backend.vm.types.VoidType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
* Represents a Java method.
*/
public class Method {
/**
* the type the method is declared on (class, interface or similar type)
*/
@JsonProperty
private final Type declaringType;
/**
* the name of the method
*/
@JsonProperty
private final String name;
/**
* a list of arguments of the method
*/
@JsonProperty
private final List arguments = new ArrayList<>();
/**
* the return type of the method
*/
@JsonProperty
private final Type returnType;
/**
* true, if the method is a constructor, false otherwise
*/
@JsonProperty
private final boolean isConstructor;
/**
* Creates a new method object.
* @param declaringType the type the method is declared in
* @param name the method's name
* @param arguments a list of arguments of the method (objects will be copied to a new internal
* list)
* @param returnType the return type of the method (including {@link VoidType} for a void method)
* @param isConstructor set to true, if the method is a constructor, or set to false otherwise
*/
private Method(Type declaringType, String name, List arguments, Type returnType, boolean isConstructor) {
this.declaringType = declaringType;
this.name = name;
this.arguments.addAll(arguments);
this.returnType = returnType;
this.isConstructor = isConstructor;
}
/**
* Create a new method object based on a {@link com.sun.jdi.Method} object.
* @param jdiMethod the JDI method object
* @return the new method object representing {@code jdiMethod}
* @throws AbsentInformationException thrown, if the VM does not provide enough information to
* construct this object
*/
public static Method fromJdi(com.sun.jdi.Method jdiMethod) throws AbsentInformationException {
// add arguments
List arguments = new ArrayList<>();
for (var argument : jdiMethod.arguments()) {
arguments.add(LocalVariable.fromJdi(argument));
}
// set special names for constructors and static initializers
String methodName = jdiMethod.name()
.replace("", "(constructor)")
.replace("", "(static initializer)");
try {
return new Method(
Type.fromJdi(jdiMethod.declaringType()),
methodName,
arguments,
Type.fromJdi(jdiMethod.returnType()),
jdiMethod.isConstructor()
);
} catch (ClassNotLoadedException ex) {
return new Method(
Type.fromJdi(jdiMethod.declaringType()),
methodName,
arguments,
new Type(jdiMethod.returnTypeName()) {},
jdiMethod.isConstructor()
);
}
}
/**
* Retrieve the name of the method.
* @return the name of the method
*/
public String getName() {
return name;
}
/**
* Retrieve an unmodifiable list of the arguments of this method. Values can be retrieved via
* the corresponding {@link StackFrame} object.
* @return a list of this method's arguments
*/
public List getArguments() {
return Collections.unmodifiableList(arguments);
}
/**
* Retrieves the return type of the method.
* @return the method's return type
*/
public Type getReturnType() {
return returnType;
}
/**
* Retrieves the type this method was declared in.
* @return the method's declaring type
*/
public Type getDeclaringType() {
return declaringType;
}
/**
* Checks, if the method is a constructor.
* @return true, if the method is a constructor, false otherwise
*/
public boolean isConstructor() {
return isConstructor;
}
@Override
public String toString() {
return returnType.getName() + " "
+ name + " "
+ "("
+ arguments.stream()
.map((arg) -> arg.getType().getName())
.collect(Collectors.joining(", "))
+ ")";
}
}