Skip to content

Commit

Permalink
Fix compatibility issue with Android API 28+ (9+) (#185)
Browse files Browse the repository at this point in the history
* migrate optimizations from #180  to #185

* Fix #184
  • Loading branch information
Katsute authored Jun 3, 2021
1 parent cf36aab commit eb376d8
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 41 deletions.
10 changes: 8 additions & 2 deletions src/main/java/com/kttdevelopment/mal4j/APICall.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,15 @@ private static class JDK11 {

if(!useNetHttp)
try{
final Field methods = HttpURLConnection.class.getDeclaredField("methods");
Field methods;

try{
methods = HttpURLConnection.class.getDeclaredField("methods");
}catch(final NoSuchFieldException ignored){ // fix Google randomly breaking code for no reason
//noinspection JavaReflectionMemberAccess
methods = HttpURLConnection.class.getDeclaredField("PERMITTED_USER_METHODS");
}

// allow variable modification
Field modifiers;
try{
modifiers = Field.class.getDeclaredField("modifiers");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ private static String toCommaSeparatedString(final String... fields){
private static final String inverted = "^%s$|^%s(?=,)|(?<=\\w)\\{%s}|(?:^|,)%s\\{.*?}|,%s|(?<=\\{)%s,";

private static String convertFields(final String defaultFields, final List<String> fields){
return convertFields(defaultFields, fields.toArray(new String[0]));
return convertFields(defaultFields, fields == null ? null : fields.toArray(new String[0]));
}

/**
Expand Down
9 changes: 0 additions & 9 deletions src/main/java/com/kttdevelopment/mal4j/MyAnimeListSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,6 @@
@SuppressWarnings("unused")
abstract class MyAnimeListSchema {

protected static <R> R[] copyArray(final R[] array, final Class<R> Class){
if(array == null) return null;

@SuppressWarnings("unchecked")
final R[] cp = (R[]) Array.newInstance(Class, array.length);
System.arraycopy(array, 0, cp, 0, array.length);
return cp;
}

protected static <R> R[] adaptList(final JsonObject[] list, final Function<JsonObject,R> adapter, final Class<R> Class){
if(list == null) return null;

Expand Down
22 changes: 11 additions & 11 deletions src/main/java/com/kttdevelopment/mal4j/MyAnimeListSchema_Anime.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public final NSFW getNSFW() {

@Override
public final Genre[] getGenres() {
return copyArray(genres, Genre.class);
return genres != null ? Arrays.copyOf(genres, genres.length) : null;
}

@Override
Expand Down Expand Up @@ -441,12 +441,12 @@ public final AnimeRating getRating() {

@Override
public final Studio[] getStudios() {
return copyArray(studios, Studio.class);
return studios != null ? Arrays.copyOf(studios, studios.length) : null;
}

@Override
public final Picture[] getPictures() {
return copyArray(pictures, Picture.class);
return pictures != null ? Arrays.copyOf(pictures, pictures.length) : null;
}

@Override
Expand All @@ -456,17 +456,17 @@ public final String getBackground() {

@Override
public final RelatedAnime[] getRelatedAnime() {
return copyArray(relatedAnime, RelatedAnime.class);
return relatedAnime != null ? Arrays.copyOf(relatedAnime, relatedAnime.length) : null;
}

@Override
public final RelatedManga[] getRelatedManga() {
return copyArray(relatedManga, RelatedManga.class);
return relatedManga != null ? Arrays.copyOf(relatedManga, relatedManga.length) : null;
}

@Override
public final AnimeRecommendation[] getRecommendations() {
return copyArray(recommendations, AnimeRecommendation.class);
return recommendations != null ? Arrays.copyOf(recommendations, recommendations.length) : null;
}

@Override
Expand All @@ -476,12 +476,12 @@ public final AnimeStatistics getStatistics() {

@Override
public final OpeningTheme[] getOpeningThemes(){
return copyArray(openingThemes, OpeningTheme.class);
return openingThemes != null ? Arrays.copyOf(openingThemes, openingThemes.length) : null;
}

@Override
public final EndingTheme[] getEndingThemes(){
return copyArray(endingThemes, EndingTheme.class);
return endingThemes != null ? Arrays.copyOf(endingThemes, endingThemes.length) : null;
}

// additional methods
Expand Down Expand Up @@ -588,7 +588,7 @@ public final Priority getPriority() {

@Override
public final String[] getTags() {
return copyArray(tags, String.class);
return tags != null ? Arrays.copyOf(tags, tags.length) : null;
}

@Override
Expand Down Expand Up @@ -767,7 +767,7 @@ public final NSFW getNSFW() {

@Override
public final Genre[] getGenres() {
return copyArray(genres, Genre.class);
return genres != null ? Arrays.copyOf(genres, genres.length) : null;
}

@Override
Expand Down Expand Up @@ -837,7 +837,7 @@ public final AnimeRating getRating() {

@Override
public final Studio[] getStudios() {
return copyArray(studios, Studio.class);
return studios != null ? Arrays.copyOf(studios, studios.length) : null;
}

// additional methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static AlternativeTitles asAlternativeTitles(final MyAnimeList mal, final Json.J

@Override
public final String[] getSynonyms() {
return copyArray(synonyms, String.class);
return synonyms != null ? Arrays.copyOf(synonyms, synonyms.length) : null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public final Boolean isClosed() {

@Override
public final PollOption[] getOptions() {
return copyArray(options, PollOption.class);
return options != null ? Arrays.copyOf(options, options.length) : null;
}

// additional methods
Expand Down Expand Up @@ -329,7 +329,7 @@ public final String getDescription() {

@Override
public final ForumSubBoard[] getSubBoards() {
return copyArray(subBoards, ForumSubBoard.class);
return subBoards != null ? Arrays.copyOf(subBoards, subBoards.length) : null;
}

// additional methods
Expand Down Expand Up @@ -367,7 +367,7 @@ public final String getTitle() {

@Override
public final ForumBoard[] getForumBoards() {
return copyArray(forumBoards, ForumBoard.class);
return forumBoards != null ? Arrays.copyOf(forumBoards, forumBoards.length) : null;
}

// additional methods
Expand Down Expand Up @@ -437,7 +437,7 @@ public final String getTitle() {

@Override
public final Post[] getPosts() {
return copyArray(posts, Post.class);
return posts != null ? Arrays.copyOf(posts, posts.length) : null;
}

@Override
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/com/kttdevelopment/mal4j/MyAnimeListSchema_Manga.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public final NSFW getNSFW() {

@Override
public final Genre[] getGenres() {
return copyArray(genres, Genre.class);
return genres != null ? Arrays.copyOf(genres, genres.length) : null;
}

@Override
Expand Down Expand Up @@ -269,12 +269,12 @@ public final Integer getChapters() {

@Override
public final Author[] getAuthors() {
return copyArray(authors, Author.class);
return authors != null ? Arrays.copyOf(authors, authors.length) : null;
}

@Override
public final Picture[] getPictures() {
return copyArray(pictures, Picture.class);
return pictures != null ? Arrays.copyOf(pictures, pictures.length) : null;
}

@Override
Expand All @@ -284,22 +284,22 @@ public final String getBackground() {

@Override
public final RelatedAnime[] getRelatedAnime() {
return copyArray(relatedAnime, RelatedAnime.class);
return relatedAnime != null ? Arrays.copyOf(relatedAnime, relatedAnime.length) : null;
}

@Override
public final RelatedManga[] getRelatedManga() {
return copyArray(relatedManga, RelatedManga.class);
return relatedManga != null ? Arrays.copyOf(relatedManga, relatedManga.length) : null;
}

@Override
public final MangaRecommendation[] getRecommendations() {
return copyArray(recommendations, MangaRecommendation.class);
return recommendations != null ? Arrays.copyOf(recommendations, recommendations.length) : null;
}

@Override
public final Publisher[] getSerialization() {
return copyArray(serialization, Publisher.class);
return serialization != null ? Arrays.copyOf(serialization, serialization.length) : null;
}

// additional methods
Expand Down Expand Up @@ -400,7 +400,7 @@ public final Priority getPriority() {

@Override
public final String[] getTags() {
return copyArray(tags, String.class);
return tags != null ? Arrays.copyOf(tags, tags.length) : null;
}

@Override
Expand Down Expand Up @@ -581,7 +581,7 @@ public final NSFW getNSFW() {

@Override
public final Genre[] getGenres() {
return copyArray(genres, Genre.class);
return genres != null ? Arrays.copyOf(genres, genres.length) : null;
}

@Override
Expand Down Expand Up @@ -631,7 +631,7 @@ public final Integer getChapters() {

@Override
public final Author[] getAuthors() {
return copyArray(authors, Author.class);
return authors != null ? Arrays.copyOf(authors, authors.length) : null;
}

// additional methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ public enum Season {
* @since 1.0.0
*/
public final String[] getMonths(){
final String[] copy = new String[months.length];
System.arraycopy(months, 0, copy, 0, months.length);
return copy;
return Arrays.copyOf(months, months.length);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public void testStatus(){
mal.getUserAnimeListing()
.withStatus(AnimeStatus.Dropped)
.withFields(Fields.Anime.list_status)
.withLimit(1)
.search();
Assertions.assertEquals(AnimeStatus.Dropped, list.get(0).getStatus());
}
Expand All @@ -33,6 +34,7 @@ public void testSort(){
mal.getUserAnimeListing()
.sortBy(AnimeSort.UpdatedAt)
.withFields(Fields.Anime.list_status)
.withLimit(2)
.search();
Assertions.assertTrue(list.get(0).getUpdatedAt().getTime() > list.get(1).getUpdatedAt().getTime());
}
Expand Down

0 comments on commit eb376d8

Please sign in to comment.