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

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

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

Class.toString介绍

[英]Converts the object to a string. The string representation is the string "class" or "interface", followed by a space, and then by the fully qualified name of the class in the format returned by getName. If this Class object represents a primitive type, this method returns the name of the primitive type. If this Class object represents void this method returns "void".
[中]

代码示例

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

private boolean matchesClassCastMessage(String classCastMessage, Class<?> eventClass) {
  // On Java 8, the message starts with the class name: "java.lang.String cannot be cast..."
  if (classCastMessage.startsWith(eventClass.getName())) {
    return true;
  }
  // On Java 11, the message starts with "class ..." a.k.a. Class.toString()
  if (classCastMessage.startsWith(eventClass.toString())) {
    return true;
  }
  // On Java 9, the message used to contain the module name: "java.base/java.lang.String cannot be cast..."
  int moduleSeparatorIndex = classCastMessage.indexOf('/');
  if (moduleSeparatorIndex != -1 && classCastMessage.startsWith(eventClass.getName(), moduleSeparatorIndex + 1)) {
    return true;
  }
  // Assuming an unrelated class cast failure...
  return false;
}

代码示例来源:origin: bumptech/glide

@SuppressWarnings("PMD.PreserveStackTrace")
private static void appendExceptionMessage(Throwable t, Appendable appendable) {
 try {
  appendable.append(t.getClass().toString()).append(": ").append(t.getMessage()).append('\n');
 } catch (IOException e1) {
  throw new RuntimeException(t);
 }
}

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

private Map<String, Map<String, String>> map( Class<? extends PropertyContainer> cls )
{
  if ( cls.equals( Node.class ) )
  {
    return nodeConfig;
  }
  else if ( cls.equals( Relationship.class ) )
  {
    return relConfig;
  }
  throw new IllegalArgumentException( cls.toString() );
}

代码示例来源:origin: apache/flink

private String fieldTypesToString() {
  StringBuilder string = new StringBuilder();
  string.append(this.fieldTypes[0].toString());
  for (int i = 1; i < this.fieldTypes.length; i++) {
    string.append(", ").append(this.fieldTypes[i]);
  }
  
  return string.toString();
}

代码示例来源:origin: org.testng/testng

/**
 * @return A hashcode representing the name of this method and its parameters,
 * but without its class
 */
private static String createMethodKey(Method m) {
 StringBuilder result = new StringBuilder(m.getName());
 for (Class paramClass : m.getParameterTypes()) {
  result.append(' ').append(paramClass.toString());
 }
 return result.toString();
}

代码示例来源:origin: hankcs/HanLP

private static Object createValue(Class<?> type, String valueAsString) throws NoSuchMethodException
{
  for (ValueCreator valueCreator : valueCreators)
  {
    Object createdValue = valueCreator.createValue(type, valueAsString);
    if (createdValue != null)
    {
      return createdValue;
    }
  }
  throw new IllegalArgumentException(String.format("cannot instanciate any %s object using %s value", type.toString(), valueAsString));
}

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

/**
 * {@inheritDoc}
 */
public Annotation resolve() {
  throw new IncompatibleClassChangeError("Not an annotation type: " + incompatibleType.toString());
}

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

/**
 * {@inheritDoc}
 */
public Enum<?> resolve() {
  throw new IncompatibleClassChangeError("Not an enumeration type: " + type.toString());
}

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

public static MockitoException cannotMockClass(Class<?> clazz, String reason) {
  return new MockitoException(join(
      "Cannot mock/spy " + clazz.toString(),
      "Mockito cannot mock/spy because :",
      " - " + reason
  ));
}

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

@Override
public final T getNullValue(DeserializationContext ctxt) throws JsonMappingException {
  // 01-Mar-2017, tatu: Alas, not all paths lead to `_coerceNull()`, as `SettableBeanProperty`
  //    short-circuits `null` handling. Hence need this check as well.
  if (_primitive && ctxt.isEnabled(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)) {
    ctxt.reportInputMismatch(this,
        "Cannot map `null` into type %s (set DeserializationConfig.DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES to 'false' to allow)",
        handledType().toString());
  }
  return _nullValue;
}

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

