public static String isLocalType(Class<?> type) { /* As per [JACKSON-187], GAE seems to throw SecurityExceptions * here and there... and GAE itself has a bug, too * (see []). Bah. */ try { // one more: method locals, anonymous, are not good: if (type.getEnclosingMethod() != null) { return "local/anonymous"; } /* But how about non-static inner classes? Can't construct * easily (theoretically, we could try to check if parent * happens to be enclosing... but that gets convoluted) */ if (type.getEnclosingClass() != null) { if (!Modifier.isStatic(type.getModifiers())) { return "non-static member class"; } } } catch (SecurityException e) { } catch (NullPointerException e) { } return null; }
@Test public void testEnclosingMethod() throws Exception { for (Class<?> type : standardTypes) { Matcher<MethodDescription> matcher; if (type.getEnclosingMethod() != null) { matcher = CoreMatchers.<MethodDescription>is( new MethodDescription.ForLoadedMethod(type.getEnclosingMethod())); } else if (type.getEnclosingConstructor() != null) { matcher = CoreMatchers.<MethodDescription>is( new MethodDescription.ForLoadedConstructor(type.getEnclosingConstructor())); } else { matcher = nullValue(MethodDescription.class); } assertThat(describe(type).getEnclosingMethod(), matcher); } }