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

guru.nidi.codeassert.pmd.PmdRuleset Maven / Gradle / Ivy

There is a newer version: 0.9.15
Show newest version
/*
 * Copyright © 2015 Stefan Niederhauser ([email protected])
 *
 * 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 guru.nidi.codeassert.pmd;

import guru.nidi.codeassert.AnalyzerException;
import net.sourceforge.pmd.*;

import java.lang.reflect.Field;

public class PmdRuleset {
    final String name;

    public PmdRuleset(String name) {
        this.name = name;
    }

    public void apply(RuleSets ruleSets) {
        for (final Field descField : getClass().getDeclaredFields()) {
            final PropertyField propertyField = descField.getAnnotation(PropertyField.class);
            if (propertyField != null) {
                try {
                    descField.setAccessible(true);
                    final Object value = descField.get(this);
                    if (value != null) {
                        setProperty(ruleSets, propertyField.rule(), propertyField.property(), value);
                    }
                } catch (IllegalAccessException e) {
                    throw new AnalyzerException("Could not read property " + descField.getName()
                            + " from class " + getClass(), e);
                }
            }
        }
    }

    private void setProperty(RuleSets ruleSets, String rule, String property, Object value) {
        final Rule r = ruleSets.getRuleByName(rule);
        if (r == null) {
            throw new AnalyzerException("Rule '" + rule + "' not existing.");
        }
        final PropertyDescriptor descriptor = (PropertyDescriptor) r.getPropertyDescriptor(property);
        if (descriptor == null) {
            throw new AnalyzerException("Property '" + property + "' for rule '" + rule + "' not existing.");
        }
        r.setProperty(descriptor, value);
    }
}