Skip to content

Commit

Permalink
💾 λjcommon v1.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
LambdAurora committed Apr 25, 2018
1 parent b0dcd59 commit 1c25d56
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[![Java](https://img.shields.io/badge/language-Java%2010-9B599A.svg?style=flat-square)](http://java.com)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://raw.githubusercontent.com/AperLambda/lambdajcommon/master/LICENSE)
![Version](https://img.shields.io/github/release/AperLambda/lambdajcommon.svg?style=flat-square)
[![GitHub issues](https://img.shields.io/github/issues/AperLambda/lambdajcommon.svg?style=flat-square)](https://github.com/AperLambda/lambdajcommon/issues)

[λcommon](https://github.com/AperLambda/lambdacommon.git):
Expand Down Expand Up @@ -34,5 +35,5 @@ maven { url "https://aperlambda.github.io/maven" }

And in your dependencies add:
```groovy
compile 'org.aperlambda:lambdajcommon:1.4.0'
compile 'org.aperlambda:lambdajcommon:1.4.1'
```
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ apply plugin: 'maven'
apply plugin: 'maven-publish'

group = 'org.aperlambda'
version = '1.4.0'
version = '1.4.1'
description = 'A library written in Java 8 with common features.'

sourceCompatibility = 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ private LambdaReflection()
Fields
*/

private static void setupField(Field field)
{
field.setAccessible(true);
try
{
var modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
int modifiers = modifiersField.getInt(field);
modifiers &= ~Modifier.FINAL;
modifiersField.setInt(field, modifiers);
}
catch (IllegalAccessException | NoSuchFieldException ignored)
{
}
}

/**
* Gets the field by it's name from a specified class.
*
Expand All @@ -43,21 +59,57 @@ private LambdaReflection()
{
try
{
Field field = declared ? clazz.getDeclaredField(fieldName) : clazz.getField(fieldName);
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
int modifiers = modifiersField.getInt(field);
modifiers &= ~Modifier.FINAL;
modifiersField.setInt(field, modifiers);
var field = declared ? clazz.getDeclaredField(fieldName) : clazz.getField(fieldName);
setupField(field);
return Optional.of(field);
}
catch (NoSuchFieldException | IllegalAccessException e)
catch (NoSuchFieldException e)
{
return Optional.empty();
}
}

/**
* Gets the first field with the specified type in a class.
*
* @param clazz The class of the field.
* @param type The type of the field.
* @return The optional field.
*/
public static @NotNull Optional<Field> getFirstFieldOfType(@NotNull Class<?> clazz, @NotNull Class<?> type)
{
for (var field : clazz.getDeclaredFields())
{
if (field.getType().equals(type))
{
setupField(field);
return Optional.of(field);
}
}
return Optional.empty();
}

/**
* Gets the last field with the specified type in a class.
*
* @param clazz The class of the field.
* @param type The type of the field.
* @return The optional field.
*/
public static @NotNull Optional<Field> getLastFieldOfType(@NotNull Class<?> clazz, @NotNull Class<?> type)
{
Field field = null;
for (var currentField : clazz.getDeclaredFields())
{
if (currentField.getType().equals(type))
field = currentField;
}
if (field != null)
setupField(field);
return Optional.ofNullable(field);
}


/**
* Sets the value of the specified field in the instance of the specified object.
*
Expand Down Expand Up @@ -150,7 +202,6 @@ public static void setValue(@Nullable Object object, @NotNull Field field, @Null
}
catch (NoSuchMethodException e)
{
e.printStackTrace();
return Optional.empty();
}
}
Expand All @@ -171,7 +222,6 @@ public static void setValue(@Nullable Object object, @NotNull Field field, @Null
}
catch (IllegalAccessException | InvocationTargetException e)
{
e.printStackTrace();
return null;
}
}
Expand Down Expand Up @@ -223,9 +273,8 @@ public static void setValue(@Nullable Object object, @NotNull Field field, @Null
}
catch (InstantiationException | IllegalAccessException | InvocationTargetException e)
{
e.printStackTrace();
return null;
}
return null;
}

/*
Expand All @@ -239,7 +288,6 @@ public static void setValue(@Nullable Object object, @NotNull Field field, @Null
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
return Optional.empty();
}
}
Expand Down

0 comments on commit 1c25d56

Please sign in to comment.