author | Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
Tue, 04 Dec 2012 14:49:01 +0100 | |
branch | reflection |
changeset 261 | 5d1e20215d12 |
parent 237 | 84ffc347412d |
child 264 | ed0c92c81ea4 |
permissions | -rw-r--r-- |
jaroslav@222 | 1 |
/** |
jaroslav@222 | 2 |
* Back 2 Browser Bytecode Translator |
jaroslav@222 | 3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
jaroslav@222 | 4 |
* |
jaroslav@222 | 5 |
* This program is free software: you can redistribute it and/or modify |
jaroslav@222 | 6 |
* it under the terms of the GNU General Public License as published by |
jaroslav@222 | 7 |
* the Free Software Foundation, version 2 of the License. |
jaroslav@222 | 8 |
* |
jaroslav@222 | 9 |
* This program is distributed in the hope that it will be useful, |
jaroslav@222 | 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
jaroslav@222 | 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
jaroslav@222 | 12 |
* GNU General Public License for more details. |
jaroslav@222 | 13 |
* |
jaroslav@222 | 14 |
* You should have received a copy of the GNU General Public License |
jaroslav@222 | 15 |
* along with this program. Look for COPYING file in the top folder. |
jaroslav@222 | 16 |
* If not, see http://opensource.org/licenses/GPL-2.0. |
jaroslav@222 | 17 |
*/ |
jaroslav@222 | 18 |
package org.apidesign.vm4brwsr; |
jaroslav@222 | 19 |
|
jaroslav@222 | 20 |
import java.io.IOException; |
jaroslav@237 | 21 |
import java.lang.annotation.Annotation; |
jaroslav@261 | 22 |
import java.lang.reflect.Method; |
jaroslav@222 | 23 |
import java.net.MalformedURLException; |
jaroslav@222 | 24 |
|
jaroslav@222 | 25 |
/** |
jaroslav@222 | 26 |
* |
jaroslav@222 | 27 |
* @author Jaroslav Tulach <jtulach@netbeans.org> |
jaroslav@222 | 28 |
*/ |
jaroslav@235 | 29 |
@ClassesMarker(number = 10) |
jaroslav@237 | 30 |
@ClassesNamer(name = "my text") |
jaroslav@222 | 31 |
public class Classes { |
jaroslav@222 | 32 |
public static boolean equalsClassesOfExceptions() { |
jaroslav@222 | 33 |
return MalformedURLException.class.getSuperclass() == IOException.class; |
jaroslav@222 | 34 |
} |
jaroslav@222 | 35 |
public static boolean differenceInClasses() { |
jaroslav@222 | 36 |
Class<?> c1 = MalformedURLException.class; |
jaroslav@222 | 37 |
Class<?> c2 = IOException.class; |
jaroslav@222 | 38 |
return c1 != c2; |
jaroslav@222 | 39 |
} |
jaroslav@222 | 40 |
|
jaroslav@225 | 41 |
public static String classForInstance() { |
jaroslav@225 | 42 |
return new IOException().getClass().getName().toString(); |
jaroslav@225 | 43 |
} |
jaroslav@225 | 44 |
|
jaroslav@261 | 45 |
@ClassesMarker(number = 1) |
jaroslav@222 | 46 |
public static String name() { |
jaroslav@223 | 47 |
return IOException.class.getName().toString(); |
jaroslav@222 | 48 |
} |
jaroslav@222 | 49 |
public static String simpleName() { |
jaroslav@222 | 50 |
return IOException.class.getSimpleName(); |
jaroslav@222 | 51 |
} |
jaroslav@222 | 52 |
public static String canonicalName() { |
jaroslav@222 | 53 |
return IOException.class.getCanonicalName(); |
jaroslav@222 | 54 |
} |
jaroslav@231 | 55 |
public static boolean newInstance() throws Exception { |
jaroslav@231 | 56 |
IOException ioe = IOException.class.newInstance(); |
jaroslav@231 | 57 |
if (ioe instanceof IOException) { |
jaroslav@231 | 58 |
return ioe.getClass() == IOException.class; |
jaroslav@231 | 59 |
} |
jaroslav@231 | 60 |
throw new IllegalStateException("Not a subtype: " + ioe); |
jaroslav@222 | 61 |
} |
jaroslav@235 | 62 |
public static int getMarker() { |
jaroslav@235 | 63 |
if (!Classes.class.isAnnotationPresent(ClassesMarker.class)) { |
jaroslav@235 | 64 |
return -2; |
jaroslav@235 | 65 |
} |
jaroslav@235 | 66 |
ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class); |
jaroslav@235 | 67 |
return cm == null ? -1 : cm.number(); |
jaroslav@235 | 68 |
} |
jaroslav@237 | 69 |
public static String getNamer(boolean direct) { |
jaroslav@237 | 70 |
if (direct) { |
jaroslav@237 | 71 |
ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class); |
jaroslav@237 | 72 |
return cm == null ? null : cm.name(); |
jaroslav@237 | 73 |
} |
jaroslav@237 | 74 |
for (Annotation a : Classes.class.getAnnotations()) { |
jaroslav@237 | 75 |
if (a instanceof ClassesNamer) { |
jaroslav@237 | 76 |
return ((ClassesNamer)a).name(); |
jaroslav@237 | 77 |
} |
jaroslav@237 | 78 |
} |
jaroslav@237 | 79 |
return null; |
jaroslav@237 | 80 |
} |
jaroslav@261 | 81 |
|
jaroslav@261 | 82 |
public static Object reflectiveMethodCall(boolean direct) throws Exception { |
jaroslav@261 | 83 |
Method m; |
jaroslav@261 | 84 |
/* |
jaroslav@261 | 85 |
if (!direct) { |
jaroslav@261 | 86 |
final Class<? extends Annotation> v = ClassesMarker.class; |
jaroslav@261 | 87 |
for (Method single : Classes.class.getMethods()) { |
jaroslav@261 | 88 |
if (single.getAnnotation(v)) { |
jaroslav@261 | 89 |
m = single; |
jaroslav@261 | 90 |
break; |
jaroslav@261 | 91 |
} |
jaroslav@261 | 92 |
} |
jaroslav@261 | 93 |
} else*/ { |
jaroslav@261 | 94 |
m = Classes.class.getMethod("name"); |
jaroslav@261 | 95 |
} |
jaroslav@261 | 96 |
return m.invoke(null); |
jaroslav@261 | 97 |
} |
jaroslav@222 | 98 |
} |