com.htmlhifive.tools.codeassist.core.proposal.build.CodeBuilderUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.htmlhifive.tools.codeassist.core.proposal.build.CodeBuilderUtils.java

Source

/*
 * Copyright (C) 2012 NS Solutions Corporation
 *
 * 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.htmlhifive.tools.codeassist.core.proposal.build;

import org.apache.commons.lang.StringUtils;

import com.htmlhifive.tools.codeassist.core.config.bean.FunctionBean;
import com.htmlhifive.tools.codeassist.core.config.bean.VarReferenceBean;
import com.htmlhifive.tools.codeassist.core.config.bean.VariableBean;

/**
 * ?.
 * 
 * @author NS Solutions Corporation
 * 
 */
final class CodeBuilderUtils {
    /**
     * .
     */
    private CodeBuilderUtils() {

    }

    /**
     * FunctionBean????.<br>
     * ex.<br>
     * <code>
     * function(arg1,arg2){return new Return();}
     * </code>
     * 
     * @param sb ?.
     * @param method ???.
     */
    static void addFunctionCode(StringBuilder sb, FunctionBean method) {

        sb.append("function(");
        VariableBean[] args = method.getArgments();
        boolean existArgs = false;
        for (VariableBean arg : args) {
            existArgs = true;
            sb.append(arg.getName());
            sb.append(",");
        }
        // 
        if (existArgs) {
            sb.deleteCharAt(sb.length() - 1);
        }
        sb.append("){");
        if (StringUtils.isNotEmpty(method.getReturnType()) && !(method.getReturnType().equals("void"))) {
            sb.append("return new ");
            sb.append(method.getReturnType());
            sb.append("();");
        }
        sb.append("}");
    }

    /**
     * ??.<br>
     * <code>
     * key : new ClassName()
     * </code>
     * 
     * @param sb ?.
     * @param varReferenceBean ???.
     */
    public static void addJsProperty(StringBuilder sb, VarReferenceBean varReferenceBean) {

        sb.append(varReferenceBean.getKey());
        sb.append(": new ");
        sb.append(varReferenceBean.getClassName());
        sb.append("()");

    }
}