Java Code Examples for java.lang.reflect.Constructor#getExceptionTypes()
The following examples show how to use
java.lang.reflect.Constructor#getExceptionTypes() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConstructorTest.java From j2objc with Apache License 2.0 | 6 votes |
public void test_getExceptionTypes() { // Test for method java.lang.Class [] // java.lang.reflect.Constructor.getExceptionTypes() Class[] exceptions = null; Class<? extends IndexOutOfBoundsException> ex = null; try { Constructor<? extends ConstructorTestHelper> ctor = new ConstructorTestHelper().getClass() .getConstructor(new Class[0]); exceptions = ctor.getExceptionTypes(); ex = new IndexOutOfBoundsException().getClass(); } catch (Exception e) { fail("Exception during test : " + e.getMessage()); } assertEquals("Returned exception list of incorrect length", 1, exceptions.length); assertTrue("Returned incorrect exception", exceptions[0].equals(ex)); }
Example 2
Source File: GenericSignatureParser.java From j2objc with Apache License 2.0 | 6 votes |
/** * Parses the generic signature of a constructor and creates the data * structure representing the signature. * * @param genericDecl the GenericDeclaration calling this method * @param signature the generic signature of the class */ public void parseForConstructor(GenericDeclaration genericDecl, String signature, Class<?>[] rawExceptionTypes) { setInput(genericDecl, signature); if (!eof) { parseMethodTypeSignature(rawExceptionTypes); } else { Constructor c = (Constructor) genericDecl; this.formalTypeParameters = EmptyArray.TYPE_VARIABLE; Class<?>[] parameterTypes = c.getParameterTypes(); if (parameterTypes.length == 0) { this.parameterTypes = ListOfTypes.EMPTY; } else { this.parameterTypes = new ListOfTypes(parameterTypes); } Class<?>[] exceptionTypes = c.getExceptionTypes(); if (exceptionTypes.length == 0) { this.exceptionTypes = ListOfTypes.EMPTY; } else { this.exceptionTypes = new ListOfTypes(exceptionTypes); } } }
Example 3
Source File: ClassDescription.java From ldp4j with Apache License 2.0 | 6 votes |
@Override protected void processType(Constructor<?> type) { super.addTitle("Constructor"); super.addBlockField("member", wrapElementAs(type,Member.class)); super.addBlockField("generic declaration", wrapElementAs(type,GenericDeclaration.class)); super.addBlockField("annotated element", wrapElementAs(type,AnnotatedElement.class)); super.addField("accessible", type.isAccessible()); super.addMultiField("parameter types",type.getParameterTypes()); super.addMultiField("generic parameter types",type.getGenericParameterTypes()); Annotation[][] parameterAnnotations = type.getParameterAnnotations(); List<String> annots=new ArrayList<String>(); for(Annotation[] pa:parameterAnnotations) { annots.add(Arrays.toString(pa)); } super.addMultiField("parameter annotations",annots); super.addField("var args",type.isVarArgs()); super.addMultiField("exception types",type.getExceptionTypes()); super.addMultiField("generic exception types",type.getGenericExceptionTypes()); }
Example 4
Source File: ConstructorTest.java From j2objc with Apache License 2.0 | 5 votes |
public void test_getExceptionTypes() throws Exception { Constructor<?> constructor = ConstructorTestHelper.class.getConstructor(new Class[0]); Class[] exceptions = constructor.getExceptionTypes(); assertEquals(1, exceptions.length); assertEquals(IndexOutOfBoundsException.class, exceptions[0]); // Check that corrupting our array doesn't affect other callers. exceptions[0] = NullPointerException.class; exceptions = constructor.getExceptionTypes(); assertEquals(1, exceptions.length); assertEquals(IndexOutOfBoundsException.class, exceptions[0]); }
Example 5
Source File: DynamicSubclass.java From tomee with Apache License 2.0 | 5 votes |
private static MethodVisitor visitConstructor(final ClassWriter cw, final String proxyClassFileName, final String classFileName, final Constructor<?> constructor) { final String descriptor = Type.getConstructorDescriptor(constructor); final String[] exceptions = new String[constructor.getExceptionTypes().length]; for (int i = 0; i < exceptions.length; i++) { exceptions[i] = Type.getInternalName(constructor.getExceptionTypes()[i]); } final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", descriptor, null, exceptions); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); int index = 1; for (final Type type : Type.getArgumentTypes(descriptor)) { mv.visitVarInsn(type.getOpcode(ILOAD), index); index += size(type); } mv.visitMethodInsn(INVOKESPECIAL, classFileName, "<init>", descriptor, false); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(PUTFIELD, proxyClassFileName, "this$handler", "Ljava/lang/reflect/InvocationHandler;"); mv.visitInsn(RETURN); mv.visitMaxs(2, 1); return mv; }
Example 6
Source File: FactoryTestGenerator.java From netbeans with Apache License 2.0 | 4 votes |
private static String createMethods(String clsName, Constructor constructor) { String sname = clsName.substring(4); // Cut "Tree" Class[] params = constructor.getParameterTypes(); HashSet set = new HashSet(); String header = ""; String atrs = ""; String res = ""; // create header declaration an attr. list for (int i = 0; i < params.length; i++) { String pType = params[i].getName(); String pName; if (params[i].isPrimitive()) { pName = pType + "_val"; } else { pName = pType.substring(pType.lastIndexOf('.') + 1).toLowerCase(); } if (set.contains(pName)) { pName += i; } set.add(pName); header += pType + " " + pName; atrs += pName; if (i < params.length - 1) { header += ", "; atrs += ", "; } } // create() method res += "\n" + " static Tree" + sname + " create" + sname + "(" + header + ", String view) throws Exception {\n" + " Tree" + sname + " node = new Tree" + sname + "(" + atrs + ");\n" + " \n" + " assertEquals(node, view);\n" + " cloneNodeTest(node, view);\n" + " return node;\n" + " }\n"; Class[] exs = constructor.getExceptionTypes(); // if constructor throw InvalidArgumentException create method createInvalid() for (int i = 0; i < exs.length; i++) { if (InvalidArgumentException.class.isAssignableFrom(exs[i])) { res += "\n" + " static void create" + sname + "Invalid(" + header + ") throws Exception {\n" + " try {\n" + " new Tree" + sname + "(" + atrs + ");\n" + " // Fail if previous line doesn't trhow exception.\n" + " fail(NOT_EXCEPTION + \"from: new Tree" + sname +"(" + atrs + ")\");\n" + " } catch (InvalidArgumentException e) {\n" + " // OK\n" + " }\n" + " }\n"; break; } } return res; }
Example 7
Source File: ProxyGenerator.java From component-runtime with Apache License 2.0 | 4 votes |
private String createConstructor(final ClassWriter cw, final Class<?> classToProxy, final String classFileName, final String proxyClassFileName, final Constructor<?> constructor, final boolean withInterceptors) { try { Constructor superDefaultCt; String parentClassFileName; String[] exceptions = null; if (classToProxy.isInterface()) { parentClassFileName = Type.getInternalName(Object.class); superDefaultCt = Object.class.getConstructor(); } else { parentClassFileName = classFileName; if (constructor == null) { superDefaultCt = classToProxy.getConstructor(); } else { superDefaultCt = constructor; Class<?>[] exceptionTypes = constructor.getExceptionTypes(); exceptions = exceptionTypes.length == 0 ? null : new String[exceptionTypes.length]; for (int i = 0; i < exceptionTypes.length; i++) { exceptions[i] = Type.getInternalName(exceptionTypes[i]); } } } final String descriptor = Type.getConstructorDescriptor(superDefaultCt); final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", descriptor, null, exceptions); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); if (constructor != null) { for (int i = 1; i <= constructor.getParameterTypes().length; i++) { mv.visitVarInsn(ALOAD, i); } } mv.visitMethodInsn(INVOKESPECIAL, parentClassFileName, "<init>", descriptor, false); if (withInterceptors) { mv.visitVarInsn(ALOAD, 0); mv.visitInsn(ACONST_NULL); mv .visitFieldInsn(PUTFIELD, proxyClassFileName, FIELD_INTERCEPTOR_HANDLER, Type.getDescriptor(InterceptorHandler.class)); } mv.visitInsn(RETURN); mv.visitMaxs(-1, -1); mv.visitEnd(); return parentClassFileName; } catch (final NoSuchMethodException e) { throw new IllegalStateException(e); } }
Example 8
Source File: Hack.java From MaterialPreference with Apache License 2.0 | 4 votes |
private @Nullable Invokable<C> findInvokable(final Class<?>... param_types) { if (mClass == ANY_TYPE) // AnyType as a internal indicator for class not found. return mHasFallback ? new FallbackInvokable<C>(mFallbackReturnValue) : null; final int modifiers; Invokable<C> invokable; final AccessibleObject accessible; final Class<?>[] ex_types; try { if (mName != null) { final Method candidate = mClass.getDeclaredMethod(mName, param_types); Method method = candidate; ex_types = candidate.getExceptionTypes(); modifiers = method.getModifiers(); if (Modifier.isStatic(mModifiers) != Modifier.isStatic(candidate.getModifiers())) { fail(new AssertionException(candidate + (Modifier.isStatic(mModifiers) ? " is not static" : "is static")).setHackedMethod(method)); method = null; } if (mReturnType != null && mReturnType != ANY_TYPE && !candidate.getReturnType().equals(mReturnType)) { fail(new AssertionException("Return type mismatch: " + candidate)); method = null; } if (method != null) { invokable = new InvokableMethod<>(method); accessible = method; } else { invokable = null; accessible = null; } } else { final Constructor<C> ctor = mClass.getDeclaredConstructor(param_types); modifiers = ctor.getModifiers(); invokable = new InvokableConstructor<>(ctor); accessible = ctor; ex_types = ctor.getExceptionTypes(); } } catch (final NoSuchMethodException e) { fail(new AssertionException(e).setHackedClass(mClass).setHackedMethodName(mName).setParamTypes(param_types)); return mHasFallback ? new FallbackInvokable<C>(mFallbackReturnValue) : null; } if (mModifiers > 0 && (modifiers & mModifiers) != mModifiers) fail(new AssertionException(invokable + " does not match modifiers: " + mModifiers).setHackedMethodName(mName)); if (mThrowTypes == null && ex_types.length > 0 || mThrowTypes != null && ex_types.length == 0) { fail(new AssertionException("Checked exception(s) not match: " + invokable)); if (ex_types.length > 0) invokable = null; // No need to fall-back if expected checked exceptions are absent. } else if (mThrowTypes != null) { Arrays.sort(ex_types, CLASS_COMPARATOR); if (!Arrays.equals(ex_types, mThrowTypes)) { // TODO: Check derived relation of exceptions fail(new AssertionException("Checked exception(s) not match: " + invokable)); invokable = null; } } if (invokable == null) { if (!mHasFallback) return null; return new FallbackInvokable<>(mFallbackReturnValue); } if (!accessible.isAccessible()) accessible.setAccessible(true); return invokable; }