All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.projectmonkey.internal.MappingProgress Maven / Gradle / Ivy

/*
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.projectmonkey.internal;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import net.projectmonkey.config.Configuration;
import net.projectmonkey.spi.NameableType;
import net.projectmonkey.spi.PropertyInfo;


/**
 * Tracks progress while intercepting a mapping's method invocation chain. Friend of
 * MappingBuilderImpl.
 * 
 * @param  PropertyInfo type
 * 
 * @author Jonathan Halterman
 */
abstract class MappingProgress {
  protected final MappingBuilderImpl builder;
  protected final Configuration config;
  protected final List propertyInfo = new ArrayList();

  MappingProgress(MappingBuilderImpl builder) {
    this.builder = builder;
    this.config = builder.configuration;
  }

  static class DestinationProgress extends MappingProgress {
    Object argument;

    DestinationProgress(MappingBuilderImpl builder) {
      super(builder);
    }

    @Override
    public void encountered(Class proxyType, Method method, Object[] args) {
      if (PropertyResolver.MUTATORS.isValid(method)) {
        String propertyName = config.getDestinationNameTransformer().transform(method.getName(),
            NameableType.METHOD);
        propertyInfo.add(PropertyInfoRegistry.mutatorFor(proxyType, method, config,
            propertyName));
        argument = args.length == 1 ? args[0] : null;
      } else if (PropertyResolver.ACCESSORS.isValid(method)) {
        // Find mutator corresponding to accessor
        Mutator mutator = TypeInfoRegistry.typeInfoFor(proxyType, config).mutatorForAccessor(
            method.getName());
        if (mutator != null)
          propertyInfo.add(mutator);
        else
          builder.errors.missingMutatorForAccessor(method);
      } else
        builder.errors.invalidDestinationMethod(method);

      if (argument != null && argument == builder.source)
        builder.errors.missingSource();
    }

    @Override
    public void reset() {
      super.reset();
      argument = null;
    }
  }

  static class SourceProgress extends MappingProgress {
    SourceProgress(MappingBuilderImpl builder) {
      super(builder);
    }

    @Override
    public void encountered(Class proxyType, Method method, Object[] args) {
      if (PropertyResolver.ACCESSORS.isValid(method)) {
        String propertyName = config.getSourceNameTransformer().transform(method.getName(),
            NameableType.METHOD);
        propertyInfo.add(PropertyInfoRegistry.accessorFor(proxyType, method, config,
            propertyName));
      } else
        builder.errors.invalidSourceMethod(method);
    }
  }

  boolean contains(Object object) {
    return propertyInfo.contains(object);
  }

  abstract void encountered(Class proxyType, Method method, Object[] args);;

  void reset() {
    propertyInfo.clear();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy