Just got curious to see what is the longest class name in Java JDK bundle and extended that curiosity to method and attribute names as well.
Wrote a tiny Java program to load all classes, their methods and attributes from a Jar to print their name to a file. Then ran that on JDK 1.6 (rt.jar) and got the following results.
Hope you are following naming conventions strictly in your projects. I welcome you to run this below program on your project or your favorite packages like Spring / Hibernate etc and share your interesting findings in the comments.
This Java program will print all the class, method and attribute names from the Jar file, sorted in descending order of length.
package com.javapapers.java; import java.io.File; import java.io.PrintWriter; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Enumeration; import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarFile; public class LongNames { public static void main(String[] args) throws Exception { String javaFile = "C:\\Program Files\\Java\\jdk1.6.0_24\\jre\\lib\\rt.jar"; ListclassList = new ArrayList (); List methodList = new ArrayList (); List attributeList = new ArrayList (); System.out.println("Processing..."); getList(new File(javaFile), classList, methodList, attributeList); sort(classList); print(classList, new PrintWriter("ClassList.txt")); System.out.println("Class list complete."); sort(methodList); print(methodList, new PrintWriter("MethodList.txt")); System.out.println("Method list complete."); sort(attributeList); print(attributeList, new PrintWriter("AttributeList.txt")); System.out.println("Attribute list complete."); } private static void getList(File file, List classList, List methodList, List attributeList) throws Exception { Enumeration jarEntries = new JarFile(file).entries(); while (jarEntries.hasMoreElements()) { JarEntry jarFile = jarEntries.nextElement(); String className = jarFile.getName(); if (className.endsWith(".class")) { className = className.replaceAll("\\.class", ""); className = className.replaceAll("/", "."); try { if (className != null && !className.isEmpty()) { Class clazz = Class.forName(className, false, ClassLoader.getSystemClassLoader()); // inner class hack if (clazz.getSimpleName().length() > 0) { classList .add(new Content(clazz.getSimpleName(), "")); getMethodList(clazz, methodList); getAttributeList(clazz, attributeList); } } } catch (Exception e) { System.out.println(e); } } } } private static void getMethodList(Class clazz, List methodList) { Method[] methods = clazz.getMethods(); for (Method method : methods) { methodList .add(new Content(method.getName(), clazz.getSimpleName())); } } private static void getAttributeList(Class clazz, List attributeList) { Field[] fields = clazz.getFields(); for (Field field : fields) { attributeList.add(new Content(field.getName(), clazz .getSimpleName())); } } private static void sort(List nameList) { Collections.sort(nameList, new Comparator () { public int compare(Content c1, Content c2) { //descending return c2.content.length() - c1.content.length(); } }); } private static void print(List list, PrintWriter file) { for (Content str : list) { file.println(str.content.length() + "\t" + str.content + "\t" + str.className); } file.close(); } } class Content { public Content(String content, String className) { this.content = content; this.className = className; } String content; String className; }
Comments are closed for "Longest Class, Method and Attribute Names in Java".
According to the naming conventions, its good to follow this.
But my doubt is why they have restricted to this specific length?
Is there any reason behind this?
Thanks,
Nagendra.
92 chars for class name,it is restricted,we can not go beyond to it.if this is restricted how did you calculate it?Is class name length dependent on JDK version?
One suggestion, your site template has very limited space for text/code. This makes difficult to scroll sidewise to understand code.
I appreciate your posts and interest.
No, its not a restriction.
No!! There is no such restriction. Is the content misleading?
Yes Praveen, thats right. Presently, I am working menu redesign and I will fix this also along with that.
I executed the above program.Works perfect..Good one Joe..
Nice Tutorials are here…..
Hi Joe
I tried to find in javadocs this class InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonWindowNotFocusedState. I see none. Why? What is the use of this class?
Regards
Jinu
It worked but got different results , I made some changes I used rt.jar which is in jre7 library as I don’t have java installed.
Thanks
mam cant run this file why,if so getting error
why this is working in jar file only,