Skip to content

Commit

Permalink
refactored to switch expressions and added utilities for next jd-core
Browse files Browse the repository at this point in the history
  • Loading branch information
nbauma109 committed Aug 26, 2022
1 parent 12a6337 commit 902c6e1
Show file tree
Hide file tree
Showing 47 changed files with 572 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,13 @@ public static int nextInstructionOffset(byte[] code, int index)
{
final int opcode = code[index] & 255;

switch (opcode)
return switch (opcode)
{
case Const.TABLESWITCH:
return nextTableSwitchOffset(code, index);

case Const.LOOKUPSWITCH:
return nextLookupSwitchOffset(code, index);

case Const.WIDE:
return nextWideOffset(code, index);

default:
return index + 1 + Const.getNoOfOperands(opcode);
}
case Const.TABLESWITCH -> nextTableSwitchOffset(code, index);
case Const.LOOKUPSWITCH -> nextLookupSwitchOffset(code, index);
case Const.WIDE -> nextWideOffset(code, index);
default -> index + 1 + Const.getNoOfOperands(opcode);
};
}

public static boolean jumpTo(byte[] code, int offset, int targetOffset) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

package org.jd.core.v1.model.classfile;

public class Constants {
public final class Constants {

private Constants() {
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/jd/core/v1/model/classfile/Method.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import java.util.Map;

import static org.apache.bcel.Const.ACC_SYNTHETIC;

public class Method {
private final int accessFlags;
private final String name;
Expand Down Expand Up @@ -53,6 +55,14 @@ public ConstantPool getConstants() {
return constants;
}

public String getKey() {
return name + descriptor;
}

public boolean isLambda() {
return (accessFlags & ACC_SYNTHETIC) != 0 && name.contains("lambda$");
}

@Override
public String toString() {
return String.join(".", className, name + descriptor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public class BootstrapMethod {
protected int bootstrapMethodRef;
protected int bootstrapArguments[];
protected int[] bootstrapArguments;

public BootstrapMethod(int bootstrapMethodRef, int[] bootstrapArguments) {
this.bootstrapMethodRef = bootstrapMethodRef;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @See https://docs.oracle.com/javase/specs/jvms/se16/html/jvms-4.html
*/
public abstract class Constant {
public class Constant {
public static final byte CONSTANT_UNKNOWN = 0;
public static final byte CONSTANT_UTF8 = 1;
public static final byte CONSTANT_INTEGER = 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.jd.core.v1.model.javasyntax.expression.ParenthesesExpression;
import org.jd.core.v1.model.javasyntax.expression.PostOperatorExpression;
import org.jd.core.v1.model.javasyntax.expression.PreOperatorExpression;
import org.jd.core.v1.model.javasyntax.expression.QualifiedSuperExpression;
import org.jd.core.v1.model.javasyntax.expression.StringConstantExpression;
import org.jd.core.v1.model.javasyntax.expression.SuperConstructorInvocationExpression;
import org.jd.core.v1.model.javasyntax.expression.SuperExpression;
Expand Down Expand Up @@ -121,6 +122,7 @@
import java.util.List;

public abstract class AbstractJavaSyntaxVisitor extends AbstractTypeArgumentVisitor implements DeclarationVisitor, ExpressionVisitor, ReferenceVisitor, StatementVisitor, TypeVisitor, TypeParameterVisitor {

public void visit(CompilationUnit compilationUnit) {
compilationUnit.typeDeclarations().accept(this);
}
Expand Down Expand Up @@ -484,6 +486,13 @@ public void visit(SuperExpression expression) {
type.accept(this);
}

@Override
public void visit(QualifiedSuperExpression expression) {
BaseType type = expression.getType();

type.accept(this);
}

@Override
public void visit(TernaryOperatorExpression expression) {
expression.getCondition().accept(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public class BodyDeclaration implements Declaration {
private final String internalTypeName;
protected BaseMemberDeclaration memberDeclarations;
private boolean anonymous;

public BodyDeclaration(String internalTypeName, BaseMemberDeclaration memberDeclarations) {
this.internalTypeName = internalTypeName;
Expand All @@ -24,6 +25,14 @@ public BaseMemberDeclaration getMemberDeclarations() {
return memberDeclarations;
}

public boolean isAnonymous() {
return anonymous;
}

public void setAnonymous(boolean anonymous) {
this.anonymous = anonymous;
}

@Override
public void accept(DeclarationVisitor visitor) {
visitor.visit(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.jd.core.v1.model.javasyntax.type.BaseTypeParameter;
import org.jd.core.v1.model.javasyntax.type.Type;

import static org.apache.bcel.Const.ACC_ABSTRACT;
import static org.apache.bcel.Const.ACC_PUBLIC;
import static org.apache.bcel.Const.ACC_STATIC;

public class MethodDeclaration implements MemberDeclaration {
Expand Down Expand Up @@ -62,6 +64,8 @@ public int getFlags() {
}

public boolean isStatic() { return (flags & ACC_STATIC) != 0; }
public boolean isPublic() { return (flags & ACC_PUBLIC) != 0; }
public boolean isAbstract() { return (flags & ACC_ABSTRACT) != 0; }

public String getName() {
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
import org.jd.core.v1.model.javasyntax.type.Type;

public abstract class AbstractLineNumberTypeExpression extends AbstractLineNumberExpression {
protected Type type;
private Type type;

protected AbstractLineNumberTypeExpression(Type type) {
this.type = type;
setType(type);
}

protected AbstractLineNumberTypeExpression(int lineNumber, Type type) {
super(lineNumber);
this.type = type;
setType(type);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public abstract class AbstractNopExpressionVisitor implements ExpressionVisitor
@Override public void visit(ParenthesesExpression expression) {}
@Override public void visit(PostOperatorExpression expression) {}
@Override public void visit(PreOperatorExpression expression) {}
@Override public void visit(QualifiedSuperExpression expression) {}
@Override public void visit(StringConstantExpression expression) {}
@Override public void visit(SuperConstructorInvocationExpression expression) {}
@Override public void visit(SuperExpression expression) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public interface BaseExpression extends Base<Expression> {
default boolean isLocalVariableReferenceExpression() { return false; }
default boolean isLongConstantExpression() { return false; }
default boolean isMethodInvocationExpression() { return false; }
default boolean isNew() { return isNewArray() || isNewInitializedArray() || isNewExpression(); }
default boolean isNewArray() { return false; }
default boolean isNewExpression() { return false; }
default boolean isNewInitializedArray() { return false; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ public String toString() {

@Override
public Expression copyTo(int lineNumber) {
return new BinaryOperatorExpression(lineNumber, type, leftExpression, operator, rightExpression, priority);
return new BinaryOperatorExpression(lineNumber, getType(), leftExpression, operator, rightExpression, priority);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,27 @@
public class CastExpression extends AbstractLineNumberTypeExpression {
private Expression expression;
private boolean explicit;
private boolean byteCodeCheckCast;

public CastExpression(Type type, Expression expression) {
super(type);
this.expression = expression;
this.explicit = true;
this(UNKNOWN_LINE_NUMBER, type, expression);
}

public CastExpression(int lineNumber, Type type, Expression expression) {
super(lineNumber, type);
this.expression = expression;
this.explicit = true;
this(lineNumber, type, expression, true);
}

public CastExpression(int lineNumber, Type type, Expression expression, boolean explicit) {
this(lineNumber, type, expression, explicit, false);
}

public CastExpression(int lineNumber, Type type, Expression expression, boolean explicit, boolean byteCodeCheckCast) {
super(lineNumber, type);
this.expression = expression;
this.explicit = explicit;
this.byteCodeCheckCast = byteCodeCheckCast;
}

@Override
public Expression getExpression() {
return expression;
Expand All @@ -48,6 +50,14 @@ public void setExplicit(boolean explicit) {
this.explicit = explicit;
}

public boolean isByteCodeCheckCast() {
return byteCodeCheckCast;
}

public void setByteCodeCheckCast(boolean byteCodeCheckCast) {
this.byteCodeCheckCast = byteCodeCheckCast;
}

@Override
public int getPriority() {
return 3;
Expand All @@ -63,11 +73,11 @@ public void accept(ExpressionVisitor visitor) {

@Override
public String toString() {
return "CastExpression{cast (" + type + ") " + expression + "}";
return "CastExpression{cast (" + getType() + ") " + expression + "}";
}

@Override
public Expression copyTo(int lineNumber) {
return new CastExpression(lineNumber, type, expression, explicit);
return new CastExpression(lineNumber, getType(), expression, explicit, byteCodeCheckCast);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@

public class ConstructorInvocationExpression extends ConstructorReferenceExpression {
private BaseExpression parameters;
private final boolean varArgs;

public ConstructorInvocationExpression(int lineNumber, ObjectType type, String descriptor, BaseExpression parameters) {
public ConstructorInvocationExpression(int lineNumber, ObjectType type, String descriptor, BaseExpression parameters, boolean varArgs) {
super(lineNumber, PrimitiveType.TYPE_VOID, type, descriptor);
this.parameters = parameters;
this.varArgs = varArgs;
}

public boolean isVarArgs() {
return varArgs;
}

@Override
public BaseExpression getParameters() {
return parameters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public void accept(ExpressionVisitor visitor) {

@Override
public Expression copyTo(int lineNumber) {
return new ConstructorReferenceExpression(lineNumber, type, objectType, descriptor);
return new ConstructorReferenceExpression(lineNumber, getType(), objectType, descriptor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public interface ExpressionVisitor {
void visit(ParenthesesExpression expression);
void visit(PostOperatorExpression expression);
void visit(PreOperatorExpression expression);
void visit(QualifiedSuperExpression expression);
void visit(StringConstantExpression expression);
void visit(SuperConstructorInvocationExpression expression);
void visit(SuperExpression expression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ public void accept(ExpressionVisitor visitor) {

@Override
public String toString() {
return "FieldReferenceExpression{type=" + type + ", expression=" + expression + ", name=" + name + ", descriptor=" + descriptor + "}";
return "FieldReferenceExpression{type=" + getType() + ", expression=" + expression + ", name=" + name + ", descriptor=" + descriptor + "}";
}

@Override
public Expression copyTo(int lineNumber) {
return new FieldReferenceExpression(lineNumber, type, expression, internalTypeName, name, descriptor);
return new FieldReferenceExpression(lineNumber, getType(), expression, internalTypeName, name, descriptor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ public void accept(ExpressionVisitor visitor) {

@Override
public String toString() {
return "IntegerConstantExpression{type=" + type + ", value=" + value + "}";
return "IntegerConstantExpression{type=" + getType() + ", value=" + value + "}";
}

@Override
public Expression copyTo(int lineNumber) {
return new IntegerConstantExpression(lineNumber, type, value);
return new IntegerConstantExpression(lineNumber, getType(), value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public String toString() {

@Override
public Expression copyTo(int lineNumber) {
return new LambdaFormalParametersExpression(lineNumber, type, formalParameters, statements);
return new LambdaFormalParametersExpression(lineNumber, getType(), formalParameters, statements);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ public String toString() {

@Override
public Expression copyTo(int lineNumber) {
return new LambdaIdentifiersExpression(lineNumber, type, returnedType, parameterNames, statements);
return new LambdaIdentifiersExpression(lineNumber, getType(), returnedType, parameterNames, statements);
}
}
Loading

0 comments on commit 902c6e1

Please sign in to comment.