Search in sources :

Example 1 with AccessSpecifier

use of com.github.javaparser.ast.AccessSpecifier in project javaparser by javaparser.

the class ConstructorDeclaration method getDeclarationAsString.

/**
 * The declaration returned has this schema:
 *
 * [accessSpecifier] className ([paramType [paramName]])
 * [throws exceptionsList]
 */
@Override
public String getDeclarationAsString(boolean includingModifiers, boolean includingThrows, boolean includingParameterName) {
    StringBuffer sb = new StringBuffer();
    if (includingModifiers) {
        AccessSpecifier accessSpecifier = ModifierSet.getAccessSpecifier(getModifiers());
        sb.append(accessSpecifier.getCodeRepresenation());
        sb.append(accessSpecifier == AccessSpecifier.DEFAULT ? "" : " ");
    }
    sb.append(getName());
    sb.append("(");
    boolean firstParam = true;
    for (Parameter param : getParameters()) {
        if (firstParam) {
            firstParam = false;
        } else {
            sb.append(", ");
        }
        if (includingParameterName) {
            sb.append(param.toStringWithoutComments());
        } else {
            sb.append(param.getType().toStringWithoutComments());
        }
    }
    sb.append(")");
    if (includingThrows) {
        boolean firstThrow = true;
        for (NameExpr thr : getThrows()) {
            if (firstThrow) {
                firstThrow = false;
                sb.append(" throws ");
            } else {
                sb.append(", ");
            }
            sb.append(thr.toStringWithoutComments());
        }
    }
    return sb.toString();
}
Also used : NameExpr(com.github.javaparser.ast.expr.NameExpr) TypeParameter(com.github.javaparser.ast.TypeParameter) AccessSpecifier(com.github.javaparser.ast.AccessSpecifier)

Example 2 with AccessSpecifier

use of com.github.javaparser.ast.AccessSpecifier in project javaparser by javaparser.

the class MethodDeclaration method getDeclarationAsString.

/**
 * The declaration returned has this schema:
 *
 * [accessSpecifier] [static] [abstract] [final] [native]
 * [synchronized] returnType methodName ([paramType [paramName]])
 * [throws exceptionsList]
 * @return method declaration as String
 */
@Override
public String getDeclarationAsString(boolean includingModifiers, boolean includingThrows, boolean includingParameterName) {
    StringBuffer sb = new StringBuffer();
    if (includingModifiers) {
        AccessSpecifier accessSpecifier = ModifierSet.getAccessSpecifier(getModifiers());
        sb.append(accessSpecifier.getCodeRepresenation());
        sb.append(accessSpecifier == AccessSpecifier.DEFAULT ? "" : " ");
        if (ModifierSet.isStatic(getModifiers())) {
            sb.append("static ");
        }
        if (ModifierSet.isAbstract(getModifiers())) {
            sb.append("abstract ");
        }
        if (ModifierSet.isFinal(getModifiers())) {
            sb.append("final ");
        }
        if (ModifierSet.isNative(getModifiers())) {
            sb.append("native ");
        }
        if (ModifierSet.isSynchronized(getModifiers())) {
            sb.append("synchronized ");
        }
    }
    // TODO verify it does not print comments connected to the type
    sb.append(getType().toStringWithoutComments());
    sb.append(" ");
    sb.append(getName());
    sb.append("(");
    boolean firstParam = true;
    for (Parameter param : getParameters()) {
        if (firstParam) {
            firstParam = false;
        } else {
            sb.append(", ");
        }
        if (includingParameterName) {
            sb.append(param.toStringWithoutComments());
        } else {
            sb.append(param.getType().toStringWithoutComments());
            if (param.isVarArgs()) {
                sb.append("...");
            }
        }
    }
    sb.append(")");
    if (includingThrows) {
        boolean firstThrow = true;
        for (NameExpr thr : getThrows()) {
            if (firstThrow) {
                firstThrow = false;
                sb.append(" throws ");
            } else {
                sb.append(", ");
            }
            sb.append(thr.toStringWithoutComments());
        }
    }
    return sb.toString();
}
Also used : NameExpr(com.github.javaparser.ast.expr.NameExpr) TypeParameter(com.github.javaparser.ast.TypeParameter) AccessSpecifier(com.github.javaparser.ast.AccessSpecifier)

Aggregations

AccessSpecifier (com.github.javaparser.ast.AccessSpecifier)2 TypeParameter (com.github.javaparser.ast.TypeParameter)2 NameExpr (com.github.javaparser.ast.expr.NameExpr)2