Skip to content

Commit

Permalink
use Optionals
Browse files Browse the repository at this point in the history
  • Loading branch information
nbauma109 committed May 3, 2023
1 parent ea19fa5 commit 2830dcc
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

package org.jd.core.v1.printer;

import java.util.Optional;

public class PlainTextMetaPrinter extends PlainTextPrinter {
// --- Printer --- //
@Override
Expand Down Expand Up @@ -35,7 +37,7 @@ public void printReference(int type, String internalTypeName, String name, Strin
sb.append("<META-REFERENCE type='");
printType(type);
sb.append("' internalName='");
sb.append(internalTypeName==null ? "?" : internalTypeName);
sb.append(Optional.ofNullable(internalTypeName).orElse("?"));
sb.append("' descriptor='");
sb.append(descriptor);
sb.append("' ownerInternalName='");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.util.Iterator;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

public class Loop {
Expand Down Expand Up @@ -89,6 +90,6 @@ public String toString() {
}
}

return str + "], end=" + (end ==null ? "" : end.getIndex()) + "}";
return str + "], end=" + Optional.ofNullable(end).map(BasicBlock::getIndex).orElse(-1) + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import org.jd.core.v1.model.javasyntax.declaration.AnnotationDeclaration;
import org.jd.core.v1.model.javasyntax.reference.BaseAnnotationReference;

import java.util.Optional;

public class ClassFileAnnotationDeclaration extends AnnotationDeclaration implements ClassFileTypeDeclaration {
private final int firstLineNumber;

public ClassFileAnnotationDeclaration(BaseAnnotationReference annotationReferences, int flags, String internalName, String name, ClassFileBodyDeclaration bodyDeclaration) {
super(annotationReferences, flags, internalName, name, null, bodyDeclaration);
this.firstLineNumber = bodyDeclaration==null ? 0 : bodyDeclaration.getFirstLineNumber();
this.firstLineNumber = Optional.ofNullable(bodyDeclaration).map(ClassFileMemberDeclaration::getFirstLineNumber).orElse(0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
import org.jd.core.v1.model.javasyntax.type.BaseTypeParameter;
import org.jd.core.v1.model.javasyntax.type.ObjectType;

import java.util.Optional;

public class ClassFileClassDeclaration extends ClassDeclaration implements ClassFileTypeDeclaration {
private final int firstLineNumber;

public ClassFileClassDeclaration(BaseAnnotationReference annotationReferences, int flags, String internalName, String name, BaseTypeParameter typeParameters, ObjectType superType, BaseType interfaces, ClassFileBodyDeclaration bodyDeclaration) {
super(annotationReferences, flags, internalName, name, typeParameters, superType, interfaces, bodyDeclaration);
this.firstLineNumber = bodyDeclaration==null ? 0 : bodyDeclaration.getFirstLineNumber();
this.firstLineNumber = Optional.ofNullable(bodyDeclaration).map(ClassFileMemberDeclaration::getFirstLineNumber).orElse(0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
import org.jd.core.v1.model.javasyntax.type.BaseType;

import java.util.List;
import java.util.Optional;

public class ClassFileEnumDeclaration extends EnumDeclaration implements ClassFileTypeDeclaration {
private final int firstLineNumber;

public ClassFileEnumDeclaration(BaseAnnotationReference annotationReferences, int flags, String internalName, String name, BaseType interfaces, ClassFileBodyDeclaration bodyDeclaration) {
super(annotationReferences, flags, internalName, name, interfaces, null, bodyDeclaration);
this.firstLineNumber = bodyDeclaration==null ? 0 : bodyDeclaration.getFirstLineNumber();
this.firstLineNumber = Optional.ofNullable(bodyDeclaration).map(ClassFileMemberDeclaration::getFirstLineNumber).orElse(0);
}

public void setFlags(int flags) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
import org.jd.core.v1.model.javasyntax.type.BaseType;
import org.jd.core.v1.model.javasyntax.type.BaseTypeParameter;

import java.util.Optional;

public class ClassFileInterfaceDeclaration extends InterfaceDeclaration implements ClassFileTypeDeclaration {
private final int firstLineNumber;

public ClassFileInterfaceDeclaration(BaseAnnotationReference annotationReferences, int flags, String internalName, String name, BaseTypeParameter typeParameters, BaseType interfaces, ClassFileBodyDeclaration bodyDeclaration) {
super(annotationReferences, flags, internalName, name, typeParameters, interfaces, bodyDeclaration);
this.firstLineNumber = bodyDeclaration==null ? 0 : bodyDeclaration.getFirstLineNumber();
this.firstLineNumber = Optional.ofNullable(bodyDeclaration).map(ClassFileMemberDeclaration::getFirstLineNumber).orElse(0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,7 @@ private void parseInvokeDynamic(Statements statements, DefaultStack<Expression>
ConstantNameAndType indyCnat = constants.getConstant(constantMemberRef.getNameAndTypeIndex());
String indyMethodName = constants.getConstantString(indyCnat.getNameIndex(), CONSTANT_Utf8);
String indyDescriptor = constants.getConstantString(indyCnat.getSignatureIndex(), CONSTANT_Utf8);
TypeMaker.MethodTypes indyMethodTypes = typeMaker.makeMethodTypes(indyDescriptor);
TypeMaker.MethodTypes indyMethodTypes = typeMaker.makeMethodTypes(internalTypeName, indyMethodName, indyDescriptor);

BaseExpression indyParameters = extractParametersFromStack(statements, stack, indyMethodTypes.getParameterTypes());
BootstrapMethod bootstrapMethod = attributeBootstrapMethods.getBootstrapMethods()[constantMemberRef.getClassIndex()];
Expand All @@ -1428,7 +1428,7 @@ private void parseInvokeDynamic(Statements statements, DefaultStack<Expression>

ConstantMethodType cmt0 = constants.getConstant(bootstrapArguments[0]);
String descriptor0 = constants.getConstantString(cmt0.getDescriptorIndex(), CONSTANT_Utf8);
TypeMaker.MethodTypes methodTypes0 = typeMaker.makeMethodTypes(descriptor0);
TypeMaker.MethodTypes methodTypes0 = typeMaker.makeMethodTypes(internalTypeName, null, descriptor0);
int parameterCount = methodTypes0.getParameterTypes() == null ? 0 : methodTypes0.getParameterTypes().size();
ConstantMethodHandle constantMethodHandle1 = constants.getConstant(bootstrapArguments[1]);
ConstantCP cmr1 = constants.getConstant(constantMethodHandle1.getReferenceIndex());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.bcel.classfile.LineNumberTable;
import org.apache.bcel.classfile.LocalVariable;
import org.apache.bcel.classfile.LocalVariableTable;
import org.apache.bcel.classfile.LocalVariableTypeTable;
import org.apache.bcel.classfile.Method;
import org.jd.core.v1.model.javasyntax.expression.BooleanExpression;
import org.jd.core.v1.model.javasyntax.expression.StringConstantExpression;
Expand All @@ -34,7 +35,9 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.TreeMap;
import java.util.stream.Stream;

import static org.apache.bcel.Const.ALOAD;
import static org.apache.bcel.Const.ANEWARRAY;
Expand Down Expand Up @@ -522,21 +525,22 @@ protected void writeLocalVariableTable(String linePrefix, StringBuilder sb, Code
}
}

// LocalVariableTypeTable localVariableTypeTable = attributeCode.getLocalVariableTypeTable();
//
// if (localVariableTypeTable != null) {
// sb.append(linePrefix).append("Local variable type table:\n");
// sb.append(linePrefix).append(" start\tlength\tslot\tname\tsignature\n");
//
// for (LocalVariable localVariable : localVariableTypeTable.getLocalVariableTypeTable()) {
// sb.append(linePrefix).append(" ");
// sb.append(localVariable.getStartPC()).append('\t');
// sb.append(localVariable.getLength()).append('\t');
// sb.append(localVariable.getIndex()).append('\t');
// sb.append(localVariable.getName()).append('\t');
// sb.append(localVariable.getSignature()).append('\n');
// }
// }
LocalVariableTypeTable localVariableTypeTable = (LocalVariableTypeTable) Optional.ofNullable(attributeCode.getAttributes())
.map(Stream::of).orElseGet(Stream::empty).filter(LocalVariableTypeTable.class::isInstance).findAny().orElse(null);

if (localVariableTypeTable != null) {
sb.append(linePrefix).append("Local variable type table:\n");
sb.append(linePrefix).append(" start\tlength\tslot\tname\tsignature\n");

for (LocalVariable localVariable : localVariableTypeTable.getLocalVariableTypeTable()) {
sb.append(linePrefix).append(" ");
sb.append(localVariable.getStartPC()).append('\t');
sb.append(localVariable.getLength()).append('\t');
sb.append(localVariable.getIndex()).append('\t');
sb.append(localVariable.getName()).append('\t');
sb.append(localVariable.getSignature()).append('\n');
}
}
}

protected void writeExceptionTable(String linePrefix, StringBuilder sb, ConstantPool constants, Code attributeCode) {
Expand Down

0 comments on commit 2830dcc

Please sign in to comment.