java.lang.Class.getEnclosingConstructor()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(9.2k)|赞(0)|评价(0)|浏览(113)

本文整理了Java中java.lang.Class.getEnclosingConstructor()方法的一些代码示例,展示了Class.getEnclosingConstructor()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Class.getEnclosingConstructor()方法的具体详情如下:
包路径:java.lang.Class
类名称:Class
方法名:getEnclosingConstructor

Class.getEnclosingConstructor介绍

[英]Returns the enclosing Constructor of this Class, if it is an anonymous or local/automatic class; otherwise null.
[中]如果该类是匿名类或本地/自动类,则返回该类的封闭构造函数;否则为空。

代码示例

代码示例来源:origin: spring-projects/spring-loaded

public static Constructor callGetEnclosingConstructor(Class thiz)
{
  return thiz.getEnclosingConstructor();
}

代码示例来源:origin: google/guava

private boolean mayNeedHiddenThis() {
  Class<?> declaringClass = constructor.getDeclaringClass();
  if (declaringClass.getEnclosingConstructor() != null) {
   // Enclosed in a constructor, needs hidden this
   return true;
  }
  Method enclosingMethod = declaringClass.getEnclosingMethod();
  if (enclosingMethod != null) {
   // Enclosed in a method, if it's not static, must need hidden this.
   return !Modifier.isStatic(enclosingMethod.getModifiers());
  } else {
   // Strictly, this doesn't necessarily indicate a hidden 'this' in the case of
   // static initializer. But there seems no way to tell in that case. :(
   // This may cause issues when an anonymous class is created inside a static initializer,
   // and the class's constructor's first parameter happens to be the enclosing class.
   // In such case, we may mistakenly think that the class is within a non-static context
   // and the first parameter is the hidden 'this'.
   return declaringClass.getEnclosingClass() != null
     && !Modifier.isStatic(declaringClass.getModifiers());
  }
 }
}

代码示例来源:origin: prestodb/presto

private boolean mayNeedHiddenThis() {
  Class<?> declaringClass = constructor.getDeclaringClass();
  if (declaringClass.getEnclosingConstructor() != null) {
   // Enclosed in a constructor, needs hidden this
   return true;
  }
  Method enclosingMethod = declaringClass.getEnclosingMethod();
  if (enclosingMethod != null) {
   // Enclosed in a method, if it's not static, must need hidden this.
   return !Modifier.isStatic(enclosingMethod.getModifiers());
  } else {
   // Strictly, this doesn't necessarily indicate a hidden 'this' in the case of
   // static initializer. But there seems no way to tell in that case. :(
   // This may cause issues when an anonymous class is created inside a static initializer,
   // and the class's constructor's first parameter happens to be the enclosing class.
   // In such case, we may mistakenly think that the class is within a non-static context
   // and the first parameter is the hidden 'this'.
   return declaringClass.getEnclosingClass() != null
     && !Modifier.isStatic(declaringClass.getModifiers());
  }
 }
}

代码示例来源:origin: robovm/robovm

/**
 * Tests whether the class represented by this {@code Class} is defined
 * locally.
 */
public boolean isLocalClass() {
  boolean enclosed = (getEnclosingMethod() != null ||
           getEnclosingConstructor() != null);
  return enclosed && !isAnonymousClass();
}

代码示例来源:origin: wildfly/wildfly

private boolean mayNeedHiddenThis() {
  Class<?> declaringClass = constructor.getDeclaringClass();
  if (declaringClass.getEnclosingConstructor() != null) {
   // Enclosed in a constructor, needs hidden this
   return true;
  }
  Method enclosingMethod = declaringClass.getEnclosingMethod();
  if (enclosingMethod != null) {
   // Enclosed in a method, if it's not static, must need hidden this.
   return !Modifier.isStatic(enclosingMethod.getModifiers());
  } else {
   // Strictly, this doesn't necessarily indicate a hidden 'this' in the case of
   // static initializer. But there seems no way to tell in that case. :(
   // This may cause issues when an anonymous class is created inside a static initializer,
   // and the class's constructor's first parameter happens to be the enclosing class.
   // In such case, we may mistakenly think that the class is within a non-static context
   // and the first parameter is the hidden 'this'.
   return declaringClass.getEnclosingClass() != null
     && !Modifier.isStatic(declaringClass.getModifiers());
  }
 }
}

