Skip to content
Open

changes #2799

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -565,9 +565,9 @@ public String getMethodNameFromAnnotation(ExecutableElement method) {
return annotationName;
}

if (method.getParameters().isEmpty()) {
return null;
}
// if (method.getParameters().isEmpty()) {
// return null;
// }

// Check if the class or package has the annotation
TypeElement clazz = ElementUtil.getDeclaringClass(method.getExecutableElement());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,244 @@ public void testSwiftNameClassAnnotation() throws IOException {
assertInTranslation(translation, "NS_SWIFT_NAME(FooBar)");
}

public void testSwiftNamePackagePrivateConstructor() throws IOException {
String sourceContent = """
package com.foo.bar;
import com.google.j2objc.annotations.SwiftName;
@SwiftName
public class FooBar {
FooBar(int x) {}
}
""";
String translation = translateSourceFile(sourceContent, "FooBar", "com/foo/bar/FooBar.h");
assertThat(translation).isEqualTo("""
//
// Generated by the J2ObjC translator. DO NOT EDIT!
// source: FooBar.java
//

#import <Foundation/Foundation.h>
#include "J2ObjC_header.h"

#pragma push_macro("INCLUDE_ALL_ComFooBarFooBar")
#ifdef RESTRICT_ComFooBarFooBar
#define INCLUDE_ALL_ComFooBarFooBar 0
#else
#define INCLUDE_ALL_ComFooBarFooBar 1
#endif
#undef RESTRICT_ComFooBarFooBar

#if !defined (ComFooBarFooBar_) && (INCLUDE_ALL_ComFooBarFooBar || defined(INCLUDE_ComFooBarFooBar))
#define ComFooBarFooBar_

@class JavaLangInteger;

NS_SWIFT_NAME(FooBar)
@interface ComFooBarFooBar : NSObject

#pragma mark Package-Private

- (instancetype)initWithInt:(int32_t)x NS_SWIFT_NAME(init(x:));

// Disallowed inherited constructors, do not use.

- (instancetype)init NS_SWIFT_NAME(init()) NS_UNAVAILABLE;

@end

J2OBJC_EMPTY_STATIC_INIT(ComFooBarFooBar)

FOUNDATION_EXPORT void ComFooBarFooBar_initWithInt_(ComFooBarFooBar *self, int32_t x);

FOUNDATION_EXPORT ComFooBarFooBar *new_ComFooBarFooBar_initWithInt_(int32_t x) NS_RETURNS_RETAINED;

FOUNDATION_EXPORT ComFooBarFooBar *create_ComFooBarFooBar_initWithInt_(int32_t x);

J2OBJC_TYPE_LITERAL_HEADER(ComFooBarFooBar)


#endif

#pragma pop_macro("INCLUDE_ALL_ComFooBarFooBar")
""");
}

public void testSwiftNamePackagePrivateClassConstructor() throws IOException {
String sourceContent = """
package com.foo.bar;
import com.google.j2objc.annotations.SwiftName;
@SwiftName
class FooBar {
FooBar(int x) {}
}
""";
String translation = translateSourceFile(sourceContent, "FooBar", "com/foo/bar/FooBar.h");
assertThat(translation).isEqualTo("""
//
// Generated by the J2ObjC translator. DO NOT EDIT!
// source: FooBar.java
//

#import <Foundation/Foundation.h>
#include "J2ObjC_header.h"

#pragma push_macro("INCLUDE_ALL_ComFooBarFooBar")
#ifdef RESTRICT_ComFooBarFooBar
#define INCLUDE_ALL_ComFooBarFooBar 0
#else
#define INCLUDE_ALL_ComFooBarFooBar 1
#endif
#undef RESTRICT_ComFooBarFooBar

#if !defined (ComFooBarFooBar_) && (INCLUDE_ALL_ComFooBarFooBar || defined(INCLUDE_ComFooBarFooBar))
#define ComFooBarFooBar_

@class JavaLangInteger;

NS_SWIFT_NAME(FooBar)
@interface ComFooBarFooBar : NSObject

#pragma mark Package-Private

- (instancetype)initPackagePrivateWithInt:(int32_t)x NS_SWIFT_NAME(init(x:));

// Disallowed inherited constructors, do not use.

- (instancetype)init NS_SWIFT_NAME(init()) NS_UNAVAILABLE;

@end

J2OBJC_EMPTY_STATIC_INIT(ComFooBarFooBar)

FOUNDATION_EXPORT void ComFooBarFooBar_initPackagePrivateWithInt_(ComFooBarFooBar *self, int32_t x);

FOUNDATION_EXPORT ComFooBarFooBar *new_ComFooBarFooBar_initPackagePrivateWithInt_(int32_t x) NS_RETURNS_RETAINED;

FOUNDATION_EXPORT ComFooBarFooBar *create_ComFooBarFooBar_initPackagePrivateWithInt_(int32_t x);

J2OBJC_TYPE_LITERAL_HEADER(ComFooBarFooBar)


#endif

#pragma pop_macro("INCLUDE_ALL_ComFooBarFooBar")
""");
}

