Search in sources :

Example 1 with Optional

use of org.apache.tapestry5.ioc.annotations.Optional in project tapestry-5 by apache.

the class PackageAndArtifactChangeRefactorCommitParser method apply.

@Override
public Optional<ClassRefactor> apply(String line) {
    ClassRefactor move = null;
    final Matcher matcher = PATTERN.matcher(line);
    if (matcher.matches()) {
        // System.out.printf("1(%s) 2(%s) 3(%s) 4(%s) 5(%s)\n", matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(4), matcher.group(5));
        String newPackageNameSuffix = FileRefactorCommitParser.extractPackageOrClassName(matcher.group(4));
        String oldPackageNameSuffix = FileRefactorCommitParser.extractPackageOrClassName(matcher.group(2));
        final String className = matcher.group(5);
        final String newClassName = FileRefactorCommitParser.buildClassName("", newPackageNameSuffix, className);
        final String oldClassName = FileRefactorCommitParser.buildClassName("", oldPackageNameSuffix, className);
        final String sourceArtifactName = matcher.group(1);
        final String destinationArtifactName = matcher.group(3);
        move = new ClassRefactor(newClassName, oldClassName, sourceArtifactName, destinationArtifactName);
    }
    return Optional.ofNullable(move);
}
Also used : ClassRefactor(org.apache.tapestry5.versionmigrator.ClassRefactor) Matcher(java.util.regex.Matcher)

Example 2 with Optional

use of org.apache.tapestry5.ioc.annotations.Optional in project tapestry-5 by apache.

the class PackageChangeRefactorCommitParser method apply.

@Override
public Optional<ClassRefactor> apply(String line) {
    final Matcher matcher = PATTERN.matcher(line);
    ClassRefactor move = null;
    if (matcher.matches()) {
        String newPackageNameSuffix = matcher.group(4);
        String oldPackageNameSuffix = matcher.group(3);
        // System.out.printf("1(%s) 2(%s) 3(%s) 4(%s) 5(%s)\n", matcher.group(1), matcher.group(2), oldPackageNameSuffix, newPackageNameSuffix, matcher.group(5));
        final String rootPackageName = matcher.group(2).replace("src/main/java/", "").replace("/", ".");
        final String className = matcher.group(5);
        final String newClassName = FileRefactorCommitParser.buildClassName(rootPackageName, newPackageNameSuffix, className);
        final String oldClassName = FileRefactorCommitParser.buildClassName(rootPackageName, oldPackageNameSuffix, className);
        final String artifactName = matcher.group(1);
        move = new ClassRefactor(newClassName, oldClassName, artifactName, artifactName);
    }
    return Optional.ofNullable(move);
}
Also used : ClassRefactor(org.apache.tapestry5.versionmigrator.ClassRefactor) Matcher(java.util.regex.Matcher)

Example 3 with Optional

use of org.apache.tapestry5.ioc.annotations.Optional in project tapestry-5 by apache.

the class ArtifactChangeRefactorCommitParserTest method valid_line.

@Test
public void valid_line() {
    // {tapestry-ioc => tapestry5-annotations}/src/main/java/org/apache/tapestry5/ioc/annotations/Advise.java
    ArtifactChangeRefactorCommitParser parser = new ArtifactChangeRefactorCommitParser();
    Optional<ClassRefactor> optionalRefactor = parser.apply(ArtifactChangeRefactorCommitParser.EXAMPLE);
    Assert.assertTrue(optionalRefactor.isPresent(), "Line not detected as a change of artifact.");
    ClassRefactor refactor = optionalRefactor.get();
    final String expectedClassName = "org.apache.tapestry5.ioc.annotations.Advise";
    Assert.assertEquals(refactor.getNewClassName(), expectedClassName);
    Assert.assertEquals(refactor.getOldClassName(), expectedClassName);
    Assert.assertEquals(refactor.getDestinationArtifact(), "tapestry5-annotations");
    Assert.assertEquals(refactor.getSourceArtifact(), "tapestry-ioc");
}
Also used : ClassRefactor(org.apache.tapestry5.versionmigrator.ClassRefactor) Test(org.testng.annotations.Test)

Example 4 with Optional

use of org.apache.tapestry5.ioc.annotations.Optional in project tapestry-5 by apache.

the class TypeCoercerImpl method findOrCreateCoercion.

/**
 * Here's the real meat; we do a search of the space to find coercions, or a system of
 * coercions, that accomplish
 * the desired coercion.
 *
 * There's <strong>TREMENDOUS</strong> room to improve this algorithm. For example, inheritance lists could be
 * cached. Further, there's probably more ways to early prune the search. However, even with dozens or perhaps
 * hundreds of tuples, I suspect the search will still grind to a conclusion quickly.
 *
 * The order of operations should help ensure that the most efficient tuple chain is located. If you think about how
 * tuples are added to the queue, there are two factors: size (the number of steps in the coercion) and
 * "class distance" (that is, number of steps up the inheritance hiearchy). All the appropriate 1 step coercions
 * will be considered first, in class distance order. Along the way, we'll queue up all the 2 step coercions, again
 * in class distance order. By the time we reach some of those, we'll have begun queueing up the 3 step coercions, and
 * so forth, until we run out of input tuples we can use to fabricate multi-step compound coercions, or reach a
 * final response.
 *
 * This does create a good number of short lived temporary objects (the compound tuples), but that's what the GC is
 * really good at.
 *
 * @param sourceType
 * @param targetType
 * @return coercer from sourceType to targetType
 */
