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 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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,171 @@ 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
BuildStep withMethods(@Nonnull Collection<SootMethod> overriddenSootMethods);
}

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

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

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

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

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

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

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

/** Defines a {@link OverridingClassSource} builder. */
public static class OverridingClassSourceBuilder
implements MethodsStep,
FieldsStep,
ModifiersStep,
InterfacesStep,
SuperclassStep,
OuterClassStep,
PositionStep,
BuildStep {
@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 BuildStep withMethods(@Nonnull final Collection<SootMethod> overriddenSootMethods) {
this.overriddenSootMethods = overriddenSootMethods;
return this;
}

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

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

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

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

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

@Override
@Nonnull
public BuildStep withPosition(@Nullable final 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
BuildStep withClassSource(@Nonnull SootClassSource classSource);
}

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

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

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

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

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

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

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

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