public void testSwiftNamePackagePrivateConstructorZeroParams() throws IOException {
String sourceContent = """
package com.foo.bar;
import com.google.j2objc.annotations.SwiftName;
@SwiftName
public class FooBar {
FooBar() {}
}
""";
String translation = translateSourceFile(sourceContent, "FooBar", "com/foo/bar/FooBar.h");
assertThat(translation).isEqualTo("""
//
// Generated by the J2ObjC translator. DO NOT EDIT!
// source: FooBar.java
//

#import <Foundation/Foundation.h>
#include "J2ObjC_header.h"

#pragma push_macro("INCLUDE_ALL_ComFooBarFooBar")
#ifdef RESTRICT_ComFooBarFooBar
#define INCLUDE_ALL_ComFooBarFooBar 0
#else
#define INCLUDE_ALL_ComFooBarFooBar 1
#endif
#undef RESTRICT_ComFooBarFooBar

#if !defined (ComFooBarFooBar_) && (INCLUDE_ALL_ComFooBarFooBar || defined(INCLUDE_ComFooBarFooBar))
#define ComFooBarFooBar_

NS_SWIFT_NAME(FooBar)
@interface ComFooBarFooBar : NSObject

#pragma mark Package-Private

- (instancetype)init NS_SWIFT_NAME(init());

@end

J2OBJC_EMPTY_STATIC_INIT(ComFooBarFooBar)

FOUNDATION_EXPORT void ComFooBarFooBar_init(ComFooBarFooBar *self);

FOUNDATION_EXPORT ComFooBarFooBar *new_ComFooBarFooBar_init(void) NS_RETURNS_RETAINED;

FOUNDATION_EXPORT ComFooBarFooBar *create_ComFooBarFooBar_init(void);

J2OBJC_TYPE_LITERAL_HEADER(ComFooBarFooBar)


#endif

#pragma pop_macro("INCLUDE_ALL_ComFooBarFooBar")
""");
}

public void testSwiftNameZeroParamMethod() throws IOException {
String sourceContent = """
package com.foo.bar;
import com.google.j2objc.annotations.SwiftName;
@SwiftName
public class FooBar {
public void foo() {}
}
""";
String translation = translateSourceFile(sourceContent, "FooBar", "com/foo/bar/FooBar.h");
assertThat(translation).isEqualTo("""
//
// Generated by the J2ObjC translator. DO NOT EDIT!
// source: FooBar.java
//

#import <Foundation/Foundation.h>
#include "J2ObjC_header.h"

#pragma push_macro("INCLUDE_ALL_ComFooBarFooBar")
#ifdef RESTRICT_ComFooBarFooBar
#define INCLUDE_ALL_ComFooBarFooBar 0
#else
#define INCLUDE_ALL_ComFooBarFooBar 1
#endif
#undef RESTRICT_ComFooBarFooBar

#if !defined (ComFooBarFooBar_) && (INCLUDE_ALL_ComFooBarFooBar || defined(INCLUDE_ComFooBarFooBar))
#define ComFooBarFooBar_

NS_SWIFT_NAME(FooBar)
@interface ComFooBarFooBar : NSObject

#pragma mark Public

- (instancetype)init NS_SWIFT_NAME(init());

- (void)foo NS_SWIFT_NAME(foo());

@end

J2OBJC_EMPTY_STATIC_INIT(ComFooBarFooBar)

FOUNDATION_EXPORT void ComFooBarFooBar_init(ComFooBarFooBar *self);

FOUNDATION_EXPORT ComFooBarFooBar *new_ComFooBarFooBar_init(void) NS_RETURNS_RETAINED;

FOUNDATION_EXPORT ComFooBarFooBar *create_ComFooBarFooBar_init(void);

J2OBJC_TYPE_LITERAL_HEADER(ComFooBarFooBar)


#endif

#pragma pop_macro("INCLUDE_ALL_ComFooBarFooBar")
""");
}

public void testSwiftNameClassAnnotationOverride() throws IOException {
String sourceContent =
" package com.foo.bar;"
Expand Down