/*******************************************************************************
 * 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.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;

/**
 * @author $Id$
 */
class DTOUtil {
	private static final Method[] OBJECT_CLASS_METHODS = Object.class
			.getMethods();

	private DTOUtil() {
		// Do not instantiate. This is a utility class.
	}

	static boolean isDTOType(Class< ? > cls, boolean ignorePublicNoArgsCtor) {
		if (!ignorePublicNoArgsCtor) {
			boolean hasPublicNoargsCtor = false;
			for (Constructor< ? > ctor : cls.getConstructors()) {
				if (ctor.getParameterCount() == 0) {
					hasPublicNoargsCtor = true;
					break;
				}
			}
			if (!hasPublicNoargsCtor) {
				return false;
			}
		}

		for (Method m : cls.getMethods()) {
			boolean objectClassMethod = false;
			for (Method om : OBJECT_CLASS_METHODS) {
				if (om.getName().equals(m.getName())) {
					if (Arrays.equals(om.getParameterTypes(),
							m.getParameterTypes())) {
						objectClassMethod = true;
						break;
					}
				}
			}
			if (!objectClassMethod) {
				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;
	}
}

@@ -1329,77 +1329,26 @@
 %0A%09%09%09
-boolean hasPublicNoargsCtor = false;%0A%09%09%09for (Constructor%3C ? %3E ctor : 
+if (Arrays.stream(
 cls.
@@ -1365,27 +1365,41 @@
 ctors())
- %7B
 %0A%09%09%09%09
-if (
+%09.noneMatch(ctor -%3E 
 ctor.get
@@ -1424,97 +1424,60 @@
 = 0)
+)
  %7B%0A%09%09%09%09
-%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
+// No public zero-arg constructor, not a DTO
 %0A%09%09%09
@@ -1546,63 +1546,26 @@
 %0A%09%09%09
-boolean objectClassMethod = false;%0A%09%09%09for (Method om : 
+if (Arrays.stream(
 OBJE
@@ -1585,19 +1585,31 @@
 ODS)
- %7B
 %0A%09%09%09%09
-if (
+%09.noneMatch(om -%3E 
 om.g
@@ -1640,21 +1640,19 @@
 e())
-)
- %7B
 %0A%09%09%09%09%09
-if (
+%09%09&& 
 Arra
@@ -1692,16 +1692,18 @@
 %0A%09%09%09%09%09%09%09
+%09%09
 m.getPar
@@ -1721,102 +1721,91 @@
 ()))
+)
  %7B%0A%09%09%09%09
-%09%09o
+// Not a method defined by O
 bject
-C
+.c
 lass
-Method = true;%0A%09%09%09%09%09%09break;%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09if (!objectClassM
+ (or override of such%0A%09%09%09%09// m
 ethod)
- %7B
 %0A%09%09%09

Apply suggestions from code review