com.google.inject.spi.Message类的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(142)

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

Message介绍

[英]An error message and the context in which it occured. Messages are usually created internally by Guice and its extensions. Messages can be created explicitly in a module using com.google.inject.Binder#addError(Throwable) statements:

  1. try {
  2. bindPropertiesFromFile();
  3. } catch (IOException e) {
  4. addError(e);
  5. }

[中]错误消息及其发生的上下文。消息通常由Guice及其扩展在内部创建。可以使用com在模块中显式创建消息。谷歌。注射活页夹#添加错误(可丢弃)语句:

  1. try {
  2. bindPropertiesFromFile();
  3. } catch (IOException e) {
  4. addError(e);
  5. }

代码示例

代码示例来源:origin: com.google.inject/guice

  1. /** Prepends the list of sources to the given {@link Message} */
  2. static Message mergeSources(List<Object> sources, Message message) {
  3. List<Object> messageSources = message.getSources();
  4. // It is possible that the end of getSources() and the beginning of message.getSources() are
  5. // equivalent, in this case we should drop the repeated source when joining the lists. The
  6. // most likely scenario where this would happen is when a scoped binding throws an exception,
  7. // due to the fact that InternalFactoryToProviderAdapter applies the binding source when
  8. // merging errors.
  9. if (!sources.isEmpty()
  10. && !messageSources.isEmpty()
  11. && Objects.equal(messageSources.get(0), sources.get(sources.size() - 1))) {
  12. messageSources = messageSources.subList(1, messageSources.size());
  13. }
  14. return new Message(
  15. ImmutableList.builder().addAll(sources).addAll(messageSources).build(),
  16. message.getMessage(),
  17. message.getCause());
  18. }

代码示例来源:origin: com.google.inject/guice

  1. @Override
  2. public int compare(Message a, Message b) {
  3. return a.getSource().compareTo(b.getSource());
  4. }
  5. }.sortedCopy(root.errors);

代码示例来源:origin: Graylog2/graylog2-server

  1. protected void annotateInjectorExceptions(Collection<Message> messages) {
  2. for (Message message : messages) {
  3. //noinspection ThrowableResultOfMethodCallIgnored
  4. final Throwable rootCause = ExceptionUtils.getRootCause(message.getCause());
  5. if (rootCause instanceof NodeIdPersistenceException) {
  6. LOG.error(UI.wallString(
  7. "Unable to read or persist your NodeId file. This means your node id file (" + configuration.getNodeIdFile() + ") is not readable or writable by the current user. The following exception might give more information: " + message));
  8. System.exit(-1);
  9. } else if (rootCause instanceof AccessDeniedException) {
  10. LOG.error(UI.wallString("Unable to access file " + rootCause.getMessage()));
  11. System.exit(-2);
  12. } else {
  13. // other guice error, still print the raw messages
  14. // TODO this could potentially print duplicate messages depending on what a subclass does...
  15. LOG.error("Guice error (more detail on log level debug): {}", message.getMessage());
  16. if (rootCause != null) {
  17. LOG.debug("Stacktrace:", rootCause);
  18. }
  19. }
  20. }
  21. }

