Skip to content

Commit

Permalink
Set namespace name (fixes #141)
Browse files Browse the repository at this point in the history
jwharm committed Sep 28, 2024
1 parent 8de413d commit 88dca8e
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* Java-GI - Java language bindings for GObject-Introspection-based libraries
* Copyright (C) 2022-2023 Jan-Willem Harmannij
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

package io.github.jwharm.javagi.gobject.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PACKAGE)
public @interface Namespace {
String name() default "";
}
Original file line number Diff line number Diff line change
@@ -636,12 +636,20 @@ public static boolean IS_DEPRECATED(Type type) {
public static String getName(Class<?> cls) {
// Default type name: fully qualified Java class name
String typeNameInput = cls.getName();
String namespace = "";

// Check for a Namespace annotation on the package
if (cls.getPackage().isAnnotationPresent(Namespace.class)) {
var annotation = cls.getPackage().getAnnotation(Namespace.class);
namespace = annotation.name();
typeNameInput = namespace + cls.getSimpleName();
}

// Check for an annotation that overrides the type name
if (cls.isAnnotationPresent(RegisteredType.class)) {
var annotation = cls.getAnnotation(RegisteredType.class);
if (! "".equals(annotation.name())) {
typeNameInput = annotation.name();
typeNameInput = namespace + annotation.name();
}
}

0 comments on commit 88dca8e

Please sign in to comment.