Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2013, Red Hat, Inc., and individual contributors
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * 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.weld.util.reflection;
 
 import static org.jboss.weld.util.reflection.Reflections.cast;
 
 import java.util.List;
 

Author(s):
Martin Kouba
 
 public final class DeclaredMemberIndexer {
 
 
     private static final MethodComparator METHOD_COMPARATOR_INSTANCE = new MethodComparator();
 
     private static final FieldComparator FIELD_COMPARATOR_INSTANCE = new FieldComparator();
 
     private DeclaredMemberIndexer() {
     }

    

Parameters:
field
Returns:
the index for the given field
 
     public static int getIndexForField(Field field) {
         Preconditions.checkNotNull(field);
         return getIndexForMember(fieldgetDeclaredFields(field.getDeclaringClass()));
     }

    

Parameters:
index
declaringClass
Returns:
the declared field for the given index and declaring class
Throws:
java.lang.IndexOutOfBoundsException if the index is out of range
 
     public static Field getFieldForIndex(int indexClass<?> declaringClass) {
         return getDeclaredFields(declaringClass).get(index);
     }

    

Parameters:
method
Returns:
the index for the given method
 
     public static int getIndexForMethod(Method method) {
         Preconditions.checkNotNull(method);
         return getIndexForMember(methodgetDeclaredMethods(method.getDeclaringClass()));
     }

    

Parameters:
index
declaringClass
Returns:
the declared method for the given index and declaring class
Throws:
java.lang.IndexOutOfBoundsException if the index is out of range
 
     public static Method getMethodForIndex(int indexClass<?> declaringClass) {
         return getDeclaredMethods(declaringClass).get(index);
     }

    

Parameters:
constructor
Returns:
the index for the given constructor
 
     public static int getIndexForConstructor(Constructor<?> constructor) {
         Preconditions.checkNotNull(constructor);
         return getIndexForMember(constructorgetDeclaredConstructors(constructor.getDeclaringClass()));
     }

    

Parameters:
index
declaringClass
Returns:
the declared constructor for the given index and declaring class
Throws:
java.lang.IndexOutOfBoundsException if the index is out of range
    public static <T> Constructor<T> getConstructorForIndex(int indexClass<T> declaringClass) {
        return cast(getDeclaredConstructors(declaringClass).get(index));
    }
    private static <T extends Memberint getIndexForMember(T declaredMemberList<T> declaredMembers) {
        for (ListIterator<T> iterator = declaredMembers.listIterator(); iterator.hasNext();) {
            T member = iterator.next();
            if (member.equals(declaredMember)) {
                return iterator.previousIndex();
            }
        }
        // This should never happen
        throw new IllegalStateException("No matching declared member found for: " + declaredMember);
    }

    

Parameters:
declaringClass
Returns:
the ordered list of declared fields for the given class
    public static List<FieldgetDeclaredFields(Class<?> declaringClass) {
        Preconditions.checkNotNull(declaringClass);
        List<FielddeclaredFields = Arrays.asList(declaringClass.getDeclaredFields());
        Collections.sort(declaredFields);
        return declaredFields;
    }

    

Parameters:
declaringClass
Returns:
the ordered list of declared methods for the given class
    public static List<MethodgetDeclaredMethods(Class<?> declaringClass) {
        Preconditions.checkNotNull(declaringClass);
        List<MethoddeclaredMethods = Arrays.asList(declaringClass.getDeclaredMethods());
        Collections.sort(declaredMethods);
        return declaredMethods;
    }

    

Parameters:
declaringClass
Returns:
the ordered list of declared constructors for the given class
    public static List<Constructor<?>> getDeclaredConstructors(Class<?> declaringClass) {
        Preconditions.checkNotNull(declaringClass);
        List<Constructor<?>> declaredConstructors = Arrays.asList(declaringClass.getDeclaredConstructors());
        Collections.sort(declaredConstructors);
        return declaredConstructors;
    }
    private static int compareParamTypes(Class<?>[] paramTypes1Class<?>[] paramTypes2) {
        // First compare the number of parameters
        if (paramTypes1.length != paramTypes2.length) {
            return paramTypes1.length - paramTypes2.length;
        }
        // Then compare the FCQNs of the parameter types
        for (int i = 0; i < paramTypes1.lengthi++) {
            if (!paramTypes1[i].getName().equals(paramTypes2[i].getName())) {
                return paramTypes1[i].getName().compareTo(paramTypes2[i].getName());
            }
        }
        // This should never happen - constructors cannot have the same param
        // types and methods are ordered by name first
        return 0;
    }
    private static class ConstructorComparator implements Comparator<Constructor<?>>, Serializable {
        private static final long serialVersionUID = 4694814949925290433L;
        @Override
        public int compare(Constructor<?> c1Constructor<?> c2) {
            return compareParamTypes(c1.getParameterTypes(), c2.getParameterTypes());
        }
    }
    private static class MethodComparator implements Comparator<Method>, Serializable {
        private static final long serialVersionUID = -2254993285161908832L;
        @Override
        public int compare(Method m1Method m2) {
            // First compare the names
            if (!m1.getName().equals(m2.getName())) {
                return m1.getName().compareTo(m2.getName());
            }
            // Then compare the parameters
            return compareParamTypes(m1.getParameterTypes(), m2.getParameterTypes());
        }
    }
    private static class FieldComparator implements Comparator<Field>, Serializable {
        private static final long serialVersionUID = -1417596921060498760L;
        @Override
        public int compare(Field o1Field o2) {
            return o1.getName().compareTo(o2.getName());
        }
    }
New to GrepCode? Check out our FAQ X