to String Of Modifiers - Java Reflection

Java examples for Reflection:Modifier

Description

to String Of Modifiers

Demo Code

/*/*from  w  w w  .  j  a v  a2  s . c om*/
 * Copyright 2004-2010 the Seasar Foundation and the Others.
 *
 * 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.java2s;

import java.util.Set;
import javax.lang.model.element.Modifier;

public class Main {

    public static String toStringOfModifiers(final Set<Modifier> modifiers) {
        if (modifiers.isEmpty()) {
            return "";
        }

        final StringBuilder buf = new StringBuilder(32);
        if (modifiers.contains(Modifier.PUBLIC)) {
            buf.append("public ");
        }
        if (modifiers.contains(Modifier.PROTECTED)) {
            buf.append("protected ");
        }
        if (modifiers.contains(Modifier.PRIVATE)) {
            buf.append("private ");
        }
        if (modifiers.contains(Modifier.ABSTRACT)) {
            buf.append("abstract ");
        }
        if (modifiers.contains(Modifier.STATIC)) {
            buf.append("static ");
        }
        if (modifiers.contains(Modifier.FINAL)) {
            buf.append("final ");
        }
        if (modifiers.contains(Modifier.TRANSIENT)) {
            buf.append("transient ");
        }
        if (modifiers.contains(Modifier.VOLATILE)) {
            buf.append("volatile ");
        }
        if (modifiers.contains(Modifier.SYNCHRONIZED)) {
            buf.append("synchronized ");
        }
        if (modifiers.contains(Modifier.NATIVE)) {
            buf.append("native ");
        }
        if (modifiers.contains(Modifier.STRICTFP)) {
            buf.append("strictfp ");
        }
        return new String(buf);
    }
}

Related Tutorials