com.github.htfv.maven.plugins.eclipseconfigurator.ConfigureMojo.java Source code

Java tutorial

Introduction

Here is the source code for com.github.htfv.maven.plugins.eclipseconfigurator.ConfigureMojo.java

Source

/*
 * Copyright 2013 htfv (Aliaksei Lahachou)
 *
 * 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 com.github.htfv.maven.plugins.eclipseconfigurator;

import javax.el.ELContext;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import com.github.htfv.maven.plugins.eclipseconfigurator.configuration.JavaEditorConfiguration;
import com.github.htfv.maven.plugins.eclipseconfigurator.utils.ECELUtils;

@Mojo(defaultPhase = LifecyclePhase.INITIALIZE, name = "configure", threadSafe = true)
public class ConfigureMojo extends AbstractMojo {
    private ELContext elContext;

    @Parameter
    private JavaEditorConfiguration javaEditor;

    @Component
    private MavenProject project;

    public <T> T connectorGetParameter(final String expression, final Class<T> expectedType) {
        if (StringUtils.isEmpty(expression)) {
            return null;
        }

        final String[] fieldNames = expression.split("\\.");
        Object value = this;

        for (int i = 0; i < fieldNames.length; i += 1) {
            try {
                value = FieldUtils.readField(value, fieldNames[i], true);

                if (value == null) {
                    return null;
                }
            } catch (final IllegalAccessException e) {
                return null;
            }
        }

        return ECELUtils.getValue(getELContext(), value.toString(), expectedType);
    }

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
    }

    private ELContext getELContext() {
        if (elContext == null) {
            elContext = ECELUtils.createELContext(project);
        }

        return elContext;
    }
}