Java Code Examples for java.lang.reflect.Member#getName()

The following examples show how to use java.lang.reflect.Member#getName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ValueConstraintsInstance.java    From attic-polygene-java with Apache License 2.0 6 votes vote down vote up
public void checkConstraints( Object value, AccessibleObject accessor )
{
    List<ValueConstraintViolation> violations = checkConstraints( value );
    if( !violations.isEmpty() )
    {
        for( ValueConstraintViolation violation : violations )
        {
            if( accessor instanceof Member )
            {
                Member member = (Member) accessor;
                String methodName =  member.getName();
                violation.setMixinType( member.getDeclaringClass() );
                violation.setMethodName( methodName );
            }

        }
        throw new ConstraintViolationException( violations );
    }
}
 
Example 2
Source File: AbstractJavaLinker.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Given a reflective method or a constructor, creates a dynamic method that represents it. This method will
 * distinguish between caller sensitive and ordinary methods/constructors, and create appropriate caller sensitive
 * dynamic method when needed.
 * @param m the reflective member
 * @return the single dynamic method representing the reflective member
 */
private static SingleDynamicMethod createDynamicMethod(final AccessibleObject m) {
    if(CallerSensitiveDetector.isCallerSensitive(m)) {
        // Method has @CallerSensitive annotation
        return new CallerSensitiveDynamicMethod(m);
    }
    // Method has no @CallerSensitive annotation
    final MethodHandle mh;
    try {
        mh = unreflectSafely(m);
    } catch (final IllegalAccessError e) {
        // java.lang.invoke can in some case conservatively treat as caller sensitive methods that aren't
        // marked with the annotation. In this case, we'll fall back to treating it as caller sensitive.
        return new CallerSensitiveDynamicMethod(m);
    }
    // Proceed with non-caller sensitive
    final Member member = (Member)m;
    return new SimpleDynamicMethod(mh, member.getDeclaringClass(), member.getName(), m instanceof Constructor);
}
 
Example 3
Source File: AbstractJavaLinker.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Given a reflective method or a constructor, creates a dynamic method that represents it. This method will
 * distinguish between caller sensitive and ordinary methods/constructors, and create appropriate caller sensitive
 * dynamic method when needed.
 * @param m the reflective member
 * @return the single dynamic method representing the reflective member
 */
private static SingleDynamicMethod createDynamicMethod(final AccessibleObject m) {
    if (m.isAnnotationPresent(CallerSensitive.class)) {
        // Method has @CallerSensitive annotation
        return new CallerSensitiveDynamicMethod(m);
    }
    // Method has no @CallerSensitive annotation
    final MethodHandle mh;
    try {
        mh = unreflectSafely(m);
    } catch (final IllegalAccessError e) {
        // java.lang.invoke can in some case conservatively treat as caller sensitive methods that aren't
        // marked with the annotation. In this case, we'll fall back to treating it as caller sensitive.
        return new CallerSensitiveDynamicMethod(m);
    }
    // Proceed with non-caller sensitive
    final Member member = (Member)m;
    return new SimpleDynamicMethod(mh, member.getDeclaringClass(), member.getName(), m instanceof Constructor);
}
 
Example 4
Source File: ClassReader.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected static String getSignature(Member method, Class<?>[] paramTypes) {
    // compute the method descriptor

    StringBuilder b = new StringBuilder((method instanceof Method) ? method.getName() : "<init>");
    b.append('(');

    for (int i = 0; i < paramTypes.length; i++) {
        addDescriptor(b, paramTypes[i]);
    }

    b.append(')');
    if (method instanceof Method) {
        addDescriptor(b, ((Method)method).getReturnType());
    } else if (method instanceof Constructor) {
        addDescriptor(b, void.class);
    }

    return b.toString();
}
 
