Java Code Examples for org.apache.commons.lang.reflect.FieldUtils#readField()

The following examples show how to use org.apache.commons.lang.reflect.FieldUtils#readField() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Serializer.java    From das with Apache License 2.0 6 votes vote down vote up
default Object readField(Object target, String fieldName) {
    try {
        return FieldUtils.readField(target, fieldName, true);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: CliTest.java    From pom-manipulation-ext with Apache License 2.0 6 votes vote down vote up
@Test
public void checkLocalRepositoryWithDefaults() throws Exception
{
    Cli c = new Cli();
    File settings = writeSettings( temp.newFile());

    TestUtils.executeMethod( c, "run", new Object[] { new String[] { "-s", settings.toString()} } );

    ManipulationSession session = (ManipulationSession) FieldUtils.readField( c, "session", true );
    MavenSession ms = (MavenSession)FieldUtils.readField( session, "mavenSession", true );

    assertEquals( ms.getRequest().getLocalRepository().getBasedir(),
                  ms.getRequest().getLocalRepositoryPath().toString() );
    assertEquals( "File " + new File( ms.getRequest().getLocalRepository().getBasedir() ).getParentFile().toString()
                                  + " was not equal to " + System.getProperty( "user.home" ) + File.separatorChar
                                  + ".m2",
                  new File( ms.getRequest().getLocalRepository().getBasedir() ).getParentFile().toString(),
                  System.getProperty( "user.home" ) + File.separatorChar + ".m2" );

}
 
Example 3
Source File: CliTest.java    From pom-manipulation-ext with Apache License 2.0 6 votes vote down vote up
@Test
public void checkLocalRepositoryWithExplicitMavenRepoAndSettings() throws Exception
{
    File folder = temp.newFolder();
    Cli c = new Cli();
    TestUtils.executeMethod( c, "run", new Object[] { new String[]
                    { "--settings=" + getClass().getResource("/settings-test.xml").getFile(),
                                    "-Dmaven.repo.local=" + folder.toString() }} );

    ManipulationSession session = (ManipulationSession) FieldUtils.readField( c, "session", true );
    MavenSession ms = (MavenSession)FieldUtils.readField( session, "mavenSession", true );

    assertEquals( ms.getLocalRepository().getBasedir(), folder.toString() );
    assertEquals( ms.getRequest().getLocalRepository().getBasedir(),
                  ms.getRequest().getLocalRepositoryPath().toString() );
}
 
Example 4
Source File: CliTest.java    From pom-manipulation-ext with Apache License 2.0 5 votes vote down vote up
@Test
public void checkTargetDefaultMatches() throws Exception
{
    Cli c = new Cli();
    c.run( new String[] {} );

    ManipulationSession session = (ManipulationSession) FieldUtils.readField( c, "session", true );
    File defaultTarget = (File) FieldUtils.readField( c, "target", true );

    assertEquals( "Session file should match", defaultTarget, session.getPom() );
}
 
Example 5
Source File: CliTest.java    From pom-manipulation-ext with Apache License 2.0 5 votes vote down vote up
@Test
public void checkLocalRepositoryWithSettings() throws Exception
{
    Cli c = new Cli();
    TestUtils.executeMethod( c, "run", new Object[] { new String[] { "-settings=" + getClass().getResource( "/settings-test.xml").getFile() }} );

    ManipulationSession session = (ManipulationSession) FieldUtils.readField( c, "session", true );
    MavenSession ms = (MavenSession)FieldUtils.readField( session, "mavenSession", true );

    assertEquals( ms.getRequest().getLocalRepository().getBasedir(),
                  ms.getRequest().getLocalRepositoryPath().toString() );
}
 
Example 6
Source File: CliTest.java    From pom-manipulation-ext with Apache License 2.0 5 votes vote down vote up
@Test
public void checkLocalRepositoryWithExplicitMavenRepo() throws Exception
{
    File folder = temp.newFolder();
    Cli c = new Cli();
    TestUtils.executeMethod( c, "run", new Object[] { new String[] { "-Dmaven.repo.local=" + folder.toString() }} );

    ManipulationSession session = (ManipulationSession) FieldUtils.readField( c, "session", true );
    MavenSession ms = (MavenSession)FieldUtils.readField( session, "mavenSession", true );

    assertEquals( ms.getRequest().getLocalRepository().getBasedir(),
                  ms.getRequest().getLocalRepositoryPath().toString() );
}
 
Example 7
Source File: AWSClientFactory.java    From awseb-deployment-plugin with Apache License 2.0 5 votes vote down vote up
<T extends AmazonWebServiceClient> String getEndpointFor(T client) {
    try {
        URI endpointUri = (URI) FieldUtils.readField(client, "endpoint", true);

        return endpointUri.toASCIIString();
    } catch (Exception e) {
        return null;
    }
}
 
Example 8
Source File: PropertyUtils.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the object of the (nested) property.
 */
public static Object getNestedObject(Object instance, String propertyName) throws IllegalAccessException {
    if (PropertyUtils.isWeatherProperty(propertyName)) {
        return instance;
    }

    while (PropertyResolver.hasNested(propertyName)) {
        instance = FieldUtils.readField(instance, PropertyResolver.first(propertyName), true);
        propertyName = PropertyResolver.removeFirst(propertyName);
    }
    return instance;
}
 
Example 9
Source File: CliTest.java    From pom-manipulation-ext with Apache License 2.0 4 votes vote down vote up
@Test
public void checkLocalRepositoryWithDefaultsAndModifiedUserSettings() throws Exception
{
    boolean restore = false;
    Path source = Paths.get ( System.getProperty( "user.home" ) + File.separatorChar + ".m2" + File.separatorChar + "settings.xml");
    Path backup = Paths.get( source.toString() + '.' + UUID.randomUUID().toString() );
    Path tmpSettings = Paths.get ( getClass().getResource("/settings-test.xml").toURI() );

    try
    {
        if ( source.toFile().exists() )
        {
            System.out.println ("Backing up settings.xml to " + backup);
            restore = true;
            Files.move( source, backup, StandardCopyOption.ATOMIC_MOVE);
        }
        Files.copy( tmpSettings, source );

        Cli c = new Cli();
        TestUtils.executeMethod( c, "run", new Object[] { new String[] {} } );

        ManipulationSession session = (ManipulationSession) FieldUtils.readField( c, "session", true );
        MavenSession ms = (MavenSession) FieldUtils.readField( session, "mavenSession", true );

        assertEquals( ms.getRequest().getLocalRepository().getBasedir(),
                      ms.getRequest().getLocalRepositoryPath().toString() );
        assertEquals( ms.getLocalRepository().getBasedir(),
                      System.getProperty( "user.home" ) + File.separatorChar + ".m2-mead-test" );

    }
    finally
    {
        if ( restore )
        {
            Files.move( backup, source, StandardCopyOption.ATOMIC_MOVE);
        }
        else
        {
            Files.delete( source );
        }
    }
}