/*******************************************************************************
 * Copyright (c) Contributors to the Eclipse Foundation
 *
 * 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.
 *
 * SPDX-License-Identifier: Apache-2.0 
 *******************************************************************************/

package org.osgi.util.converter;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * @author $Id$
 */
class DTOUtil {
	private DTOUtil() {
		// Do not instantiate. This is a utility class.
	}

	static boolean isDTOType(Class< ? > cls, boolean ignorePublicNoArgsCtor) {
		if (!ignorePublicNoArgsCtor) {
			try {
				cls.getConstructor();
			} catch (NoSuchMethodException | SecurityException e) {
				// No public zero-arg constructor, not a DTO
				return false;
			}
		}

		for (Method m : cls.getMethods()) {
			try {
				Object.class.getMethod(m.getName(), m.getParameterTypes());
			} catch (NoSuchMethodException snme) {
				// Not a method defined by Object.class (or override of such
				// method)
				return false;
			}
		}

		/*
		 * for (Field f : cls.getDeclaredFields()) { int modifiers =
		 * f.getModifiers(); if (Modifier.isStatic(modifiers)) { // ignore
		 * static fields continue; } if (!Modifier.isPublic(modifiers)) { return
		 * false; } }
		 */

		boolean foundField = false;
		for (Field f : cls.getFields()) {
			int modifiers = f.getModifiers();
			if (Modifier.isStatic(modifiers)) {
				// ignore static fields
				continue;
			}

			if (!Modifier.isPublic(modifiers)) {
				return false;
			}
			foundField = true;
		}
		return foundField;
	}
}

@@ -848,16 +848,54 @@
 erter;%0A%0A
+import java.lang.reflect.Constructor;%0A
 import j
@@ -985,16 +985,41 @@
 odifier;
+%0Aimport java.util.Arrays;
 %0A%0A/**%0A *
@@ -1052,16 +1052,102 @@
 OUtil %7B%0A
+%09private static final Method%5B%5D OBJECT_CLASS_METHODS = Object.class%0A%09%09%09.getMethods();%0A%0A
 %09private
@@ -1325,26 +1325,85 @@
 r) %7B%0A%09%09%09
-try %7B%0A%09%09%09%09
+boolean hasPublicNoargsCtor = false;%0A%09%09%09for (Constructor%3C ? %3E ctor : 
 cls.getC
@@ -1416,75 +1416,60 @@
 ctor
+s
 ()
-;
+) %7B
 %0A%09%09%09
-%7D catch (NoSuchMethodException %7C SecurityException e
+%09if (ctor.getParameterCount() == 0
 ) %7B%0A%09%09%09%09
 // N
@@ -1468,52 +1468,90 @@
 %09%09%09%09
-// No public zero-arg constructor, not a DTO
+%09hasPublicNoargsCtor = true;%0A%09%09%09%09%09break;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09if (!hasPublicNoargsCtor) %7B
 %0A%09%09%09
@@ -1620,461 +1620,329 @@
 %0A%09%09%09
-try %7B%0A%09%09%09%09O
+boolean o
 bject
-.c
+C
 lass
-.getMethod(m.getName(), m.getParameterTypes());%0A%09%09%09%7D catch (NoSuchMethodException snme) %7B%0A%09%09%09%09// Not a method defined by O
+Method = false;%0A%09%09%09for (Method om : OBJECT_CLASS_METHODS) %7B%0A%09%09%09%09if (om.getName().equals(m.getName())) %7B%0A%09%09%09%09%09if (Arrays.equals(om.getParameterTypes(),%0A%09%09%09%09%09%09%09m.getParameterTypes())) %7B%0A%09%09%09%09%09%09o
 bject
-.c
+C
 lass
- (or override of such%0A%09%09%09%09// method)%0A%09%09%09%09return false
+Method = true;%0A%09%09%09%09%09%09break
 ;%0A%09%09%09
+%09%09
 %7D%0A
+%09%09
 %09%09%7D%0A
-%0A
 %09%09
-/*%0A%09%09 * for (Field f : cls.getDeclaredFields()) %7B int modifiers =%0A%09%09 * f.getModifiers(); if (Modifier.isStatic(modifiers)) %7B // ignore%0A%09%09 * static fields continue; %7D if (!Modifier.isPublic(modifiers)) %7B 
+%09%7D%0A%09%09%09if (!objectClassMethod) %7B%0A%09%09%09%09
 return
-%0A%09%09 *
  false;
- %7D 
+%0A%09%09%09
 %7D%0A%09%09
- */
+%7D
 %0A%0A%09%09

Make DTOUtil stop relying on exceptions