代码示例来源:origin: robovm/robovm

private static GenericDeclaration nextLayer(GenericDeclaration decl) {
  if (decl instanceof Class) {
    // FIXME: Is the following hierarchy correct?:
    Class cl = (Class)decl;
    // RoboVM note: Start of change. Do what the old 4.1.1 code did instead
    // of AnnotationAccess.getEnclosingMethodOrConstructor(cl) in 4.4.3.
    decl = cl.getEnclosingMethod();
    if (decl != null) {
      return decl;
    }
    decl = cl.getEnclosingConstructor();
    if (decl != null) {
      return decl;
    }
    // RoboVM note: End of change,
    return cl.getEnclosingClass();
  } else if (decl instanceof Method) {
    return ((Method)decl).getDeclaringClass();
  } else if (decl instanceof Constructor) {
    return ((Constructor)decl).getDeclaringClass();
  } else {
    throw new AssertionError();
  }
}

代码示例来源:origin: google/j2objc

private boolean mayNeedHiddenThis() {
  Class<?> declaringClass = constructor.getDeclaringClass();
  if (declaringClass.getEnclosingConstructor() != null) {
   // Enclosed in a constructor, needs hidden this
   return true;
  }
  Method enclosingMethod = declaringClass.getEnclosingMethod();
  if (enclosingMethod != null) {
   // Enclosed in a method, if it's not static, must need hidden this.
   return !Modifier.isStatic(enclosingMethod.getModifiers());
  } else {
   // Strictly, this doesn't necessarily indicate a hidden 'this' in the case of
   // static initializer. But there seems no way to tell in that case. :(
   // This may cause issues when an anonymous class is created inside a static initializer,
   // and the class's constructor's first parameter happens to be the enclosing class.
   // In such case, we may mistakenly think that the class is within a non-static context
   // and the first parameter is the hidden 'this'.
   return declaringClass.getEnclosingClass() != null
     && !Modifier.isStatic(declaringClass.getModifiers());
  }
 }
}

代码示例来源:origin: redisson/redisson

/**
 * {@inheritDoc}
 */
public MethodDescription.InDefinedShape getEnclosingMethod() {
  Method enclosingMethod = type.getEnclosingMethod();
  Constructor<?> enclosingConstructor = type.getEnclosingConstructor();
  if (enclosingMethod != null) {
    return new MethodDescription.ForLoadedMethod(enclosingMethod);
  } else if (enclosingConstructor != null) {
    return new MethodDescription.ForLoadedConstructor(enclosingConstructor);
  } else {
    return MethodDescription.UNDEFINED;
  }
}

代码示例来源:origin: com.bugvm/bugvm-rt

/**
 * Tests whether the class represented by this {@code Class} is defined
 * locally.
 */
public boolean isLocalClass() {
  boolean enclosed = (getEnclosingMethod() != null ||
           getEnclosingConstructor() != null);
  return enclosed && !isAnonymousClass();
}

代码示例来源:origin: MobiVM/robovm

/**
 * Tests whether the class represented by this {@code Class} is defined
 * locally.
 */
public boolean isLocalClass() {
  boolean enclosed = (getEnclosingMethod() != null ||
           getEnclosingConstructor() != null);
  return enclosed && !isAnonymousClass();
}

代码示例来源:origin: com.mobidevelop.robovm/robovm-rt

/**
 * Tests whether the class represented by this {@code Class} is defined
 * locally.
 */
