Skip to content

Commit

Permalink
Minor cleanup of Type and Member datastructures.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 363691930
  • Loading branch information
Dino Oliva authored and copybara-github committed Mar 18, 2021
1 parent 89e775a commit 9ddf8ab
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,44 @@

final class Member {
static Member buildFrom(MemberInfo memberInfo, Type declaringType) {
Member member = new Member();
member.memberInfo = memberInfo;
member.declaringType = declaringType;
member.isStatic = memberInfo.getStatic();
member.isConstructor = memberInfo.getConstructor();
return member;
return new Member(
memberInfo.getName(),
declaringType,
memberInfo.getStatic(),
memberInfo.getConstructor(),
memberInfo.getExported());
}

private MemberInfo memberInfo;
private Type declaringType;
private boolean isStatic;
private boolean isConstructor;
private final String name;
private final Type declaringType;
private final boolean isStatic;
private final boolean isConstructor;
private final boolean isExported;

private boolean fullyTraversed;
private boolean live;
private final List<Type> referencedTypes = new ArrayList<>();
private final List<Member> referencedMembers = new ArrayList<>();

private Member() {}
private Member(String name, Type declaringType, boolean isStatic, boolean isConstructor,
boolean isExported) {
this.name = name;
this.declaringType = declaringType;
this.isStatic = isStatic;
this.isConstructor = isConstructor;
this.isExported = isExported;
}

Type getDeclaringType() {
return declaringType;
}

boolean isExported() {
return memberInfo.getExported();
return isExported;
}

String getName() {
return memberInfo.getName();
return name;
}

public boolean isConstructor() {
Expand Down
72 changes: 35 additions & 37 deletions tree_shaker/src/main/java/com/google/devtools/treeshaker/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,48 @@
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

final class Type {
private String name;
static Type buildFrom(TypeInfo typeInfo, String name) {
return new Type(name, typeInfo.getExported(), typeInfo.getMemberList());
}

private final String name;
private final boolean isExported;
private final Map<String, Member> membersByName = new LinkedHashMap<>();

private Type superClass;
private final List<Type> superInterfaces = new ArrayList<>();
private final List<Type> immediateSubtypes = new ArrayList<>();
private final LinkedHashMap<String, Member> membersByName = new LinkedHashMap<>();
private boolean live;
private boolean instantiated;
private boolean isExported;
private final List<Member> potentiallyLiveMembers = new ArrayList<>();

static Type buildFrom(TypeInfo typeInfo, String name) {
Type type = new Type();
type.name = name;
type.isExported = typeInfo.getExported();
typeInfo
.getMemberList()
.forEach(memberInfo -> type.addMember(Member.buildFrom(memberInfo, type)));

return type;
private Type(String name, boolean isExported, Collection<MemberInfo> members) {
this.name = name;
this.isExported = isExported;
members.forEach(memberInfo -> {
Member member = Member.buildFrom(memberInfo, this);
Member previous = membersByName.put(member.getName(), member);
checkState(previous == null);
});
}

private Type() {}
String getName() {
return name;
}

Collection<Member> getMembers() {
return membersByName.values();
boolean isExported() {
return isExported;
}

Member getMemberByName(String name) {
return membersByName.get(name);
}

void addMember(Member member) {
Member previous = membersByName.put(member.getName(), member);
checkState(previous == null);
}

String getName() {
return name;
Collection<Member> getMembers() {
return membersByName.values();
}

Type getSuperClass() {
Expand All @@ -69,14 +71,22 @@ void setSuperClass(Type superClass) {
this.superClass = superClass;
}

List<Type> getSuperInterfaces() {
Collection<Type> getSuperInterfaces() {
return superInterfaces;
}

void addSuperInterface(Type superInterface) {
this.superInterfaces.add(superInterface);
}

Collection<Type> getImmediateSubtypes() {
return immediateSubtypes;
}

void addImmediateSubtype(Type type) {
immediateSubtypes.add(type);
}

void markLive() {
this.live = true;
}
Expand All @@ -93,25 +103,13 @@ void instantiate() {
this.instantiated = true;
}

/** Returns the list of members that need to mark as live when the type becomes live. */
List<Member> getPotentiallyLiveMembers() {
// Returns the list of members that need to mark as live when the type becomes live.
Collection<Member> getPotentiallyLiveMembers() {
return potentiallyLiveMembers;
}

void addPotentiallyLiveMember(Member member) {
checkState(!isInstantiated());
potentiallyLiveMembers.add(member);
}

public void addImmediateSubtype(Type type) {
immediateSubtypes.add(type);
}

public List<Type> getImmediateSubtypes() {
return immediateSubtypes;
}

boolean isExported() {
return isExported;
}
}

0 comments on commit 9ddf8ab

Please sign in to comment.