Example usage for java.lang.reflect Field getName

List of usage examples for java.lang.reflect Field getName

Introduction

In this page you can find the example usage for java.lang.reflect Field getName.

Prototype

public String getName() 

Source Link

Document

Returns the name of the field represented by this Field object.

Usage

From source file:Main.java

public static void main(String[] args) throws IllegalAccessException {
    Class clazz = Color.class;
    Field[] colorFields = clazz.getDeclaredFields();

    HashMap<String, Color> singleColors = new HashMap<String, Color>();
    for (Field cf : colorFields) {
        int modifiers = cf.getModifiers();
        if (!Modifier.isPublic(modifiers))
            continue;

        Color c = (Color) cf.get(null);
        if (!singleColors.values().contains(c))
            singleColors.put(cf.getName(), c);
    }/*from  w w w .  j  a  v a 2 s.  c  o  m*/

    for (String k : singleColors.keySet()) {
        System.out.println(k + ": " + singleColors.get(k));
    }
}

From source file:ArrayFind.java

public static void main(String... args) {
    boolean found = false;
    try {//from   w  w w.ja  v  a2  s  . c o m
        Class<?> cls = Class.forName(args[0]);
        Field[] flds = cls.getDeclaredFields();
        for (Field f : flds) {
            Class<?> c = f.getType();
            if (c.isArray()) {
                found = true;
                out.format(
                        "%s%n" + "           Field: %s%n" + "            Type: %s%n" + "  Component Type: %s%n",
                        f, f.getName(), c, c.getComponentType());
            }
        }
        if (!found) {
            out.format("No array fields%n");
        }

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

From source file:Spy.java

public static void main(String... args) {
    try {/*from   w  w w  . jav  a  2  s. co  m*/
        Class<?> c = Class.forName(args[0]);
        int searchMods = 0x0;
        for (int i = 1; i < args.length; i++) {
            searchMods |= modifierFromString(args[i]);
        }

        Field[] flds = c.getDeclaredFields();
        out.format("Fields in Class '%s' containing modifiers:  %s%n", c.getName(),
                Modifier.toString(searchMods));
        boolean found = false;
        for (Field f : flds) {
            int foundMods = f.getModifiers();
            // Require all of the requested modifiers to be present
            if ((foundMods & searchMods) == searchMods) {
                out.format("%-8s [ synthetic=%-5b enum_constant=%-5b ]%n", f.getName(), f.isSynthetic(),
                        f.isEnumConstant());
                found = true;
            }
        }

        if (!found) {
            out.format("No matching fields%n");
        }

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

From source file:com.edgenius.wiki.installation.SilenceInstall.java

public static void main(String[] args) throws FileNotFoundException, IOException, NumberFormatException,
        IllegalArgumentException, IllegalAccessException, InvocationTargetException {

    if (args.length != 1) {
        System.out.println("Usage: SilenceInstall silence-install.properites");
        System.exit(1);/*from  w ww .j av a2 s. c o  m*/
        return;
    }
    if (!(new File(args[0]).exists())) {
        System.out.println("Given silence-install.properites not found: [" + args[0] + "]");
        System.exit(1);
        return;
    }
    SilenceInstall silence = new SilenceInstall();
    Properties prop = new Properties();
    prop.load(new FileInputStream(args[0]));

    log.info("Silence installation starting... on properties: {}", args[0]);

    if (Boolean.parseBoolean(getProperty(prop, "data.root.in.system.property"))) {
        System.setProperty(DataRoot.rootKey, getProperty(prop, "data.root"));
        log.info("Date root is set to System Properties {}", getProperty(prop, "data.root"));
    }
    try {
        Field[] flds = Class.forName(Server.class.getName()).getDeclaredFields();
        for (Field field : flds) {
            serverFields.add(field.getName());
        }
        flds = Class.forName(GlobalSetting.class.getName()).getDeclaredFields();
        for (Field field : flds) {
            globalFields.add(field.getName());
        }
        flds = Class.forName(Installation.class.getName()).getDeclaredFields();
        for (Field field : flds) {
            installFields.add(field.getName());
        }
    } catch (Exception e) {
        log.error("Load fields name failed", e);
        System.exit(1);
    }

    boolean succ = silence.createDataRoot(getProperty(prop, "data.root"));
    if (!succ) {
        log.error("Unable to complete create data root");
        return;
    }
    //detect if Install.xml exist and if it is already installed.
    File installFile = FileUtil.getFile(DataRoot.getDataRoot() + Installation.FILE);
    if (installFile.exists()) {
        Installation install = Installation.refreshInstallation();
        if (Installation.STATUS_COMPLETED.equals(install.getStatus())) {
            log.info("GeniusWiki is already installed, exit this installation.");
            System.exit(0);
        }
    }

    //load Server.properties, Global.xml and Installation.properties
    Server server = new Server();
    Properties serverProp = FileUtil.loadProperties(Server.FILE_DEFAULT);
    server.syncFrom(serverProp);
    GlobalSetting global = GlobalSetting
            .loadGlobalSetting(FileUtil.getFileInputStream(Global.DEFAULT_GLOBAL_XML));
    Installation install = Installation.loadDefault();

    //sync values from silence-install.properites
    silence.sync(prop, server, global, install);

    //install....
    succ = silence.setupDataRoot(server, global, install);
    if (!succ) {
        log.error("Unable to complete save configuration files to data root");
        return;
    }

    if (Boolean.parseBoolean(getProperty(prop, "create.database"))) {
        succ = silence.createDatabase(server, getProperty(prop, "database.root.username"),
                getProperty(prop, "database.root.password"));
        if (!succ) {
            log.error("Unable to complete create database");
            return;
        }
    }

    succ = silence.createTable(server);
    if (!succ) {
        log.error("Unable to complete create tables");
        return;
    }
    succ = silence.createAdministrator(server, getProperty(prop, "admin.fullname"),
            getProperty(prop, "admin.username"), getProperty(prop, "admin.password"),
            getProperty(prop, "admin.email"));
    if (!succ) {
        log.error("Unable to complete create administrator");
        return;
    }

    log.info("Silence installation completed successfully.");
}

From source file:org.syncope.hibernate.HibernateEnhancer.java

public static void main(final String[] args) throws Exception {

    if (args.length != 1) {
        throw new IllegalArgumentException("Expecting classpath as single argument");
    }//from w w  w . j a v  a  2  s .c  om

    ClassPool classPool = ClassPool.getDefault();
    classPool.appendClassPath(args[0]);

    PathMatchingResourcePatternResolver resResolver = new PathMatchingResourcePatternResolver(
            classPool.getClassLoader());
    CachingMetadataReaderFactory cachingMetadataReaderFactory = new CachingMetadataReaderFactory();

    for (Resource resource : resResolver.getResources("classpath*:org/syncope/core/**/*.class")) {

        MetadataReader metadataReader = cachingMetadataReaderFactory.getMetadataReader(resource);
        if (metadataReader.getAnnotationMetadata().isAnnotated(Entity.class.getName())) {

            Class entity = Class.forName(metadataReader.getClassMetadata().getClassName());
            classPool.appendClassPath(new ClassClassPath(entity));
            CtClass ctClass = ClassPool.getDefault().get(entity.getName());
            if (ctClass.isFrozen()) {
                ctClass.defrost();
            }
            ClassFile classFile = ctClass.getClassFile();
            ConstPool constPool = classFile.getConstPool();

            for (Field field : entity.getDeclaredFields()) {
                if (field.isAnnotationPresent(Lob.class)) {
                    AnnotationsAttribute typeAttr = new AnnotationsAttribute(constPool,
                            AnnotationsAttribute.visibleTag);
                    Annotation typeAnnot = new Annotation("org.hibernate.annotations.Type", constPool);
                    typeAnnot.addMemberValue("type",
                            new StringMemberValue("org.hibernate.type.StringClobType", constPool));
                    typeAttr.addAnnotation(typeAnnot);

                    CtField lobField = ctClass.getDeclaredField(field.getName());
                    lobField.getFieldInfo().addAttribute(typeAttr);
                }
            }

            ctClass.writeFile(args[0]);
        }
    }
}

From source file:Main.java

private static boolean isFieldsEqual(Field one, Field two) {
    return one.getName().equals(two.getName()) && one.getType().equals(two);
}

From source file:Main.java

private static Field findFieldByName(Field[] fields, String name) {
    for (Field field : fields) {
        if (field.getName().equals(name)) {
            return field;
        }/*from  w  w  w.ja  v  a 2  s . c o m*/
    }
    return null;
}

From source file:Main.java

public static Method getAppMethod(Class cls, Field field) throws Exception {
    char[] ca = field.getName().toCharArray();
    ca[0] = Character.toUpperCase(ca[0]);
    return cls.getMethod("set" + new String(ca), field.getType());
}

From source file:Main.java

public static ArrayList<String> getFields(Class<?> cls) {
    ArrayList<String> fieldList = new ArrayList<String>();
    Field[] fields = cls.getDeclaredFields();
    for (Field f : fields) {
        fieldList.add(f.getName());
    }/*ww w  .  ja  va2 s  . co  m*/
    return fieldList;
}

From source file:Main.java

public static void printFields(Class cl) {
    Field[] fields = cl.getDeclaredFields();

    for (int i = 0; i < fields.length; i++) {
        Field f = fields[i];
        Class type = f.getType();
        String name = f.getName();
        System.out.print(Modifier.toString(f.getModifiers()));
        System.out.println(" " + type.getName() + " " + name + ";");
    }/*  w w  w.  ja  v a2  s.  c  om*/
}