0

In Java, I have defined an interface MyInterfaceName [MyInterfaceName.class]. This .class file is available inside a jarFile. Using Java Reflection, I am trying to use the method, class.isInterface() in this particular class, but it returns false.

Again, based upon the following references,

http://tutorials.jenkov.com/java-reflection/classes.html#modifiers http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Modifier.html#isInterface(int)

ModifierValueLink

I tried the following code:

int modifierValue = MyInterfaceName.class.getModifiers();
System.out.println(modifierValue)

boolean interfaceFound = Modifier.isInterface(modifierValue);
System.out.println(interfaceFound)

This interfaceFound returns only false. Also, modifierValue returns 17, which doesn't fit anywhere in the ModifierValueLink I have provided.

But MyInterfaceName is an interface defined according to java standards. I couldn't understand why it doesn't work.

Please help out. Thanks.

uma
  • 327
  • 2
  • 19
  • 17 = 16 + 1 (public final) - can you show the code? – assylias May 09 '14 at 10:58
  • You should use the class, not the name. (Hard to tell ifyou don't post actual code). Something like `MyInterfaceName.class.getModifiers()` should work. – M. Deinum May 09 '14 at 10:58
  • @assylias: yes even i guessed the same. but what about the interface? M.Deinum: from the very beginning am using the same what you have mentioned. I will post the code. – uma May 09 '14 at 11:02
  • That 17 is very suspect - if it really is an interface then it shouldn't be `final` – Ian Roberts May 09 '14 at 11:10
  • @M.Deinum: As i am getting the class file from a jar file, am using this same methodology [this](http://stackoverflow.com/a/11016594/2530599) guy follows. Once i get the jarEntry, i do the following: `boolean interfaceFound = jarEntry.getName().getClass().isInterface()` But it returns false :( – uma May 09 '14 at 11:14
  • 2
    That gets a `String` and not your class. – M. Deinum May 09 '14 at 11:16
  • The value of `getModifiers()` is equal to 17 in case of `public final` class, or `public final` method or field. Post the `MyInterfaceName` definition, it is definitely not `interface MyInterfaceName`. – Oleg Estekhin May 09 '14 at 11:16
  • shouldn't it be MyInterfaceName.class.getModifiers(); ? You're calling the getModifiers() on the wrong object – EasterBunnyBugSmasher May 09 '14 at 11:17
  • Sorry. thats `jarEntry.getClass().isInterface()`. – uma May 09 '14 at 11:25
  • am trying only `MyInterfaceName.class.getModifiers();` sorry for the typo. – uma May 09 '14 at 11:26

2 Answers2

1

My new answer is on this link. http://java2s.com/Tutorials/Java/java.lang.reflect/Modifier/Java_Modifier_isInterface_int_mod_.htm

I have Implement the given exapmle at the link;

public interface MyInterfaceName {
    public int a = 0;
    public int b = 0;
}

import java.lang.reflect.Modifier;


public class Main {

    public static void main(String[] args) {

        Class<?> cls = MyInterfaceName.class;

        int modifier = cls.getModifiers();

        System.out.println(Modifier.isInterface(modifier));

    }
}

This gives true. The coorect usage should be as at the link.

xxlali
  • 996
  • 2
  • 15
  • 43
  • am using another way from Reflection. It gets the modifiers of that class. Please have a look at this link. [Get the class modifiers using reflection](http://tutorials.jenkov.com/java-reflection/classes.html#modifiers) – uma May 09 '14 at 11:06
  • Link-only answers aren't really how SO wants to work. – Dave Newton May 09 '14 at 11:17
  • yes it returns an int value. This int would describe the type of modifier. For Interface it is 512. Yet it returns 17. :| – uma May 09 '14 at 11:19
0

Finaly got it :)

url = // Your jar file path
JarFile jarFile = new JarFile(url);
Enumeration e = jarFile.entries();
URLClassLoader urlClassLoader;
URL[] urls = { 
    new URL("jar:file:" + url +"!/") 
};
urlClassLoader = URLClassLoader.newInstance(urls);
while (e.hasMoreElements()) {
    JarEntry je = (JarEntry) e.nextElement();
    if(je.isDirectory() || !je.getName().endsWith(".class")){
       continue;
    }   
    String className = je.getName().substring(0,je.getName().length()-6);       
    className = className.replace('/', '.');            
    Class myClass = urlClassLoader.loadClass(className);
    if(myClass.isInterface()){
        System.out.println("Interface found - Name: "+ myClass);
    }
}

Thanks for all those who have replied :) and lol for the downvoter(s).

VirtualTroll
  • 3,077
  • 1
  • 30
  • 47
uma
  • 327
  • 2
  • 19