Example 5
Source File: JavaBeanNaming.java    From immutables with Apache License 2.0 6 votes vote down vote up
private static String maybeNameFromGetterSetter(Member member) {
  if (!(member instanceof Method)) {
    return member.getName();
  }

  Method method = (Method) member;
  String name = member.getName();
  if (IS_SETTER.test(method)) {
    return DECAPITALIZE.apply(name.substring("set".length()));
  }

  if (IS_GETTER.test(method)) {
    for (String prefix: Arrays.asList("is", "get")) {
      if (name.startsWith(prefix) && name.length() > prefix.length()) {
        return DECAPITALIZE.apply(name.substring(prefix.length()));
      }
    }
  }

  return name;
}
 
Example 6
Source File: AbstractJavaLinker.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Given a reflective method or a constructor, creates a dynamic method that represents it. This method will
 * distinguish between caller sensitive and ordinary methods/constructors, and create appropriate caller sensitive
 * dynamic method when needed.
 * @param m the reflective member
 * @return the single dynamic method representing the reflective member
 */
private static SingleDynamicMethod createDynamicMethod(final AccessibleObject m) {
    if(CallerSensitiveDetector.isCallerSensitive(m)) {
        // Method has @CallerSensitive annotation
        return new CallerSensitiveDynamicMethod(m);
    }
    // Method has no @CallerSensitive annotation
    final MethodHandle mh;
    try {
        mh = unreflectSafely(m);
    } catch (final IllegalAccessError e) {
        // java.lang.invoke can in some case conservatively treat as caller sensitive methods that aren't
        // marked with the annotation. In this case, we'll fall back to treating it as caller sensitive.
        return new CallerSensitiveDynamicMethod(m);
    }
    // Proceed with non-caller sensitive
    final Member member = (Member)m;
    return new SimpleDynamicMethod(mh, member.getDeclaringClass(), member.getName(), m instanceof Constructor);
}
 
Example 7
Source File: AbstractJavaLinker.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Given a reflective method or a constructor, creates a dynamic method that represents it. This method will
 * distinguish between caller sensitive and ordinary methods/constructors, and create appropriate caller sensitive
 * dynamic method when needed.
 * @param m the reflective member
 * @return the single dynamic method representing the reflective member
 */
private static SingleDynamicMethod createDynamicMethod(final AccessibleObject m) {
    if(CallerSensitiveDetector.isCallerSensitive(m)) {
        // Method has @CallerSensitive annotation
        return new CallerSensitiveDynamicMethod(m);
    }
    // Method has no @CallerSensitive annotation
    final MethodHandle mh;
    try {
        mh = unreflectSafely(m);
    } catch (final IllegalAccessError e) {
        // java.lang.invoke can in some case conservatively treat as caller sensitive methods that aren't
        // marked with the annotation. In this case, we'll fall back to treating it as caller sensitive.
        return new CallerSensitiveDynamicMethod(m);
    }
    // Proceed with non-caller sensitive
    final Member member = (Member)m;
    return new SimpleDynamicMethod(mh, member.getDeclaringClass(), member.getName(), m instanceof Constructor);
}
 
Example 8
Source File: MoreTypes.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Formats a member as concise string, such as {@code java.util.ArrayList.size},
 * {@code java.util.ArrayList<init>()} or {@code java.util.List.remove()}.
 */
public static String toString(Member member) {
    Class<? extends Member> memberType = memberType(member);

    if (memberType == Method.class) {
        return member.getDeclaringClass().getName() + "." + member.getName() + "()";
    } else if (memberType == Field.class) {
        return member.getDeclaringClass().getName() + "." + member.getName();
    } else if (memberType == Constructor.class) {
        return member.getDeclaringClass().getName() + ".<init>()";
    } else {
        throw new AssertionError();
    }
}
 
Example 9
Source File: StackTraceElements.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static Object forMember(Member member) {
    if (member == null) {
        return SourceProvider.UNKNOWN_SOURCE;
    }

    Class declaringClass = member.getDeclaringClass();

    String fileName = null;
    int lineNumber = -1;

    Class<? extends Member> memberType = MoreTypes.memberType(member);
    String memberName = memberType == Constructor.class ? "<init>" : member.getName();
    return new StackTraceElement(declaringClass.getName(), memberName, fileName, lineNumber);
}
 
