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

io.permazen.PermazenFieldScanner Maven / Gradle / Ivy

The newest version!

/*
 * Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
 */

package io.permazen;

import io.permazen.annotation.PermazenField;
import io.permazen.annotation.PermazenType;
import io.permazen.annotation.ValueRange;
import io.permazen.annotation.Values;
import io.permazen.core.DeleteAction;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Scans for {@link PermazenField @PermazenField} annotations.
 */
class PermazenFieldScanner extends AbstractPermazenFieldScanner {

    PermazenFieldScanner(PermazenClass pclass, PermazenType permazenType) {
        super(pclass, PermazenField.class, permazenType);
    }

    @Override
    protected PermazenField getDefaultAnnotation() {
        return PermazenFieldScanner.getDefaultPermazenField(this.permazenType);
    }

    @Override
    protected boolean includeMethod(Method method, PermazenField annotation) {
        this.checkNotStatic(method);
        this.checkParameterTypes(method);
        if (method.getReturnType().equals(Void.TYPE))
            throw new IllegalArgumentException(String.format("%s: method returns void", this.getErrorPrefix(method)));
        return true;
    }

    @Override
    protected boolean isAutoPropertyCandidate(Method method) {
        if (!super.isAutoPropertyCandidate(method))
            return false;
        final Class returnType = method.getReturnType();
        if (List.class.isAssignableFrom(returnType)
          || Set.class.isAssignableFrom(returnType)
          || Map.class.isAssignableFrom(returnType))
            return false;
        if (returnType != Counter.class) {
            final Method setter;
            try {
                setter = Util.findPermazenFieldSetterMethod(this.pclass.type, method);
            } catch (IllegalArgumentException e) {
                return false;
            }
            if (!this.permazenType.autogenNonAbstract() && (setter.getModifiers() & Modifier.ABSTRACT) == 0)
                return false;
        }
        return true;
    }

    public static final PermazenField getDefaultPermazenField(final PermazenType permazenType) {
        return new DefaultPermazenField(permazenType);
    }

// DefaultPermazenField

    public static class DefaultPermazenField implements PermazenField {

        private PermazenType permazenType;

        DefaultPermazenField(PermazenType permazenType) {
            this.permazenType = permazenType;
        }

        @Override
        public Class annotationType() {
            return PermazenField.class;
        }
        @Override
        public String name() {
            return "";
        }
        @Override
        public String encoding() {
            return "";
        }
        @Override
        public int storageId() {
            return 0;
        }
        @Override
        public boolean indexed() {
            return false;
        }
        @Override
        public boolean unique() {
            return false;
        }
        @Override
        public Values uniqueExcludes() {
            return new Values() {
                @Override
                public boolean nulls() {
                    return false;
                }
                @Override
                public boolean nonNulls() {
                    return false;
                }
                @Override
                public String[] value() {
                    return new String[0];
                }
                @Override
                public ValueRange[] ranges() {
                    return new ValueRange[0];
                }
                @Override
                public Class annotationType() {
                    return Values.class;
                }
            };
        }
        @Override
        public String[] forwardCascades() {
            return new String[0];
        }
        @Override
        public String[] inverseCascades() {
            return new String[0];
        }
        @Override
        public DeleteAction inverseDelete() {
            return DeleteAction.EXCEPTION;
        }
        @Override
        public boolean forwardDelete() {
            return false;
        }
        @Override
        public boolean allowDeleted() {
            return this.permazenType.autogenAllowDeleted();
        }
        @Override
        public UpgradeConversionPolicy upgradeConversion() {
            return this.permazenType.autogenUpgradeConversion();
        }
    };
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy