Skip to content

Commit

Permalink
Code cleanup and nullable annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
jaskarth committed Oct 16, 2023
1 parent a3fb2e4 commit 902d878
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.java.decompiler.main;

import org.jetbrains.annotations.Nullable;
import org.jetbrains.java.decompiler.main.collectors.BytecodeSourceMapper;
import org.jetbrains.java.decompiler.main.collectors.CounterContainer;
import org.jetbrains.java.decompiler.main.collectors.ImportCollector;
Expand Down Expand Up @@ -102,7 +103,7 @@ public static void startMethod(VarProcessor varProcessor) {
// context access
// *****************************************************************************

public static <T> T getContextProperty(Key<T> key) {
public static <T> @Nullable T getContextProperty(Key<T> key) {
return (T) getCurrentContext().staticProps.get(key);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.java.decompiler.modules.decompiler;

import org.jetbrains.annotations.Nullable;
import org.jetbrains.java.decompiler.modules.decompiler.stats.Statement;
import org.jetbrains.java.decompiler.modules.decompiler.stats.Statement.EdgeDirection;

Expand Down Expand Up @@ -52,7 +53,7 @@ public class StatEdge {
// The body of the if statement would be considered the source, and the println would be considered the destination.
// The sequence statement enclosing the if would be considered the closure.
// BREAK and CONTINUE edge types should always have a closure! (except for when break edges are return edges)
public Statement closure;
public @Nullable Statement closure;

// Whether this edge is labeled or not.
public boolean labeled = true;
Expand All @@ -66,7 +67,7 @@ public class StatEdge {
// If this edge is a continue edge set as a break edge for readability
public boolean phantomContinue = false;

public StatEdge(int type, Statement source, Statement destination, Statement closure) {
public StatEdge(int type, Statement source, Statement destination, @Nullable Statement closure) {
this(type, source, destination);
this.closure = closure;
}
Expand Down Expand Up @@ -100,6 +101,7 @@ public Statement getSource() {
}

public void setSource(Statement source) {
ValidationHelper.assertTrue(source != null, "Should not be null");
this.source = source;
}

Expand All @@ -111,7 +113,7 @@ public void setSource(Statement source) {
* @param newSource the new source of this edge
*/
public void changeSource(Statement newSource) {
ValidationHelper.notNull(newSource);
ValidationHelper.assertTrue(newSource != null, "Should not be null");

Statement oldSource = this.source;
oldSource.removeEdgeInternal(EdgeDirection.FORWARD, this);
Expand All @@ -124,6 +126,7 @@ public Statement getDestination() {
}

public void setDestination(Statement destination) {
ValidationHelper.assertTrue(destination != null, "Should not be null");
this.destination = destination;
}

Expand All @@ -135,7 +138,7 @@ public void setDestination(Statement destination) {
* @param newDestination the new destination of this edge
*/
public void changeDestination(Statement newDestination) {
ValidationHelper.notNull(newDestination);
ValidationHelper.assertTrue(newDestination != null, "Should not be null");

Statement oldDestination = this.destination;
oldDestination.removeEdgeInternal(EdgeDirection.BACKWARD, this);
Expand Down Expand Up @@ -168,12 +171,12 @@ public void remove() {
}
}


/**
* Remove the closure of this edge. This edge will no
* longer be labeled.
*/
public void removeClosure() {
ValidationHelper.validateTrue(closure != null, "closure shouldn't be null here");
this.closure.getLabelEdges().remove(this);
this.labeled = false;
this.closure = null;
Expand All @@ -183,10 +186,6 @@ public List<String> getExceptions() {
return this.exceptions;
}

// public void setException(String exception) {
// this.exception = exception;
// }

@Override
public String toString() {
return this.type + ": " + this.source.toString() + " -> " + this.destination.toString() + ((this.closure == null) ? "" : " (" + this.closure + ")") + ((this.exceptions == null) ? "" : " Exceptions: " + this.exceptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public TextBuffer toJava(int indent) {
return buf;
}
else {
MethodWrapper method = (MethodWrapper)DecompilerContext.getContextProperty(DecompilerContext.CURRENT_METHOD_WRAPPER);
ClassNode node = ((ClassNode)DecompilerContext.getContextProperty(DecompilerContext.CURRENT_CLASS_NODE));
MethodWrapper method = DecompilerContext.getContextProperty(DecompilerContext.CURRENT_METHOD_WRAPPER);
ClassNode node = DecompilerContext.getContextProperty(DecompilerContext.CURRENT_CLASS_NODE);

if (method != null && node != null) {
StructExceptionsAttribute attr = method.methodStruct.getAttribute(StructGeneralAttribute.ATTRIBUTE_EXCEPTIONS);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.java.decompiler.modules.decompiler.vars;

import org.jetbrains.annotations.Nullable;
import org.jetbrains.java.decompiler.main.collectors.VarNamesCollector;
import org.jetbrains.java.decompiler.modules.decompiler.exps.VarExprent;
import org.jetbrains.java.decompiler.modules.decompiler.stats.RootStatement;
Expand All @@ -25,7 +26,7 @@ public class VarProcessor {
private Map<VarVersionPair, String> mapVarNames = new HashMap<>();
private List<VarVersionPair> params = new ArrayList<>();
private Map<VarVersionPair, LocalVariable> mapVarLVTs = new HashMap<>();
private VarVersionsProcessor varVersions;
private @Nullable VarVersionsProcessor varVersions;
private final Map<VarVersionPair, String> thisVars = new HashMap<>();
private final Set<VarVersionPair> externalVars = new HashSet<>();
private final Map<VarVersionPair, String> clashingNames = new HashMap<>();
Expand Down Expand Up @@ -104,7 +105,7 @@ public void rerunClashing(RootStatement root) {
}
}

public Integer getVarOriginalIndex(int index) {
public @Nullable Integer getVarOriginalIndex(int index) {
if (varVersions == null) {
return null;
}
Expand Down Expand Up @@ -145,11 +146,11 @@ public void setVarType(VarVersionPair pair, VarType type) {
}
}

public String getVarName(VarVersionPair pair) {
public @Nullable String getVarName(VarVersionPair pair) {
return mapVarNames == null ? null : mapVarNames.get(pair);
}

public String getClashingName(VarVersionPair pair) {
public @Nullable String getClashingName(VarVersionPair pair) {
return this.clashingNames.get(pair);
}

Expand Down Expand Up @@ -223,49 +224,19 @@ public void findLVT(VarExprent exprent, int start) {
}
}

public void copyVarInfo(VarVersionPair from, VarVersionPair to) {
setVarName(to, getVarName(from));
setVarFinal(to, getVarFinal(from));
setVarType(to, getVarType(from));
varVersions.getMapOriginalVarIndices().put(to.var, varVersions.getMapOriginalVarIndices().get(from.var));
}

public boolean hasLVT() {
return method.getLocalVariableAttr() != null;
}


public Map<Integer, LocalVariable> getLocalVariables(Statement stat) {
if (!hasLVT() || stat == null)
return new HashMap<>();

final StartEndPair sep = stat.getStartEndRange();
final Set<Integer> blacklist = new HashSet<>();
Map<Integer, LocalVariable> ret = method.getLocalVariableAttr().getVariables().filter(lv -> lv.getEnd() > sep.start && lv.getStart() <= sep.end)
.collect(Collectors.toMap(lv -> lv.getVersion().var, lv -> lv,
(lv1, lv2) ->
{
//System.out.println("DUPLICATE INDEX FOR SCOPE: (" +sep +") " + lv1.toString() + " " + lv2.toString());
blacklist.add(lv1.getVersion().var);
return lv1;
}
));

for (int b : blacklist)
ret.remove(b);

return ret;
}

public VarVersionsProcessor getVarVersions() {
public @Nullable VarVersionsProcessor getVarVersions() {
return varVersions;
}

public void setVarLVT(VarVersionPair var, LocalVariable lvt) {
mapVarLVTs.put(var, lvt);
}

public LocalVariable getVarLVT(VarVersionPair var) {
public @Nullable LocalVariable getVarLVT(VarVersionPair var) {
return mapVarLVTs.get(var);
}
}
2 changes: 1 addition & 1 deletion src/org/jetbrains/java/decompiler/struct/gen/VarType.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class VarType { // TODO: optimize switch

public final int type;
public final int arrayDim;
public final String value;
public final @Nullable String value;
public final int typeFamily;
public final int stackSize;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -578,8 +578,7 @@ public static void cleanLoweredGenericTypes(Map<VarType, VarType> tempMap, Gener
// In the case where Type is defined as "class Type<T>", and the in type is "Type<?>", find the base (un-remapped) type "Type<T>"
// out of it. Essentially a wrapper to do a class lookup to find the generic class type descriptor.
// Returns null if there is a failure at any point.
@Nullable
public GenericType findBaseType() {
public @Nullable GenericType findBaseType() {
StructClass cl = DecompilerContext.getStructContext().getClass(value);
if (cl == null) {
return null;
Expand Down

0 comments on commit 902d878

Please sign in to comment.