@Override
public final T getNullValue(DeserializationContext ctxt) throws JsonMappingException {
  // 01-Mar-2017, tatu: Alas, not all paths lead to `_coerceNull()`, as `SettableBeanProperty`
  //    short-circuits `null` handling. Hence need this check as well.
  if (_primitive && ctxt.isEnabled(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)) {
    ctxt.reportInputMismatch(this,
        "Cannot map `null` into type %s (set DeserializationConfig.DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES to 'false' to allow)",
        handledType().toString());
  }
  return _nullValue;
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void just() {
  Flowable<Integer> source = Flowable.fromArray(new Integer[] { 1 });
  Assert.assertTrue(source.getClass().toString(), source instanceof ScalarCallable);
}

代码示例来源:origin: ReactiveX/RxJava

@SuppressWarnings("unchecked")
  @Test
  public void scalarCallable() {
    Maybe<Integer> m = Maybe.just(1);

    assertTrue(m.getClass().toString(), m instanceof ScalarCallable);

    assertEquals(1, ((ScalarCallable<Integer>)m).call().intValue());
  }
}

代码示例来源:origin: ReactiveX/RxJava

@SuppressWarnings("unchecked")
@Test
public void callable() throws Exception {
  final int[] counter = { 0 };
  Maybe<Void> m = Maybe.fromAction(new Action() {
    @Override
    public void run() throws Exception {
      counter[0]++;
    }
  });
  assertTrue(m.getClass().toString(), m instanceof Callable);
  assertNull(((Callable<Void>)m).call());
  assertEquals(1, counter[0]);
}

代码示例来源:origin: ReactiveX/RxJava

@SuppressWarnings("unchecked")
@Test
public void callable() throws Exception {
  final int[] counter = { 0 };
  Maybe<Void> m = Maybe.fromRunnable(new Runnable() {
    @Override
    public void run() {
      counter[0]++;
    }
  });
  assertTrue(m.getClass().toString(), m instanceof Callable);
  assertNull(((Callable<Void>)m).call());
  assertEquals(1, counter[0]);
}

代码示例来源:origin: ReactiveX/RxJava

@SuppressWarnings("unchecked")
@Test
public void callable() throws Exception {
  final int[] counter = { 0 };
  Maybe<Integer> m = Maybe.fromCallable(new Callable<Integer>() {
    @Override
    public Integer call() throws Exception {
      counter[0]++;
      return 0;
    }
  });
  assertTrue(m.getClass().toString(), m instanceof Callable);
  assertEquals(0, ((Callable<Void>)m).call());
  assertEquals(1, counter[0]);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
  public void scalarCallable() {
    Maybe<Integer> m = Maybe.empty();

    assertTrue(m.getClass().toString(), m instanceof ScalarCallable);

    assertNull(((ScalarCallable<?>)m).call());
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void source() {
  Maybe<Integer> m = Maybe.just(1);
  Single<Integer> s = m.toSingle();
  assertTrue(s.getClass().toString(), s instanceof HasUpstreamMaybeSource);
  assertSame(m, (((HasUpstreamMaybeSource<?>)s).source()));
}

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

@Test
public void processHeadersToSend() {
  Map<String, Object> map = this.messagingTemplate.processHeadersToSend(null);
  assertNotNull(map);
  assertTrue("Actual: " + map.getClass().toString(), MessageHeaders.class.isAssignableFrom(map.getClass()));
  SimpMessageHeaderAccessor headerAccessor =
      MessageHeaderAccessor.getAccessor((MessageHeaders) map, SimpMessageHeaderAccessor.class);
  assertTrue(headerAccessor.isMutable());
  assertEquals(SimpMessageType.MESSAGE, headerAccessor.getMessageType());
}

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

@Test
public void setPropertyWithCustomEditor() {
  MutablePropertyValues values = new MutablePropertyValues();
  values.add("name", Integer.class);
  TestBean target = new TestBean();
  AbstractPropertyAccessor accessor = createAccessor(target);
  accessor.registerCustomEditor(String.class, new PropertyEditorSupport() {
    @Override
    public void setValue(Object value) {
      super.setValue(value.toString());
    }
  });
  accessor.setPropertyValues(values);
  assertEquals(Integer.class.toString(), target.getName());
}

相关文章

Class类方法