Skip to content

Commit

Permalink
Merge pull request #1 from g3force/migrate-to-log4j2
Browse files Browse the repository at this point in the history
Migrate to log4j2
  • Loading branch information
g3force authored Oct 5, 2019
2 parents 703479c + ed5bfa3 commit f5ae35e
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 227 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/target/
.idea
*.iml
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.g3force</groupId>
<artifactId>instanceables</artifactId>
<version>v1.3</version>
<version>2.0</version>
<name>instanceables</name>

<properties>
Expand All @@ -20,15 +20,15 @@
<dependencies>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.12.1</version>
</dependency>

<dependency>
<groupId>com.github.g3force</groupId>
<artifactId>String2ValueConverter</artifactId>
<version>v1.6</version>
<version>2.1</version>
</dependency>

</dependencies>
Expand Down Expand Up @@ -74,4 +74,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,11 @@

/**
* Implement this in an enum that provides {@link InstanceableClass}s
*
* @author Nicolai Ommer <nicolai.ommer@gmail.com>
*/
public interface IInstanceableEnum
{
/**
* @return
*/
InstanceableClass getInstanceableClass();


/**
* @return
*/


String name();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,8 @@

/**
* Observer interface to get notified about new instances
*
* @author Nicolai Ommer <nicolai.ommer@gmail.com>
*/
public interface IInstanceableObserver
{
/**
* @param object
*/
void onNewInstance(Object object);
}
108 changes: 29 additions & 79 deletions src/main/java/com/github/g3force/instanceables/InstanceableClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,30 @@
package com.github.g3force.instanceables;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
* An {@link InstanceableClass} can be used to create an object from a class and a set of parameters.
*
* @author Nicolai Ommer <nicolai.ommer@gmail.com>
*/
public class InstanceableClass
{
// --------------------------------------------------------------------------
// --- variables and constants ----------------------------------------------
// --------------------------------------------------------------------------
private final Class<?> impl;
private final List<InstanceableParameter> params;


// --------------------------------------------------------------------------
// --- constructors ---------------------------------------------------------
// --------------------------------------------------------------------------

/**
* @param impl
* @param params
*/
private final Class<?> impl;
private final List<InstanceableParameter> params;


public InstanceableClass(final Class<?> impl, final InstanceableParameter... params)
{
this.impl = impl;
this.params = Arrays.asList(params);
}


// --------------------------------------------------------------------------
// --- methods --------------------------------------------------------------
// --------------------------------------------------------------------------



/**
* Create a new instance with the specified arguments
*
*
* @param args
* @return
* @throws NotCreateableException
Expand All @@ -62,35 +44,20 @@ public Object newInstance(final Object... args) throws NotCreateableException
{
Constructor<?> con = getConstructor();
result = con.newInstance(args);
} catch (final SecurityException err)
{
throw new NotCreateableException("", err);
} catch (final InstantiationException err)
{
throw new NotCreateableException("", err);
} catch (final IllegalAccessException err)
{
throw new NotCreateableException("", err);
} catch (final IllegalArgumentException err)
{
throw new NotCreateableException("", err);
} catch (final InvocationTargetException err)
{
throw new NotCreateableException("", err);
} catch (final IllegalStateException err)
{
throw new NotCreateableException("", err);
} catch (NoSuchMethodException err)
{
throw new NotCreateableException("Wrong constructor types.", err);
} catch (final Exception err)
{
throw new NotCreateableException("Can not create instance", err);
}
return result;
}


/**
* Create a new instance with default parameters (as defined in enum)
*
*
* @return
* @throws NotCreateableException
*/
Expand All @@ -100,79 +67,62 @@ public Object newDefaultInstance() throws NotCreateableException
{
return newInstance();
}
List<Object> objParams = new ArrayList<Object>(getParams().size());
List<Object> objParams = new ArrayList<>(getParams().size());
for (InstanceableParameter param : getParams())
{
Object objParam = param.parseString(param.getDefaultValue());
objParams.add(objParam);
}
return newInstance(objParams.toArray());
}


/**
* Add parameter
*
*
* @param param
*/
public void addParam(final InstanceableParameter param)
{
params.add(param);
}


// --------------------------------------------------------------------------
// --- getter/setter --------------------------------------------------------
// --------------------------------------------------------------------------




/**
* Returns the first public constructor of the Play.
*
* @return
* @throws NoSuchMethodException
* @throws SecurityException
*/
public Constructor<?> getConstructor() throws NoSuchMethodException
{
Class<?> paramTypes[] = new Class<?>[params.size()];
Class<?>[] paramTypes = new Class<?>[params.size()];
for (int i = 0; i < params.size(); i++)
{
paramTypes[i] = params.get(i).getImpl();
}
return impl.getConstructor(paramTypes);
}


/**
* @return the params
*/
public final List<InstanceableParameter> getParams()
{
return params;
}


/**
* @author Nicolai Ommer <nicolai.ommer@gmail.com>
*/


public static class NotCreateableException extends Exception
{
/** */
private static final long serialVersionUID = 89775383135278930L;


/**
* @param message
* @param cause
*/
private static final long serialVersionUID = 89775383135278930L;


public NotCreateableException(final String message, final Throwable cause)
{
super(message, cause);
}
}


/**
* @return the impl
*/
Expand Down
Loading

0 comments on commit f5ae35e

Please sign in to comment.