public static void conditionFailedWithException( @Nullable ErrorCollector errorCollector, @Nullable ValueRecorder recorder, @Nullable String text, int line, int column, @Nullable Object message, Throwable throwable) { if (throwable instanceof SpockAssertionError) { final SpockAssertionError spockAssertionError = (SpockAssertionError) throwable; errorCollector.collectOrThrow( spockAssertionError); // this is our exception - it already has good message return; } if (throwable instanceof SpockException) { final SpockException spockException = (SpockException) throwable; errorCollector.collectOrThrow( spockException); // this is our exception - it already has good message return; } final ConditionNotSatisfiedError conditionNotSatisfiedError = new ConditionNotSatisfiedError( new Condition( getValues(recorder), text, TextPosition.create(line, column), messageToString(message), recorder == null ? null : recorder.getCurrentRecordingVarNum(), recorder == null ? null : throwable), throwable); errorCollector.collectOrThrow(conditionNotSatisfiedError); }
@Override public void register(Class<?> endpointClass, String contextPath) throws DeploymentException { final ErrorCollector collector = new ErrorCollector(); AnnotatedEndpoint endpoint = AnnotatedEndpoint.fromClass(endpointClass, componentProviderService, true, collector); EndpointConfig config = endpoint.getEndpointConfig(); TyrusEndpointWrapper ew = new TyrusEndpointWrapper( endpoint, config, componentProviderService, webSocketContainer, contextPath, config instanceof ServerEndpointConfig ? ((ServerEndpointConfig) config).getConfigurator() : null); if (collector.isEmpty()) { register(new TyrusEndpoint(ew)); } else { throw collector.composeComprehensiveException(); } }
private IRubyObject ensureParsedAndDecoded(ThreadContext ctx, String ivname) { ErrorCollector collector = new ErrorCollector(); try { if (!template.isParsed()) { ParseStrategy s = template.accept(ctx, CodecStrategyVisitor.INSTANCE); parse(ctx, this, template, s, collector); collector.clear(); } RubyAsn1Template v = (RubyAsn1Template) getInstanceVariable(ivname); if (v == null) return ctx.getRuntime().getNil(); Asn1Template valueTemplate = v.getTemplate(); if (!(valueTemplate.isParsed() && valueTemplate.isDecoded())) { ParseStrategy s = valueTemplate.accept(ctx, CodecStrategyVisitor.INSTANCE); if (!valueTemplate.isParsed()) { parse(ctx, v, valueTemplate, s, collector); collector.clear(); } if (!valueTemplate.isDecoded()) { decode(ctx, v, valueTemplate, s, collector); } } return valueTemplate.getValue(); } catch (RuntimeException ex) { throw templateError(ctx, collector.getErrorMessages(), template.getDefinition()); } }
@Override public void register(ServerEndpointConfig serverConfig, String contextPath) throws DeploymentException { TyrusEndpointWrapper ew; Class<?> endpointClass = serverConfig.getEndpointClass(); boolean isEndpointClass = false; do { endpointClass = endpointClass.getSuperclass(); if (endpointClass.equals(Endpoint.class)) { isEndpointClass = true; } } while (!endpointClass.equals(Object.class)); if (isEndpointClass) { // we are pretty sure that endpoint class is javax.websocket.Endpoint descendant. //noinspection unchecked ew = new TyrusEndpointWrapper( (Class<? extends Endpoint>) serverConfig.getEndpointClass(), serverConfig, componentProviderService, webSocketContainer, contextPath, serverConfig.getConfigurator()); } else { final ErrorCollector collector = new ErrorCollector(); final AnnotatedEndpoint endpoint = AnnotatedEndpoint.fromClass( serverConfig.getEndpointClass(), componentProviderService, true, collector); final EndpointConfig config = endpoint.getEndpointConfig(); ew = new TyrusEndpointWrapper( endpoint, config, componentProviderService, webSocketContainer, contextPath, config instanceof ServerEndpointConfig ? ((ServerEndpointConfig) config).getConfigurator() : null); if (!collector.isEmpty()) { throw collector.composeComprehensiveException(); } } register(new TyrusEndpoint(ew)); }
/** * Provide an instance of class which is coupled to {@link Session}. The first time the method is * called the provider creates an instance and caches it. Next time the method is called the * cached instance is returned. * * @param c {@link Class} whose instance will be provided. * @param collector error collector. * @param <T> type of the provided instance. * @return instance */ public <T> Object getInstance(Class<T> c, Session session, ErrorCollector collector) { Object loaded = null; final Map<Class<?>, Object> classObjectMap = sessionToObject.get(session); try { if (classObjectMap != null) { synchronized (classObjectMap) { if (classObjectMap.containsKey(c)) { loaded = classObjectMap.get(c); } else { // returns not-null value loaded = getEndpointInstance(c); sessionToObject.get(session).put(c, loaded); } } } else { loaded = getEndpointInstance(c); final HashMap<Class<?>, Object> hashMap = new HashMap<Class<?>, Object>(); hashMap.put(c, loaded); sessionToObject.put(session, hashMap); } } catch (Exception e) { collector.addException( new DeploymentException( LocalizationMessages.COMPONENT_PROVIDER_THREW_EXCEPTION(c.getName()), e)); } return loaded; }
public T orCollectAndThrow(RuntimeException e, ErrorCollector collector) { T t = orNull(); if (t == null) { collector.add(e); throw e; } return t; }
private void flagError( final String s, final String code, final boolean warning, final boolean info, final EObject source, final String... data) throws GamaRuntimeException { if (warning && !info && !GamaPreferences.WARNINGS_ENABLED.getValue()) { return; } if (info && !GamaPreferences.INFO_ENABLED.getValue()) { return; } IDescription desc = this; EObject e = source; if (e == null) { e = getUnderlyingElement(null); } while (e == null && desc != null) { desc = desc.getEnclosingDescription(); if (desc != null) { e = desc.getUnderlyingElement(null); } } if (!warning && !info) { final String resource = e == null ? "(no file)" : e.eResource().getURI().lastSegment(); // System.err.println("COMPILATION ERROR in " + this.toString() + ": // " + s + "; source: " + resource); } // throws a runtime exception if there is no way to signal the error in // the source // (i.e. we are probably in a runtime scenario) if (e == null || e.eResource().getURI().path().contains(IExpressionCompiler.SYNTHETIC_RESOURCES_PREFIX)) { throw warning ? GamaRuntimeException.warning(s) : GamaRuntimeException.error(s); } final ErrorCollector c = getErrorCollector(); if (c == null) { System.out.println((warning ? "Warning" : "Error") + ": " + s); return; } c.add(new GamlCompilationError(s, code, e, warning, info, data)); }
private static void parse( ThreadContext ctx, IRubyObject recv, Asn1Template template, ParseStrategy s, ErrorCollector collector) { Definition d = new Definition(template.getDefinition(), template.getOptions()); ParseContext parseCtx = new ParseContext(ctx, recv, template, d, collector); if (!s.match(parseCtx.asMatchContext()).isSuccess()) throw collector.addAndReturn(Errors.newASN1Error(ctx.getRuntime(), "Type mismatch")); s.parse(parseCtx); }
/** * Creates an instance of {@link Class} c using {@link Class#newInstance()}. Exceptions are logged * to {@link ErrorCollector}. * * @param c {@link Class} whose instance is going to be created * @param collector {@link ErrorCollector} which collects the {@link Exception}s. * @param <T> type. * @return new instance of {@link Class}. */ public static <T> T getInstance(Class<T> c, ErrorCollector collector) { T instance = null; try { instance = getInstance(c); } catch (Exception e) { collector.addException( new DeploymentException(LocalizationMessages.CLASS_NOT_INSTANTIATED(c.getName()), e)); } return instance; }
// method calls with spread-dot operator are not rewritten, hence this method doesn't have to care // about spread-dot public static void verifyMethodCondition( @Nullable ErrorCollector errorCollector, @Nullable ValueRecorder recorder, @Nullable String text, int line, int column, @Nullable Object message, Object target, String method, Object[] args, boolean safe, boolean explicit, int lastVariableNum) { MatcherCondition matcherCondition = MatcherCondition.parse(target, method, args, safe); if (matcherCondition != null) { matcherCondition.verify( errorCollector, getValues(recorder), text, line, column, messageToString(message)); return; } if (recorder != null) { recorder.startRecordingValue(lastVariableNum); } Object result = safe ? GroovyRuntimeUtil.invokeMethodNullSafe(target, method, args) : GroovyRuntimeUtil.invokeMethod(target, method, args); if (!explicit && result == null && GroovyRuntimeUtil.isVoidMethod(target, method, args)) return; if (!GroovyRuntimeUtil.isTruthy(result)) { List<Object> values = getValues(recorder); if (values != null) CollectionUtil.setLastElement(values, result); final ConditionNotSatisfiedError conditionNotSatisfiedError = new ConditionNotSatisfiedError( new Condition( values, text, TextPosition.create(line, column), messageToString(message), null, null)); errorCollector.collectOrThrow(conditionNotSatisfiedError); } }
/** * Provide an instance of {@link javax.websocket.Encoder} or {@link javax.websocket.Decoder} * descendant which is coupled to {@link Session}. The first time the method is called the * provider creates an instance, calls {@link * javax.websocket.Encoder#init(javax.websocket.EndpointConfig)} or {@link * javax.websocket.Decoder#init(javax.websocket.EndpointConfig)} and caches it. Next time the * method is called the cached instance is returned. * * @param c {@link Class} whose instance will be provided. * @param collector error collector. * @param endpointConfig configuration corresponding to current context. Used for {@link * javax.websocket.Encoder#init(javax.websocket.EndpointConfig)} and {@link * javax.websocket.Decoder#init(javax.websocket.EndpointConfig)} * @param <T> type of the provided instance. * @return instance */ public <T> Object getCoderInstance( Class<T> c, Session session, EndpointConfig endpointConfig, ErrorCollector collector) { Object loaded = null; final Map<Class<?>, Object> classObjectMap = sessionToObject.get(session); try { if (classObjectMap != null) { synchronized (classObjectMap) { if (classObjectMap.containsKey(c)) { loaded = classObjectMap.get(c); } else { loaded = getInstance(c); if (loaded != null) { if (loaded instanceof Encoder) { ((Encoder) loaded).init(endpointConfig); } else if (loaded instanceof Decoder) { ((Decoder) loaded).init(endpointConfig); } sessionToObject.get(session).put(c, loaded); } } } } else { loaded = getInstance(c); if (loaded != null) { if (loaded instanceof Encoder) { ((Encoder) loaded).init(endpointConfig); } else if (loaded instanceof Decoder) { ((Decoder) loaded).init(endpointConfig); } final HashMap<Class<?>, Object> hashMap = new HashMap<Class<?>, Object>(); hashMap.put(c, loaded); sessionToObject.put(session, hashMap); } } } catch (InstantiationException e) { collector.addException( new DeploymentException( LocalizationMessages.COMPONENT_PROVIDER_THREW_EXCEPTION(c.getName()), e)); } return loaded; }
void verify( @Nullable ErrorCollector errorCollector, @Nullable List<Object> values, @Nullable String text, int line, int column, @Nullable String message) { if (HamcrestFacade.matches(matcher, actual)) return; if (values != null) { CollectionUtil.setLastElement(values, shortSyntax ? actual : false); replaceMatcherValues(values); } String description = HamcrestFacade.getFailureDescription(matcher, actual, message); Condition condition = new Condition(values, text, TextPosition.create(line, column), description, null, null); errorCollector.collectOrThrow(new ConditionNotSatisfiedError(condition)); }
// condition can be null too, but not in the sense of "not available" public static void verifyCondition( @Nullable ErrorCollector errorCollector, @Nullable ValueRecorder recorder, @Nullable String text, int line, int column, @Nullable Object message, @Nullable Object condition) { if (!GroovyRuntimeUtil.isTruthy(condition)) { final ConditionNotSatisfiedError conditionNotSatisfiedError = new ConditionNotSatisfiedError( new Condition( getValues(recorder), text, TextPosition.create(line, column), messageToString(message), null, null)); errorCollector.collectOrThrow(conditionNotSatisfiedError); } }