@SuppressWarnings("unchecked")
private Coercion findOrCreateCoercion(Class sourceType, Class targetType) {
    if (sourceType == Void.class) {
        return searchForNullCoercion(targetType);
    }
    // Trying to find exact match.
    Optional<CoercionTuple> maybeTuple = getTuples(sourceType, targetType).stream().filter((t) -> sourceType.equals(t.getSourceType()) && targetType.equals(t.getTargetType())).findFirst();
    if (maybeTuple.isPresent()) {
        return maybeTuple.get().getCoercion();
    }
    // These are instance variables because this method may be called concurrently.
    // On a true race, we may go to the work of seeking out and/or fabricating
    // a tuple twice, but it's more likely that different threads are looking
    // for different source/target coercions.
    Set<CoercionTuple.Key> consideredTuples = CollectionFactory.newSet();
    LinkedList<CoercionTuple> queue = CollectionFactory.newLinkedList();
    seedQueue(sourceType, targetType, consideredTuples, queue);
    while (!queue.isEmpty()) {
        CoercionTuple tuple = queue.removeFirst();
        // If the tuple results in a value type that is assignable to the desired target type,
        // we're done! Later, we may add a concept of "cost" (i.e. number of steps) or
        // "quality" (how close is the tuple target type to the desired target type). Cost
        // is currently implicit, as compound tuples are stored deeper in the queue,
        // so simpler coercions will be located earlier.
        Class tupleTargetType = tuple.getTargetType();
        if (targetType.isAssignableFrom(tupleTargetType)) {
            return tuple.getCoercion();
        }
        // So .. this tuple doesn't get us directly to the target type.
        // However, it *may* get us part of the way. Each of these
        // represents a coercion from the source type to an intermediate type.
        // Now we're going to look for conversions from the intermediate type
        // to some other type.
        queueIntermediates(sourceType, targetType, tuple, consideredTuples, queue);
    }
    throw new CoercionNotFoundException(String.format("Could not find a coercion from type %s to type %s.", sourceType.getName(), targetType.getName()), buildCoercionCatalog(), sourceType, targetType);
}
Also used : PlasticUtils(org.apache.tapestry5.plastic.PlasticUtils) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) InternalCommonsUtils(org.apache.tapestry5.commons.internal.util.InternalCommonsUtils) Collection(java.util.Collection) Set(java.util.Set) StringToEnumCoercion(org.apache.tapestry5.commons.util.StringToEnumCoercion) LockSupport(org.apache.tapestry5.commons.internal.util.LockSupport) List(java.util.List) F(org.apache.tapestry5.func.F) Coercion(org.apache.tapestry5.commons.services.Coercion) CoercionNotFoundException(org.apache.tapestry5.commons.util.CoercionNotFoundException) CollectionFactory(org.apache.tapestry5.commons.util.CollectionFactory) Map(java.util.Map) CoercionTuple(org.apache.tapestry5.commons.services.CoercionTuple) AvailableValues(org.apache.tapestry5.commons.util.AvailableValues) Optional(java.util.Optional) InheritanceSearch(org.apache.tapestry5.commons.internal.util.InheritanceSearch) LinkedList(java.util.LinkedList) Collections(java.util.Collections) WeakHashMap(java.util.WeakHashMap) CoercionFailedException(org.apache.tapestry5.commons.util.CoercionFailedException) UnknownValueException(org.apache.tapestry5.commons.util.UnknownValueException) CoercionNotFoundException(org.apache.tapestry5.commons.util.CoercionNotFoundException) CoercionTuple(org.apache.tapestry5.commons.services.CoercionTuple)

Example 5 with Optional

use of org.apache.tapestry5.ioc.annotations.Optional in project tapestry-5 by apache.

the class RestSupportImplTest method get_request_body_as_null_result.

@Test
public void get_request_body_as_null_result() {
    HttpRequestBodyConverter converter = new TestHttpRequestBodyConverter(null);
    RestSupport restSupport = new RestSupportImpl(null, converter);
    Optional<String> result = restSupport.getRequestBodyAs(String.class);
    assertFalse(result.isPresent());
}
Also used : HttpRequestBodyConverter(org.apache.tapestry5.http.services.HttpRequestBodyConverter) RestSupportImpl(org.apache.tapestry5.http.internal.services.RestSupportImpl) RestSupport(org.apache.tapestry5.http.services.RestSupport) Test(org.testng.annotations.Test)

Aggregations

ClassRefactor (org.apache.tapestry5.versionmigrator.ClassRefactor)6 Test (org.testng.annotations.Test)5 List (java.util.List)3 Optional (java.util.Optional)3 Matcher (java.util.regex.Matcher)3 JSONObject (org.apache.tapestry5.json.JSONObject)3 Arrays (java.util.Arrays)2 Map (java.util.Map)2 Set (java.util.Set)2 RequestParameter (org.apache.tapestry5.annotations.RequestParameter)2 RestSupportImpl (org.apache.tapestry5.http.internal.services.RestSupportImpl)2 HttpRequestBodyConverter (org.apache.tapestry5.http.services.HttpRequestBodyConverter)2 RestSupport (org.apache.tapestry5.http.services.RestSupport)2 Contribute (org.apache.tapestry5.ioc.annotations.Contribute)2 BeanInfo (java.beans.BeanInfo)1 IntrospectionException (java.beans.IntrospectionException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1