Skip to content

Commit

Permalink
Code optimization & removal. (#26, close #29)
Browse files Browse the repository at this point in the history
  • Loading branch information
WeAthFolD committed Jun 14, 2016
1 parent 28d590c commit 0d3b811
Show file tree
Hide file tree
Showing 43 changed files with 180 additions and 2,733 deletions.
49 changes: 0 additions & 49 deletions src/main/java/cn/lambdalib/annoreg/asm/InnerClassVisitor.java

This file was deleted.

108 changes: 41 additions & 67 deletions src/main/java/cn/lambdalib/annoreg/core/RegistrationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

import cn.lambdalib.annoreg.base.RegistrationEmpty;
import cn.lambdalib.core.LLModContainer;
import cn.lambdalib.core.Profiler;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import cpw.mods.fml.common.discovery.ASMDataTable.ASMData;

import java.lang.annotation.Annotation;
Expand All @@ -20,11 +19,12 @@
import java.util.*;

public class RegistrationManager {

public static final RegistrationManager INSTANCE = new RegistrationManager();

public final Profiler profiler = new Profiler();

private Set<String> unloadedClass = new HashSet<>();
private Set<Class<?>> loadedClass = new HashSet<>();

private Set<String> unloadedRegType;
private Map<Class<? extends Annotation>, RegistryType> regByClass = new HashMap<>();
Expand All @@ -33,76 +33,59 @@ public class RegistrationManager {
private Map<String, RegModInformation> modMap = new HashMap<>();
private Set<RegModInformation> mods = new HashSet<>();

private Multimap<String, String> innerClassList = HashMultimap.create();

public void annotationList(Set<String> data) {
unloadedClass.addAll(data);
}

private void loadClasses() {
profiler.begin("loadClasses");
loadRegistryTypes();
for (String name : unloadedClass) {
tryPrepareClass(name);
prepareClass(name);
}
unloadedClass.clear();
profiler.end("loadClasses");
}

private void tryPrepareClass(String name) {
private void prepareClass(String name) {
try {
prepareClass(Class.forName(name));
} catch (Throwable e) {
LLModContainer.log.fatal("Error on loading class {}.", name);
Throwables.propagate(e);
}
}

private void prepareClass(Class<?> clazz) {
//First check loadedClass to avoid infinite recursion (when extending the enclosing class).
if (loadedClass.contains(clazz)) {
return;
}
loadedClass.add(clazz);

//Class annotations
for (Annotation anno : clazz.getAnnotations()) {
Class<? extends Annotation> annoclazz = anno.annotationType();
if (regByClass.containsKey(annoclazz)) {
regByClass.get(annoclazz).visitClass(clazz);
}
}

//Field annotations
for (Field field : clazz.getDeclaredFields()) {
for (Annotation anno : field.getAnnotations()) {
Class<?> clazz = Class.forName(name);

//Class annotations
for (Annotation anno : clazz.getAnnotations()) {
Class<? extends Annotation> annoclazz = anno.annotationType();
if (regByClass.containsKey(annoclazz)) {
field.setAccessible(true);

regByClass.get(annoclazz).visitField(field);
regByClass.get(annoclazz).visitClass(clazz);
}
}
}

//Method annotations
for (Method method : clazz.getDeclaredMethods()) {
for (Annotation anno : method.getAnnotations()) {
Class<? extends Annotation> annoclazz = anno.annotationType();
if(regByClass.containsKey(annoclazz)) {
method.setAccessible(true);

regByClass.get(annoclazz).visitMethod(method);
//Field annotations
for (Field field : clazz.getDeclaredFields()) {
for (Annotation anno : field.getAnnotations()) {
Class<? extends Annotation> annoclazz = anno.annotationType();
if (regByClass.containsKey(annoclazz)) {
field.setAccessible(true);

regByClass.get(annoclazz).visitField(field);
}
}
}
}


//Inner classes
if (innerClassList.containsKey(clazz.getName())) {
for (String inner : innerClassList.get(clazz.getName())) {
tryPrepareClass(inner);

//Method annotations
for (Method method : clazz.getDeclaredMethods()) {
for (Annotation anno : method.getAnnotations()) {
Class<? extends Annotation> annoclazz = anno.annotationType();
if(regByClass.containsKey(annoclazz)) {
method.setAccessible(true);

regByClass.get(annoclazz).visitMethod(method);
}
}
}
} catch (Throwable e) {
LLModContainer.log.fatal("Error on loading class {}.", name);
Throwables.propagate(e);
}

}

RegModInformation findMod(AnnotationData data) {
Expand Down Expand Up @@ -158,9 +141,14 @@ private void registerAll(RegModInformation mod, String type) {
//First load all classes that have not been loaded.
loadClasses();

final String id = String.format("task %s:%s", mod.getModID(), type);
profiler.begin(id);

RegistryType rt = Preconditions.checkNotNull(regByName.get(type), "RegistryType " + type + " not found.");

rt.registerAll(mod);

profiler.end(id);
}

public void registerAll(Object mod, String type) {
Expand All @@ -184,16 +172,6 @@ private void loadRegistryTypes() {
unloadedRegType.clear();
}

public void addSideOnlyRegAnnotation(Set<ASMData> data) {
for (ASMData asm : data) {
try {
Class<?> clazz = Class.forName(asm.getClassName());
} catch (Exception e) {
LLModContainer.log.error("Error on adding registry annotation {}.", asm.getClassName());
}
}
}

public void checkLoadState() {
for (RegistryType rt : regByName.values()) {
rt.checkLoadState();
Expand All @@ -208,10 +186,6 @@ public void addDependencyFor(String type, String dep) {
regByName.get(type).addDependency(dep);
}

public void addInnerClassList(String outer, List<String> inner) {
innerClassList.putAll(outer, inner);
}

static {
for (LoadStage ls : LoadStage.values()) {
INSTANCE.addRegType(new RegistrationEmpty(ls.name));
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/cn/lambdalib/annoreg/core/RegistryType.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ public void visitMethod(Method method) {
}

public void registerAll(RegModInformation mod) {
if(LambdaLib.DEBUG) {
LLModContainer.log.info("Reg " + this.name);
}

//Dependencies.
for (String dep : dependencies) {
RegistrationManager.INSTANCE.registerAll(mod.getModInstance(), dep);
Expand Down
23 changes: 0 additions & 23 deletions src/main/java/cn/lambdalib/annoreg/mc/ForcePreloadTexture.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ public InitCallbackRegistration() {

@Override
protected void register(Method method, RegInitCallback value) throws Exception {
if (!Modifier.isPrivate(method.getModifiers())) {
LambdaLib.log.warn("Init method " + method + " is not private.");
}
method.invoke(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ public PostInitCallbackRegistration() {

@Override
protected void register(Method method, RegPostInitCallback value) throws Exception {
if (!Modifier.isPrivate(method.getModifiers())) {
LambdaLib.log.warn("PostInit method " + method + " is not private.");
}
method.invoke(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ public PreInitCallbackRegistration() {

@Override
protected void register(Method method, RegPreInitCallback value) throws Exception {
if (!Modifier.isPrivate(method.getModifiers())) {
LambdaLib.log.warn("PreInit method " + method + " is not private.");
}
method.invoke(null);
}

Expand Down
77 changes: 0 additions & 77 deletions src/main/java/cn/lambdalib/annoreg/mc/PreloadTexRegistration.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/cn/lambdalib/annoreg/mc/RegEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* Registers the class as a listener into either FML or Forge bus.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
@Target(ElementType.FIELD)
public @interface RegEventHandler {

public enum Bus {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/lambdalib/core/LLCorePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class LLCorePlugin implements IFMLLoadingPlugin {

@Override
public String[] getASMTransformerClass() {
return new String[] { "cn.lambdalib.core.asm.RegistryTransformer" };
return new String[0];
}

@Override
Expand Down
Loading

0 comments on commit 0d3b811

Please sign in to comment.