package com.bumptech.glide.supportapp;

import java.lang.reflect.*;
import java.util.List;

import android.content.Context;

import com.bumptech.glide.*;
import com.bumptech.glide.module.GlideModule;

public class GlideReset extends BaseGlideReset {
	private static final Field GLIDE_FIELD;
	private static final Constructor<GlideBuilder> BUILDER_CONSTRUCTOR;
	private static final Method CREATEGLIDE_METHOD;

	static {
		try {
			GLIDE_FIELD = Glide.class.getDeclaredField("glide");
			GLIDE_FIELD.setAccessible(true);
			BUILDER_CONSTRUCTOR = GlideBuilder.class.getDeclaredConstructor(Context.class);
			BUILDER_CONSTRUCTOR.setAccessible(true);
			CREATEGLIDE_METHOD = GlideBuilder.class.getDeclaredMethod("createGlide");
			CREATEGLIDE_METHOD.setAccessible(true);
		} catch (Exception ex) {
			throw new ExceptionInInitializerError(ex);
		}
	}

	public GlideReset(Context context) {
		super(context);
	}

	@Override protected GlideBuilder newBuilder() {
		try {
			return BUILDER_CONSTRUCTOR.newInstance(applicationContext);
		} catch (Exception ex) {
			throw new IllegalStateException(ex);
		}
	}

	@Override protected Glide createGlide(GlideBuilder builder) {
		try {
			return (Glide)CREATEGLIDE_METHOD.invoke(builder);
		} catch (Exception ex) {
			throw new IllegalStateException(ex);
		}
	}

	@Override protected void applyOptions(List<GlideModule> modules, GlideBuilder builder) {
		for (GlideModule module : modules) {
			module.applyOptions(applicationContext, builder);
		}
	}

	@Override protected void registerComponents(List<GlideModule> modules, Glide glide) {
		for (GlideModule module : modules) {
			module.registerComponents(applicationContext, glide.getRegistry());
		}
	}

	@Override protected void doTearDown() {
		try {
			GLIDE_FIELD.set(null, null);
		} catch (Exception ex) {
			throw new IllegalStateException(ex);
		}
	}

	@Override protected boolean isSetup() {
		try {
			return GLIDE_FIELD.get(null) != null;
		} catch (Exception ex) {
			throw new IllegalStateException(ex);
		}
	}
}

@@ -58,17 +58,21 @@
 reflect.
-*
+Field
 ;%0Aimport
@@ -292,1014 +292,448 @@
 LD;%0A
-%09private static final Constructor%3CGlideBuilder%3E BUILDER_CONSTRUCTOR;%0A%09private static final Method CREATEGLIDE_METHOD;%0A%0A%09static %7B%0A%09%09try %7B%0A%09%09%09GLIDE_FIELD = Glide.class.getDeclaredField(%22glide%22);%0A%09%09%09GLIDE_FIELD.setAccessible(true);%0A%09%09%09BUILDER_CONSTRUCTOR = GlideBuilder.class.getDeclaredConstructor(Context.class);%0A%09%09%09BUILDER_CONSTRUCTOR.setAccessible(true);%0A%09%09%09CREATEGLIDE_METHOD = GlideBuilder.class.getDeclaredMethod(%22createGlide%22);%0A%09%09%09CREATEGLIDE_METHOD.setAccessible(true);%0A%09%09%7D catch (Exception ex) %7B%0A%09%09%09throw new ExceptionInInitializerError(ex);%0A%09%09%7D%0A%09%7D%0A%0A%09public GlideReset(Context context) %7B%0A%09%09super(context);%0A%09%7D%0A%0A%09@Override protected GlideBuilder newBuilder() %7B%0A%09%09try %7B%0A%09%09%09return BUILDER_CONSTRUCTOR.newInstance(applicationContext);%0A%09%09%7D catch (Exception ex) %7B%0A%09%09%09throw new IllegalStateException(ex);%0A%09%09%7D%0A%09%7D%0A%0A%09@Override protected Glide createGlide(GlideBuilder builder) %7B%0A%09%09try %7B%0A%09%09%09return (Glide)CREATEGLIDE_METHOD.invoke(builder);%0A%09%09%7D catch (Exception ex) %7B%0A%09%09%09throw new IllegalStateException(ex);%0A%09%09%7D
+%0A%09static %7B%0A%09%09try %7B%0A%09%09%09GLIDE_FIELD = Glide.class.getDeclaredField(%22glide%22);%0A%09%09%09GLIDE_FIELD.setAccessible(true);%0A%09%09%7D catch (Exception ex) %7B%0A%09%09%09throw new ExceptionInInitializerError(ex);%0A%09%09%7D%0A%09%7D%0A%0A%09public GlideReset(Context context) %7B%0A%09%09super(context);%0A%09%7D%0A%0A%09@Override protected GlideBuilder newBuilder() %7B%0A%09%09return new GlideBuilder();%0A%09%7D%0A%0A%09@Override protected Glide createGlide(GlideBuilder builder) %7B%0A%09%09return builder.build(applicationContext);
 %0A%09%7D%0A

Fix broken Glide v4 hacks