代码示例来源:origin: com.google.inject/guice

  1. fmt.format("Encountered circular dependency spanning several threads.");
  2. if (proxyCreationError != null) {
  3. fmt.format(" %s", proxyCreationError.getMessage());
  4. fmt.format("%s is holding locks the following singletons in the cycle:%n", lockedThread);
  5. for (Key<?> lockedKey : lockedKeys) {
  6. fmt.format("%s%n", Errors.convert(lockedKey));
  7. return new Message(Thread.currentThread(), sb.toString());

代码示例来源:origin: jclouds/legacy-jclouds

  1. public void testGetCauseCreation() {
  2. AuthorizationException aex = createMock(AuthorizationException.class);
  3. Message message = new Message(ImmutableList.of(), "test", aex);
  4. CreationException pex = new CreationException(ImmutableSet.of(message));
  5. assertEquals(getFirstThrowableOfType(pex, AuthorizationException.class), aex);
  6. }

代码示例来源:origin: jclouds/legacy-jclouds

  1. @Test(expectedExceptions = AuthorizationException.class)
  2. public void testPropagateCreationExceptionAuthorizationException() throws Throwable {
  3. Exception e = new AuthorizationException();
  4. propagateIfPossible(
  5. new CreationException(ImmutableSet.of(new Message(ImmutableList.of(), "Error in custom provider", e))),
  6. ImmutableSet.<TypeToken<? extends Throwable>> of());
  7. }

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

  1. @Test
  2. public void TestFormatError()
  3. {
  4. Problems problems = new Problems();
  5. problems.addError("message %d", "NaN");
  6. Message[] errors = problems.getErrors().toArray(new Message[] {});
  7. assertEquals(errors.length, 1);
  8. assertContainsAllOf(errors[0].toString(), "Error", "message %d", "NaN", "IllegalFormatConversionException");
  9. assertEquals(problems.getWarnings().size(), 0, "Found unexpected warnings in problem object");
  10. try {
  11. problems.throwIfHasErrors();
  12. fail("Expected exception from problems object");
  13. }
  14. catch (ConfigurationException e) {
  15. assertContains(e.getMessage(), "message %d [NaN]");
  16. }
  17. }

代码示例来源:origin: org.jclouds.api/chef

  1. static <T> T checkNotNull(T reference, String name) {
  2. if (reference != null) {
  3. return reference;
  4. }
  5. NullPointerException npe = new NullPointerException(name);
  6. throw new ConfigurationException(ImmutableSet.of(new Message(ImmutableList.of(), npe.toString(), npe)));
  7. }
  8. }

代码示例来源:origin: com.google.inject/guice

  1. /**
  2. * Throws a ConfigurationException with an NullPointerExceptions as the cause if the given
  3. * reference is {@code null}.
  4. */
  5. static <T> T checkNotNull(T reference, String name) {
  6. if (reference != null) {
  7. return reference;
  8. }
  9. NullPointerException npe = new NullPointerException(name);
  10. throw new ConfigurationException(ImmutableSet.of(new Message(npe.toString(), npe)));
  11. }

代码示例来源:origin: com.google.inject/guice

  1. @Override
  2. public void addError(Throwable t) {
  3. String message = "An exception was caught and reported. Message: " + t.getMessage();
  4. elements.add(new Message(ImmutableList.of((Object) getElementSource()), message, t));
  5. }

代码示例来源:origin: com.google.inject/guice

  1. /**
  2. * Throws a ConfigurationException with a formatted {@link Message} if this condition is {@code
  3. * false}.
  4. */
  5. static void checkConfiguration(boolean condition, String format, Object... args) {
  6. if (condition) {
  7. return;
  8. }
  9. throw new ConfigurationException(ImmutableSet.of(new Message(Errors.format(format, args))));
  10. }

代码示例来源:origin: com.teradata.airlift/bootstrap

  1. @Test
  2. public void testRequiresExplicitBindings()
  3. throws Exception
  4. {
  5. Bootstrap bootstrap = new Bootstrap();
  6. try {
  7. bootstrap.initialize().getInstance(Instance.class);
  8. Assert.fail("should require explicit bindings");
  9. }
  10. catch (ConfigurationException e) {
  11. Assertions.assertContains(e.getErrorMessages().iterator().next().getMessage(), "Explicit bindings are required");
  12. }
  13. }

代码示例来源:origin: com.google.inject/guice

  1. /**
  2. * When serialized, we eagerly convert sources to strings. This hurts our formatting, but it
  3. * guarantees that the receiving end will be able to read the message.
  4. */
  5. private Object writeReplace() throws ObjectStreamException {
  6. Object[] sourcesAsStrings = sources.toArray();
  7. for (int i = 0; i < sourcesAsStrings.length; i++) {
  8. sourcesAsStrings[i] = Errors.convert(sourcesAsStrings[i]).toString();
  9. }
  10. return new Message(ImmutableList.copyOf(sourcesAsStrings), message, cause);
  11. }

代码示例来源:origin: com.google.inject/guice

  1. public ProvisionException(String message, Throwable cause) {
  2. super(cause);
  3. this.messages = ImmutableSet.of(new Message(message, cause));
  4. }

代码示例来源:origin: at.bestsolution.efxclipse.eclipse/com.google.inject

  1. /** Returns the formatted message for an exception with the specified messages. */
  2. public static String format(String heading, Collection<Message> errorMessages) {
  3. Formatter fmt = new Formatter().format(heading).format(":%n%n");
  4. int index = 1;
  5. boolean displayCauses = getOnlyCause(errorMessages) == null;
  6. for (Message errorMessage : errorMessages) {
  7. fmt.format("%s) %s%n", index++, errorMessage.getMessage());
  8. List<Object> dependencies = errorMessage.getSources();
  9. for (int i = dependencies.size() - 1; i >= 0; i--) {
  10. Object source = dependencies.get(i);
  11. formatSource(fmt, source);
  12. }
  13. Throwable cause = errorMessage.getCause();
  14. if (displayCauses && cause != null) {
  15. StringWriter writer = new StringWriter();
  16. cause.printStackTrace(new PrintWriter(writer));
  17. fmt.format("Caused by: %s", writer.getBuffer());
  18. }
  19. fmt.format("%n");
  20. }
  21. if (errorMessages.size() == 1) {
  22. fmt.format("1 error");
  23. } else {
  24. fmt.format("%s errors", errorMessages.size());
  25. }
  26. return fmt.toString();
  27. }

代码示例来源:origin: com.google.inject/guice

  1. for (Message errorMessage : errorMessages) {
  2. int thisIdx = index++;
  3. fmt.format("%s) %s%n", thisIdx, errorMessage.getMessage());
  4. List<Object> dependencies = errorMessage.getSources();
  5. for (int i = dependencies.size() - 1; i >= 0; i--) {
  6. Object source = dependencies.get(i);
  7. Throwable cause = errorMessage.getCause();
  8. if (displayCauses && cause != null) {
  9. Equivalence.Wrapper<Throwable> causeEquivalence = ThrowableEquivalence.INSTANCE.wrap(cause);

代码示例来源:origin: com.google.inject/guice

  1. @Override
  2. public Boolean visit(Message message) {
  3. if (message.getCause() != null) {
  4. String rootMessage = getRootMessage(message.getCause());
  5. logger.log(
  6. Level.INFO,
  7. "An exception was caught and reported. Message: " + rootMessage,
  8. message.getCause());
  9. }
  10. errors.addMessage(message);
  11. return true;
  12. }

代码示例来源:origin: Nextdoor/bender

  1. static void checkConfiguration(boolean condition, String format, Object... args) {
  2. if (condition) {
  3. return;
  4. }
  5. throw new ConfigurationException(ImmutableSet.of(new Message(Errors.format(format, args))));
  6. }

代码示例来源:origin: com.google.inject/guice

  1. @Override
  2. public void addError(String message, Object... arguments) {
  3. elements.add(new Message(getElementSource(), Errors.format(message, arguments)));
  4. }

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

  1. @Test
  2. public void testPropertySetting() throws Exception {
  3. IMocksControl control = createControl();
  4. TypeEncounter<SomeInjectableBean> encounter = control.createMock(TypeEncounter.class);
  5. Provider<Injector> injectorProvider = control.createMock(Provider.class);
  6. Injector injector = control.createMock(Injector.class);
  7. expect(encounter.getProvider(Injector.class)).andReturn(injectorProvider);
  8. expect(injectorProvider.get()).andReturn(injector).anyTimes();
  9. Capture<MembersInjector<SomeInjectableBean>> capture = new Capture<MembersInjector<SomeInjectableBean>>();
  10. encounter.register(and(anyObject(MembersInjector.class), capture(capture)));
  11. SecurityManager securityManager = control.createMock(SecurityManager.class);
  12. String property = "myPropertyValue";
  13. expect(injector.getInstance(Key.get(SecurityManager.class))).andReturn(securityManager);
  14. expect(injector.getInstance(Key.get(String.class, Names.named("shiro.myProperty")))).andReturn(property);
  15. expect(injector.getInstance(Key.get(String.class, Names.named("shiro.unavailableProperty"))))
  16. .andThrow(new ConfigurationException(Collections.singleton(new Message("Not Available!"))));
  17. expect((Map)injector.getInstance(BeanTypeListener.MAP_KEY)).andReturn(Collections.EMPTY_MAP).anyTimes();
  18. control.replay();
  19. BeanTypeListener underTest = new BeanTypeListener();
  20. underTest.hear(TypeLiteral.get(SomeInjectableBean.class), encounter);
  21. SomeInjectableBean bean = new SomeInjectableBean();
  22. capture.getValue().injectMembers(bean);
  23. assertSame(securityManager, bean.securityManager);
  24. assertSame(property, bean.myProperty);
  25. assertNull(bean.unavailableProperty);
  26. control.verify();
  27. }

相关文章