package jetbrains.mps.debugger.java.runtime.breakpoints;
/*Generated by MPS */
import jetbrains.mps.debug.api.breakpoints.ILocationBreakpoint;
import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
import org.jetbrains.mps.openapi.model.SNodeReference;
import jetbrains.mps.debug.api.breakpoints.BreakpointLocation;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.project.Project;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.debugger.java.runtime.engine.events.EventsProcessor;
import com.sun.jdi.ReferenceType;
import jetbrains.mps.debugger.java.runtime.engine.RequestManager;
import java.util.List;
import com.sun.jdi.Location;
import com.sun.jdi.request.BreakpointRequest;
import com.sun.jdi.ClassNotPreparedException;
import com.sun.jdi.ObjectCollectedException;
import com.sun.jdi.InvalidLineNumberException;
import com.sun.jdi.InternalException;
import org.jetbrains.annotations.Nullable;
public class LineBreakpoint extends JavaBreakpoint implements ILocationBreakpoint {
private static final Logger LOG = LogManager.getLogger(LineBreakpoint.class);
private final SNodeReference myNode;
private BreakpointLocation myLocation;
public LineBreakpoint(@NotNull SNodeReference nodePointer, Project project) {
super(project);
myNode = nodePointer;
}
public LineBreakpoint(@NotNull SNode node, Project project) {
this(node.getReference(), project);
}
@NotNull
@Override
public BreakpointLocation getLocation() {
if (myLocation == null) {
myLocation = new BreakpointLocationUpdate(myNode, getRepository()).get();
}
return myLocation;
}
@Override
protected void createRequestForPreparedClass(EventsProcessor debugProcess, final ReferenceType classType) {
RequestManager requestManager = debugProcess.getRequestManager();
try {
int lineIndex = getLocation().getLineIndexInFile();
List<Location> locs = classType.locationsOfLine(lineIndex);
if (locs.size() > 0) {
for (final Location location : locs) {
BreakpointRequest request = requestManager.createBreakpointRequest(this, location);
requestManager.enableRequest(request);
}
} else {
// there's no executable code in this class
requestManager.setInvalid(this, "no executable code found");
String message = "No locations of type " + classType.name() + " found at line " + lineIndex;
LOG.warn(message);
}
} catch (ClassNotPreparedException ex) {
LOG.warn("ClassNotPreparedException: " + ex.getMessage());
// there's a chance to add a breakpoint when the class is prepared
} catch (ObjectCollectedException ex) {
LOG.warn("ObjectCollectedException: " + ex.getMessage());
// there's a chance to add a breakpoint when the class is prepared
} catch (InvalidLineNumberException ex) {
requestManager.setInvalid(this, "no executable code found");
LOG.warn("InvalidLineNumberException: " + ex.getMessage());
} catch (InternalException ex) {
LOG.error(null, ex);
} catch (Exception ex) {
LOG.error(null, ex);
}
}
@Nullable
@Override
protected String getClassNameToPrepare() {
String className = getLocation().getTargetUnitName();
if (className == null) {
// todo when this case does actually happen?
String fileName = getLocation().getFileName();
if (fileName != null && fileName.endsWith(".java")) {
fileName = fileName.substring(0, fileName.length() - ".java".length());
className = myNode.getModelReference().getName().getLongName() + "." + fileName;
} else {
return null;
}
}
return className;
}
@NotNull
@Override
public JavaBreakpointKind getKind() {
return JavaBreakpointKind.LINE_BREAKPOINT;
}
@Override
public boolean isValid() {
return isNotEmptyString(getLocation().getTargetUnitName()) || isNotEmptyString(getLocation().getFileName());
}
@Override
public String getPresentation() {
return new BreakpointPresentation(getLocation(), getRepository()).getText();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
return eq_owwtjm_a0d0o(getLocation(), ((LineBreakpoint) o).getLocation());
}
@Override
public int hashCode() {
return myNode.hashCode() + getKind().hashCode() * 31;
}
private static boolean isNotEmptyString(String str) {
return str != null && str.length() > 0;
}
private static boolean eq_owwtjm_a0d0o(Object a, Object b) {
return (a != null ? a.equals(b) : a == b);
}
}