001/*
002 * Copyright (c) 2012-2018 Institut National des Sciences Appliquées de Lyon (INSA Lyon) and others
003 *
004 * This program and the accompanying materials are made available under the
005 * terms of the Eclipse Public License 2.0 which is available at
006 * http://www.eclipse.org/legal/epl-2.0.
007 *
008 * SPDX-License-Identifier: EPL-2.0
009 */
010
011package org.eclipse.golo.runtime.augmentation;
012
013import java.util.stream.Stream;
014import org.eclipse.golo.runtime.MethodInvocation;
015
016import static org.eclipse.golo.runtime.augmentation.DefiningModule.Scope;
017
018/**
019 * Encapsulate runtime information for an augmentation application resolution.
020 */
021public final class AugmentationApplication {
022
023  enum Kind { SIMPLE, NAMED }
024
025  private final Class<?> augmentation;
026  private final Class<?> target;
027  private final Scope scope;
028  private final Kind kind;
029
030  AugmentationApplication(Class<?> augmentation, Class<?> target, Scope scope, Kind kind) {
031    this.scope = scope;
032    this.target = target;
033    this.augmentation = augmentation;
034    this.kind = kind;
035  }
036
037  @Override
038  public String toString() {
039    return String.format("AugmentationApplication<%s,%s,%s,%s>", augmentation, target, scope, kind);
040  }
041
042  public Stream<AugmentationMethod> methodsMaching(MethodInvocation invocation) {
043    if (augmentation == null) {
044      return Stream.empty();
045    }
046    return Stream.of(augmentation.getMethods())
047      .filter(invocation::match)
048      .map(method -> new AugmentationMethod(kind, scope, target, method));
049  }
050}
051