Example 10
Source File: MoreTypes.java    From crate with Apache License 2.0 5 votes vote down vote up
private MemberImpl(Member member) {
    this.declaringClass = member.getDeclaringClass();
    this.name = member.getName();
    this.modifiers = member.getModifiers();
    this.synthetic = member.isSynthetic();
    this.memberType = memberType(member);
    this.memberKey = memberKey(member);
}
 
Example 11
Source File: AbstractJavaLinker.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given a reflective method or a constructor, creates a dynamic method that represents it. This method will
 * distinguish between caller sensitive and ordinary methods/constructors, and create appropriate caller sensitive
 * dynamic method when needed.
 * @param m the reflective member
 * @return the single dynamic method representing the reflective member
 */
private static SingleDynamicMethod createDynamicMethod(AccessibleObject m) {
    if(CallerSensitiveDetector.isCallerSensitive(m)) {
        return new CallerSensitiveDynamicMethod(m);
    }
    final Member member = (Member)m;
    return new SimpleDynamicMethod(unreflectSafely(m), member.getDeclaringClass(), member.getName());
}
 
Example 12
Source File: MatchFieldPathArgumentExtractor.java    From hollow with Apache License 2.0 5 votes vote down vote up
private static String getFieldPath(Member m, AnnotatedElement e) {
    FieldPath fpa = e.getDeclaredAnnotation(FieldPath.class);
    if (fpa == null) {
        return m.getName();
    }

    String fieldPath = e.getDeclaredAnnotation(FieldPath.class).value();
    if (fieldPath.isEmpty()) {
        return m.getName();
    }

    return fieldPath;
}
 
Example 13
Source File: AbstractJavaLinker.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given a reflective method or a constructor, creates a dynamic method that represents it. This method will
 * distinguish between caller sensitive and ordinary methods/constructors, and create appropriate caller sensitive
 * dynamic method when needed.
 * @param m the reflective member
 * @return the single dynamic method representing the reflective member
 */
private static SingleDynamicMethod createDynamicMethod(AccessibleObject m) {
    if(CallerSensitiveDetector.isCallerSensitive(m)) {
        return new CallerSensitiveDynamicMethod(m);
    }
    final Member member = (Member)m;
    return new SimpleDynamicMethod(unreflectSafely(m), member.getDeclaringClass(), member.getName());
}
 
Example 14
Source File: Reflection.java    From lyra with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a simplified String representation of the {@code member}.
 */
public static String toString(Member member) {
  if (member instanceof Method)
    return member.getDeclaringClass().getSimpleName() + "." + member.getName() + "()";
  return null;
}
 
Example 15
Source File: Members.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static void error(Member member, String description) {
    throw new IllegalArgumentException(member.getDeclaringClass().getName() + "#" + member.getName() + " " + description);
}
 
Example 16
Source File: ClassData.java    From reflectutils with Apache License 2.0 4 votes vote down vote up
public int compare(Member o1, Member o2) {
    String c1 = getModifierPrefix(o1.getModifiers()) + o1.getName();
    String c2 = getModifierPrefix(o2.getModifiers()) + o2.getName();
    return c1.compareTo(c2);
}
 
Example 17
Source File: AttributeFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public String determineMemberDescription(Member member) {
	return member.getDeclaringClass().getName() + '#' + member.getName();
}
 
Example 18
Source File: TypeMappingException.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
public static TypeMappingException ambiguousMemberType(Member fieldOrMethod, AnnotatedType declaringType, Exception cause) {
    return new TypeMappingException("The type of member \"" + fieldOrMethod.getName() + "\" belonging to " + declaringType.getType().getTypeName() +
            " is missing generic type parameters and can not be mapped." +
            " For details and possible solutions see " + Urls.Errors.AMBIGUOUS_MEMBER_TYPE, cause);
}
 
Example 19
Source File: MetricResolver.java    From metrics-cdi with Apache License 2.0 4 votes vote down vote up
private String memberName(Member member) {
    if (member instanceof Constructor)
        return member.getDeclaringClass().getSimpleName();
    else
        return member.getName();
}
 
Example 20
Source File: RepositorySetup.java    From immutables with Apache License 2.0 4 votes vote down vote up
@Override
public String translateName(Member member) {
  return member.getName();
}