Skip to content

Commit

Permalink
Fixed the method caller inference - ignoring all Rapidoid classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Apr 11, 2016
1 parent 2434e7e commit 4c4f7c7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 42 deletions.
65 changes: 24 additions & 41 deletions rapidoid-commons/src/main/java/org/rapidoid/util/UTILS.java
Expand Up @@ -29,7 +29,6 @@
import org.rapidoid.commons.Arr;
import org.rapidoid.commons.English;
import org.rapidoid.commons.Str;
import org.rapidoid.config.Conf;
import org.rapidoid.ctx.Ctx;
import org.rapidoid.ctx.Ctxs;
import org.rapidoid.insight.Insights;
Expand Down Expand Up @@ -424,26 +423,11 @@ protected void loop() {
return thread;
}

public static boolean isRapidoidType(Class<?> clazz) {
try {
for (Class<?> c = clazz; c != Object.class; c = c.getSuperclass()) {
String pkg = c.getCanonicalName();
if (pkg.startsWith("org.rapidoid.") || pkg.startsWith("org.rapidoidx.")) {
return !pkg.startsWith("org.rapidoid.docs.");
}
}
} catch (Exception e) {
throw U.rte(e);
}

return false;
}

public static Class<?> getCallingClassOf(Class<?>... ignoreClasses) {
public static Class<?> getCallingClass(Class<?>... ignoreClasses) {
return inferCaller(ignoreClasses);
}

public static String getCallingPackageOf(Class<?>... ignoreClasses) {
public static String getCallingPackage(Class<?>... ignoreClasses) {
Class<?> callerCls = inferCaller(ignoreClasses);

if (callerCls != null) {
Expand All @@ -453,7 +437,7 @@ public static String getCallingPackageOf(Class<?>... ignoreClasses) {
}
}

public static Class<?> getCallingMainClass() {
private static Class<?> inferCaller(Class<?>... ignoreClasses) {
StackTraceElement[] trace = Thread.currentThread().getStackTrace();

// skip the first 2 elements:
Expand All @@ -462,41 +446,40 @@ public static Class<?> getCallingMainClass() {

for (int i = 2; i < trace.length; i++) {
String cls = trace[i].getClassName();

if (U.eq(trace[i].getMethodName(), "main")) {
Class<?> clazz = Cls.getClassIfExists(cls);
if (clazz != null) {
Method main = Cls.findMethod(clazz, "main", String[].class);
if (main != null && main.getReturnType() == void.class
&& !main.isVarArgs() && main.getDeclaringClass().equals(clazz)) {
int modif = main.getModifiers();
if (Modifier.isStatic(modif) && Modifier.isPublic(modif)) {
return clazz;
}
}
if (!Cls.isRapidoidClass(cls) && !Cls.isJREClass(cls) && !shouldIgnore(cls, ignoreClasses)) {
try {
return Class.forName(cls);
} catch (ClassNotFoundException e) {
Log.error("Couldn't load the caller class!", e);
return null;
}
}
}

return null;
}

private static Class<?> inferCaller(Class<?>... ignoreClasses) {
public static Class<?> getCallingMainClass() {
StackTraceElement[] trace = Thread.currentThread().getStackTrace();

// skip the first 3 elements:
// skip the first 2 elements:
// [0] java.lang.Thread.getStackTrace
// [1] THIS METHOD
// [2] UTILS#getCallingPackageOf or UTILS#getCallingClassOf

for (int i = 3; i < trace.length; i++) {
for (int i = 2; i < trace.length; i++) {
String cls = trace[i].getClassName();
if (!shouldIgnore(cls, ignoreClasses)) {
try {
return Class.forName(cls);
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;

if (!Cls.isRapidoidClass(cls) && !Cls.isJREClass(cls) && U.eq(trace[i].getMethodName(), "main")) {
Class<?> clazz = Cls.getClassIfExists(cls);
if (clazz != null) {
Method main = Cls.findMethod(clazz, "main", String[].class);
if (main != null && main.getReturnType() == void.class
&& !main.isVarArgs() && main.getDeclaringClass().equals(clazz)) {
int modif = main.getModifiers();
if (Modifier.isStatic(modif) && Modifier.isPublic(modif)) {
return clazz;
}
}
}
}
}
Expand Down
Expand Up @@ -365,7 +365,7 @@ public synchronized String[] path() {

private static void inferCallers() {
if (!restarted && appPkgName == null && mainClassName == null) {
String pkg = UTILS.getCallingPackageOf(Setup.class, On.class, Setup.class);
String pkg = UTILS.getCallingPackage();

appPkgName = pkg;

Expand Down

0 comments on commit 4c4f7c7

Please sign in to comment.