Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create SootClass builder #1069

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,172 @@ public OverridingClassSource withPosition(@Nullable Position position) {
position,
delegate);
}

/**
* Creates a builder for {@link OverridingClassSource}.
*
* @return a {@link OverridingClassSourceBuilder}
*/
@Nonnull
public static MethodsStep builder() {
return new OverridingClassSourceBuilder();
}

public interface MethodsStep {
@Nonnull
FieldsStep withMethods(@Nonnull Collection<SootMethod> overriddenSootMethods);
}

public interface FieldsStep {
@Nonnull
ModifiersStep withFields(@Nonnull Collection<SootField> overriddenSootFields);
}

public interface ModifiersStep {
@Nonnull
InterfacesStep withModifiers(@Nonnull Set<ClassModifier> overriddenModifiers);
}

public interface InterfacesStep {
@Nonnull
SuperclassStep withInterfaces(@Nonnull Set<ClassType> overriddenInterfaces);
}

public interface SuperclassStep {
@Nonnull
OuterClassStep withSuperclass(@Nonnull Optional<ClassType> overriddenSuperclass);
}

public interface OuterClassStep {
@Nonnull
PositionStep withOuterClass(@Nonnull Optional<ClassType> overriddenOuterClass);
}

public interface PositionStep {
@Nonnull
Build withPosition(@Nullable Position position);
}

public interface Build {
@Nonnull
OverridingClassSource build();
}

/** Defines a {@link OverridingClassSource} builder. */
public static class OverridingClassSourceBuilder
implements MethodsStep,
FieldsStep,
ModifiersStep,
InterfacesStep,
SuperclassStep,
OuterClassStep,
PositionStep,
Build {
@Nullable private Collection<SootMethod> overriddenSootMethods;
@Nullable private Collection<SootField> overriddenSootFields;
@Nullable private Set<ClassModifier> overriddenModifiers;
@Nullable private Set<ClassType> overriddenInterfaces;
@Nullable private Optional<ClassType> overriddenSuperclass;
@Nullable private Optional<ClassType> overriddenOuterClass;
@Nullable private Position position;

@Nullable
public Collection<SootMethod> getMethods() {
return overriddenSootMethods;
}

@Nullable
public Collection<SootField> getFields() {
return overriddenSootFields;
}

@Nullable
public Set<ClassModifier> getModifiers() {
return overriddenModifiers;
}

@Nullable
public Set<ClassType> getInterfaces() {
return overriddenInterfaces;
}

@Nullable
public Optional<ClassType> getSuperclass() {
return overriddenSuperclass;
}

@Nullable
public Optional<ClassType> getOuterClass() {
return overriddenOuterClass;
}

@Nullable
public Position getPosition() {
return position;
}

@Override
@Nonnull
public FieldsStep withMethods(@Nonnull Collection<SootMethod> overriddenSootMethods) {
this.overriddenSootMethods = overriddenSootMethods;
return this;
}

@Override
@Nonnull
public ModifiersStep withFields(@Nonnull Collection<SootField> overriddenSootFields) {
this.overriddenSootFields = overriddenSootFields;
return this;
}

@Override
@Nonnull
public InterfacesStep withModifiers(@Nonnull Set<ClassModifier> overriddenModifiers) {
this.overriddenModifiers = overriddenModifiers;
return this;
}

@Override
@Nonnull
public SuperclassStep withInterfaces(@Nonnull Set<ClassType> overriddenInterfaces) {
this.overriddenInterfaces = overriddenInterfaces;
return this;
}

@Override
@Nonnull
public OuterClassStep withSuperclass(@Nonnull Optional<ClassType> overriddenSuperclass) {
this.overriddenSuperclass = overriddenSuperclass;
return this;
}

@Override
@Nonnull
public PositionStep withOuterClass(@Nonnull Optional<ClassType> overriddenOuterClass) {
this.overriddenOuterClass = overriddenOuterClass;
return this;
}

@Override
@Nonnull
public Build withPosition(@Nullable Position position) {
this.position = position;
return this;
}

@Override
@Nonnull
public OverridingClassSource build() {
return new OverridingClassSource(
getMethods(),
getFields(),
getModifiers(),
getInterfaces(),
getSuperclass(),
getOuterClass(),
getPosition(),
null
);
}
}
}
66 changes: 66 additions & 0 deletions sootup.core/src/main/java/sootup/core/model/SootClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.util.Set;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import sootup.core.frontend.ResolveException;
import sootup.core.frontend.SootClassSource;
import sootup.core.types.ClassType;
Expand Down Expand Up @@ -291,4 +293,68 @@ public SootClass withClassSource(@Nonnull SootClassSource classSource) {
public SootClass withSourceType(@Nonnull SourceType sourceType) {
return new SootClass(classSource, sourceType);
}

/**
* Creates a builder for {@link SootClass}.
* @return a {@link SootClassBuilder}
*/
@Nonnull
public static ClassSourceStep builder() {
return new SootClassBuilder();
}

public interface ClassSourceStep {
@Nonnull
SourceTypeStep withClassSource(@Nonnull SootClassSource classSource);
}

public interface SourceTypeStep {
@Nonnull
Build withSourceType(@Nonnull SourceType sourceType);
}

public interface Build {
@Nonnull
SootClass build();
}

/**
* Defines a {@link SootClass} builder.
*/
public static class SootClassBuilder implements ClassSourceStep, SourceTypeStep, Build {
@Nullable
private SootClassSource classSource;
@Nullable
private SourceType sourceType;

@Nullable
public SootClassSource getClassSource() {
return classSource;
}

@Nullable
public SourceType getSourceType() {
return sourceType;
}

@Override
@Nonnull
public SourceTypeStep withClassSource(@Nonnull SootClassSource classSource) {
this.classSource = classSource;
return this;
}

@Override
@Nonnull
public Build withSourceType(@Nonnull SourceType sourceType) {
this.sourceType = sourceType;
return this;
}

@Override
@Nonnull
public SootClass build() {
return new SootClass(getClassSource(), getSourceType());
}
}
}