Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * JBoss, Home of Professional Open Source.
  *
  * Copyright 2012 Red Hat, Inc.
  *
  * 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 org.jboss.classfilewriter.util;
Generates the contents of a Signature attribute from a java type.

TODO: this is all wrong, I'm to tired to read the pathetic excuse for a spec properly

Author(s):
Stuart Douglas
public class SignatureBuilder {

    
Generates the field signiture for a field of the given type

Parameters:
type
Returns:
the signiture, or null if no signure is required (e.g. for Class types)
    public static String fieldAttribute(Type type) {
        StringBuilder builder = new StringBuilder();
        fieldAttribute(typebuilder);
        return builder.toString();
    }
    private static void fieldAttribute(Type typeStringBuilder builder) {
        if (type instanceof Class<?>) {
            classType((Class<?>) typebuilder);
        } else if (type instanceof ParameterizedType) {
            ParameterizedType ptype = (ParameterizedTypetype;
            parametizedType(ptypebuilder);
        } else if (type instanceof WildcardType) {
            WildcardType ptype = (WildcardTypetype;
            wildcardType(ptypebuilder);
        }
    }
    private static void wildcardType(WildcardType typeStringBuilder builder) {
        // WRONG
        builder.append('*');
    }
    public static void parametizedType(ParameterizedType typeStringBuilder builder) {
        fieldAttribute(type.getRawType(), builder); //write the owner type
        //now write the type arguments
        builder.append('<');
        for(Type t : type.getActualTypeArguments()) {
            fieldAttribute(tbuilder);
            builder.append(';');
        }
        builder.append(">;");
    }
    private static void classType(Class<?> clazzStringBuilder builder) {
        if (clazz.isMemberClass()) {
            classType(clazz.getDeclaringClass(), builder);
            builder.append('.');
            builder.append(clazz.getSimpleName());
        } else {
            builder.append("L");
            builder.append(clazz.getName().replace('.''/'));
        }
    }
    private SignatureBuilder() {
    }
New to GrepCode? Check out our FAQ X