public boolean isLocalClass() {
  boolean enclosed = (getEnclosingMethod() != null ||
           getEnclosingConstructor() != null);
  return enclosed && !isAnonymousClass();
}

代码示例来源:origin: org.ceylon-lang/com.redhat.ceylon.model

private boolean isStaticLocalContainer(Class<?> klass) {
  Constructor<?> enclosingConstructor = klass.getEnclosingConstructor();
  if(enclosingConstructor != null)
    return Modifier.isStatic(enclosingConstructor.getModifiers());
  Method enclosingMethod = klass.getEnclosingMethod();
  return Modifier.isStatic(enclosingMethod.getModifiers());
}

代码示例来源:origin: ibinti/bugvm

/**
 * Tests whether the class represented by this {@code Class} is defined
 * locally.
 */
public boolean isLocalClass() {
  boolean enclosed = (getEnclosingMethod() != null ||
           getEnclosingConstructor() != null);
  return enclosed && !isAnonymousClass();
}

代码示例来源:origin: com.gluonhq/robovm-rt

/**
 * Tests whether the class represented by this {@code Class} is defined
 * locally.
 */
public boolean isLocalClass() {
  boolean enclosed = (getEnclosingMethod() != null ||
           getEnclosingConstructor() != null);
  return enclosed && !isAnonymousClass();
}

代码示例来源:origin: org.testifyproject.external/external-bytebuddy

@Override
public MethodDescription getEnclosingMethod() {
  Method enclosingMethod = type.getEnclosingMethod();
  Constructor<?> enclosingConstructor = type.getEnclosingConstructor();
  if (enclosingMethod != null) {
    return new MethodDescription.ForLoadedMethod(enclosingMethod);
  } else if (enclosingConstructor != null) {
    return new MethodDescription.ForLoadedConstructor(enclosingConstructor);
  } else {
    return MethodDescription.UNDEFINED;
  }
}

代码示例来源:origin: javapathfinder/jpf-core

@Test
public void getEnclosingConstructor () throws SecurityException, NoSuchMethodException{
 if (verifyNoPropertyViolation()){
  Class cls = (new ClassTest.TestEnclosedClass()).foo.getClass();
  assertTrue(cls.getEnclosingConstructor().getDeclaringClass() == ClassTest.TestEnclosedClass.class);
  assertEquals(cls.getEnclosingConstructor().getName(), "<init>");
  assertNull(cls.getEnclosingMethod());
 }
}

代码示例来源:origin: org.jruby/jruby-complete

@JRubyMethod
public IRubyObject enclosing_constructor() {
  Constructor<?> ctor = javaClass().getEnclosingConstructor();
  if (ctor != null) {
    return new JavaConstructor(getRuntime(), ctor);
  }
  return getRuntime().getNil();
}

代码示例来源:origin: org.netbeans.api/org-jruby

@JRubyMethod
public IRubyObject enclosing_constructor() {
  Constructor<?> ctor = javaClass().getEnclosingConstructor();
  if (ctor != null) {
    return new JavaConstructor(getRuntime(), ctor);
  }
  return getRuntime().getNil();
}

代码示例来源:origin: org.kill-bill.billing/killbill-osgi-bundles-jruby

@JRubyMethod
public IRubyObject enclosing_constructor() {
  Constructor<?> ctor = javaClass().getEnclosingConstructor();
  if (ctor != null) {
    return new JavaConstructor(getRuntime(), ctor);
  }
  return getRuntime().getNil();
}

代码示例来源:origin: org.jruby/jruby-core

@JRubyMethod
public IRubyObject enclosing_constructor() {
  Constructor<?> ctor = javaClass().getEnclosingConstructor();
  if (ctor != null) {
    return new JavaConstructor(getRuntime(), ctor);
  }
  return getRuntime().getNil();
}

相关文章

微信公众号

最新文章

更多

Class类方法