Example usage for java.lang.annotation Annotation toString

List of usage examples for java.lang.annotation Annotation toString

Introduction

In this page you can find the example usage for java.lang.annotation Annotation toString.

Prototype

String toString();

Source Link

Document

Returns a string representation of this annotation.

Usage

From source file:Main.java

public static void main(String[] args) {

    Main cls = new Main();
    Class c = cls.getClass();//from   w ww .jav  a 2s  . c o  m

    Annotation[] a = c.getAnnotations();
    for (Annotation val : a) {
        System.out.println(val.toString());
    }
}

From source file:Spy.java

public static void main(String... args) throws Exception {
    Class<?> c = Class.forName("Spy");
    Field[] flds = c.getFields();
    for (Field f : flds) {
        Annotation[] annos = f.getDeclaredAnnotations();
        for (Annotation anno : annos) {
            System.out.println(anno.toString());
        }/*from w  w  w .  java2 s. c om*/

    }
}

From source file:Main.java

public static void main(String... args) {
    try {/* w ww.  ja  v  a2 s  .c  o m*/
        Class<?> c = Class.forName(args[0]);
        out.format("Class:%n  %s%n%n", c.getCanonicalName());
        out.format("Modifiers:%n  %s%n%n", Modifier.toString(c.getModifiers()));

        out.format("Type Parameters:%n");
        TypeVariable[] tv = c.getTypeParameters();
        if (tv.length != 0) {
            out.format("  ");
            for (TypeVariable t : tv)
                out.format("%s ", t.getName());
            out.format("%n%n");
        } else {
            out.format("  -- No Type Parameters --%n%n");
        }

        out.format("Implemented Interfaces:%n");
        Type[] intfs = c.getGenericInterfaces();
        if (intfs.length != 0) {
            for (Type intf : intfs)
                out.format("  %s%n", intf.toString());
            out.format("%n");
        } else {
            out.format("  -- No Implemented Interfaces --%n%n");
        }

        out.format("Inheritance Path:%n");
        List<Class> l = new ArrayList<Class>();
        printAncestor(c, l);
        if (l.size() != 0) {
            for (Class<?> cl : l)
                out.format("  %s%n", cl.getCanonicalName());
            out.format("%n");
        } else {
            out.format("  -- No Super Classes --%n%n");
        }

        out.format("Annotations:%n");
        Annotation[] ann = c.getAnnotations();
        if (ann.length != 0) {
            for (Annotation a : ann)
                out.format("  %s%n", a.toString());
            out.format("%n");
        } else {
            out.format("  -- No Annotations --%n%n");
        }

        // production code should handle this exception more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    }
}

From source file:Main.java

public static String getValueInAnntationList(Annotation[] annotations, String annClassName, String key) {
    if (annotations != null) {
        for (Annotation annotation : annotations) {
            String annName = annotation.toString();
            if (annName.indexOf(annClassName) != 0) {
                return getVlaueFromAnnotation(annotation, key);
            }/*from  www  . ja  v a 2 s  .co  m*/
        }
    }
    return "";
}

From source file:Main.java

public static String getVlaueFromAnnotation(Annotation annotation, String annName) {
    if (cache == null) {
        cache = new LruCache(500);
    }/*from  ww  w. j  a v  a 2  s .co  m*/
    String annotationString = annotation.toString();
    String cacheKey = "getVlaueByColumnAnnotation:" + annotationString.hashCode() + "," + annName;
    String ret = (String) cache.get(cacheKey);
    if (ret == null || "".equals(ret)) {
        String pattern = annName + "=(.*?),";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(annotation.toString());
        if (m.find()) {
            ret = m.group();
            ret = ret.substring(annName.length() + 1, ret.length() - 1);
        }
        cache.put(cacheKey, ret);
    }
    return ret;
}

From source file:com.github.gekoh.yagen.util.FieldInfo.java

private static String formatAnnotation(Annotation annotation) {
    String a = annotation.toString();
    StringBuilder result = new StringBuilder();

    // wrap string value of attribute "name" into double quotes as needed for java code
    Matcher m = STRING_ATTR_PATTERN.matcher(a);
    int idx = 0;//from   ww w .j  a v  a  2s .  c om
    while (m.find(idx)) {
        result.append(a.substring(idx, m.start(2)));
        result.append("\"").append(escapeAttributeValue(m.group(2))).append("\"");
        result.append(a.substring(m.end(2), m.end()));
        idx = m.end();
    }
    result.append(a.substring(idx));

    a = result.toString();
    result = new StringBuilder();

    // remove empty attributes like (columnDefinition=)
    m = Pattern.compile("\\(?(,?\\s*[A-Za-z]*=)[,|\\)]").matcher(a);
    idx = 0;
    while (m.find(idx)) {
        result.append(a.substring(idx, m.start(1)));
        idx = m.end(1);
    }
    result.append(a.substring(idx));

    // set nullable=true
    m = NULLABLE_PATTERN.matcher(result);
    idx = 0;
    while (m.find(idx)) {
        if (m.group(1).equals("false")) {
            result.replace(m.start(1), m.end(1), "true");
        }
        idx = m.start(1) + 1;
        m = NULLABLE_PATTERN.matcher(result);
    }

    // set unique=false
    m = UNIQUE_PATTERN.matcher(result);
    idx = 0;
    while (m.find(idx)) {
        if (m.group(1).equals("true")) {
            result.replace(m.start(1), m.end(1), "false");
        }
        idx = m.start(1) + 1;
        m = UNIQUE_PATTERN.matcher(result);
    }

    return result.toString().replaceAll("=\\[([^\\]]*)\\]", "={$1}");
}

From source file:io.fabric8.forge.rest.dto.UICommands.java

protected static boolean isJsonObject(Object value) {
    if (value != null) {
        Class<?> aClass = value.getClass();
        while (aClass != null && !aClass.equals(Object.class)) {
            Annotation[] annotations = aClass.getAnnotations();
            if (annotations != null) {
                for (Annotation annotation : annotations) {
                    String annotationClassName = annotation.getClass().getName();
                    if (annotationClassName != null
                            && annotationClassName.startsWith("com.fasterxml.jackson.")) {
                        // lets assume its a JSON DTO!
                        return true;
                    } else {
                        String text = annotation.toString();
                        // because of the Forge proxying we can't just use the actual class here...
                        if (text.indexOf("com.fasterxml.jackson.") >= 0) {
                            return true;
                        }/*from  w  w  w .  j a  va 2s . c  o m*/
                    }
                }
            }
            aClass = aClass.getSuperclass();
        }
    }
    return false;
}

From source file:com.all.app.DefaultContext.java

private void checkInvokePredestroy(Method method, Object ob) {
    boolean annotationPresent = false;
    boolean noArguments = method.getParameterTypes().length == 0;
    boolean isVoid = method.getReturnType().equals(void.class);
    boolean isPublic = (method.getModifiers() & Modifier.PUBLIC) == Modifier.PUBLIC;
    boolean isNotStatic = (method.getModifiers() & Modifier.STATIC) != Modifier.STATIC;
    Annotation[] annotations = method.getAnnotations();
    for (Annotation annotation : annotations) {
        if (annotation.getClass().getName().contains("PreDestroy")
                || annotation.toString().contains("PreDestroy")) {
            annotationPresent = true;/*www  .  ja  v  a2  s .co  m*/
        }
    }
    if (annotationPresent && noArguments && isPublic && isNotStatic && isVoid) {
        try {
            method.invoke(ob);
        } catch (Exception e) {
            log.error(e, e);
        }
    }

}

From source file:com.seleniumtests.core.aspects.LogAction.java

/**
 * Returns the value of cucumber annotation to get corresponding text
 * @param annotation/* w  w w. jav  a 2 s .  c o m*/
 * @return
 */
private String getAnnotationValue(Annotation annotation) {
    return annotation.toString().replaceFirst("timeout=\\d+", "")
            .replace("@" + annotation.annotationType().getCanonicalName() + "(", "")
            .replaceFirst(",?\\s?value=", "").replaceFirst("\\)$", "");
}

From source file:com.hiperium.dao.common.generic.GenericDAO.java

/**
 * Gets the columns that form the primary key of an entity.
 * //from   w ww  . j a  va 2 s . c  o m
 * @param object
 *            Entity that contains the primary fields.
 * @return the list of primary key fields.
 */
@SuppressWarnings("unchecked")
private <E> List<String> getIdFields(Object object) {
    Class<E> classe = (Class<E>) object.getClass();
    Field[] fields = classe.getDeclaredFields();
    List<String> primaryKeys = new ArrayList<String>();
    for (Field field : fields) {
        Annotation[] annotationArray = field.getAnnotations();
        for (Annotation annotation : annotationArray) {
            if ("@javax.persistence.Id()".equals(annotation.toString())) {
                primaryKeys.add(field.getName());
                break;
            } else if ("@javax.persistence.EmbeddedId()".equals(annotation.toString())) {
                String fieldName = field.getName();
                String methodName = "get" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
                try {
                    Method method = classe.getMethod(methodName, new Class[] {});
                    Object result = method.invoke(object, new Object[] {});
                    Class<E> classe1 = (Class<E>) result.getClass();
                    Field[] fields1 = classe1.getDeclaredFields();
                    for (Field fieldPK : fields1) {
                        if (!"serialVersionUID".equals(fieldPK.getName())) {
                            primaryKeys.add(fieldName + "." + fieldPK.getName());
                        }
                    }
                } catch (RuntimeException e) {
                    continue;
                } catch (NoSuchMethodException e) {
                    continue;
                } catch (IllegalAccessException e) {
                    continue;
                } catch (InvocationTargetException e) {
                    continue;
                }
            }
        }
    }
    return primaryKeys;
}