Skip to content
This repository has been archived by the owner on Jun 16, 2021. It is now read-only.

Commit

Permalink
[Feature] Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanghai committed Jun 4, 2019
0 parents commit 56fe136
Show file tree
Hide file tree
Showing 21 changed files with 1,154 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.gradle/
/.idea/
/build/
/local.properties
.DS_Store
*.iml
102 changes: 102 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Reflected

A Java library for easy-to-use reflection.

## Why Reflected?

- Thread-safe one-time retrieval of reflected objects.
- One line to reflect a method/field along with its class.
- A single exception type to handle (or ignore) for reflective operations.

## Integration

```gradle
dependencies {
implementation 'me.zhanghai.java.reflected:library:1.0.0'
}
```

## Usage

Create an instance of `ReflectedClass`, `ReflectedConstructor`, `ReflectedField` or `ReflectdMethod` to access a class, constructor, field or method via reflection. `.class` constant, `ReflectedClass` instance and `String` for class name can all be used in place where a class is expected. If you want to handle a reflection failure, catch for `ReflectedException`.

For example:

```java
public class ReflectedExamples {

// Using reflection to access a field.

private static final ReflectedField<ErrnoException> sFunctionNameField = new ReflectedField<>(
ErrnoException.class, "functionName");

@NonNull
public static String getFunctionName(@NonNull ErrnoException errnoException) {
return sFunctionNameField.getObject(errnoException);
}


// Using reflection to access a method.

@NonNull
private static final ReflectedMethod<StorageManager> sGetVolumeListMethod =
new ReflectedMethod<>(StorageManager.class, "getVolumeList");

@NonNull
public static StorageVolume[] getStorageVolumes(@NonNull StorageManager storageManager) {
return sGetVolumeListMethod.invoke(storageManager);
}


// Using a string for the declaring class of a method.

private static final ReflectedMethod<?> sNewFileChannelMethod = new ReflectedMethod<>(
"java.nio.NioUtils", "newFileChannel", Closeable.class, FileDescriptor.class,
int.class);

@NonNull
public static FileChannel newFileChannel(@NonNull Closeable ioObject,
@NonNull FileDescriptor fd, int mode) {
return sNewFileChannelMethod.invoke(null, ioObject, fd, mode);
}


// Sharing a ReflectedClass instance for the declaring class of methods.

@NonNull
private static final ReflectedClass<?> sSELinuxClass = new ReflectedClass<>(
"android.os.SELinux");

@NonNull
private static final ReflectedMethod<?> sIsSELinuxEnabledMethod = new ReflectedMethod<>(
sSELinuxClass, "isSELinuxEnabled");

@NonNull
private static final ReflectedMethod<?> sIsSELinuxEnforcedMethod = new ReflectedMethod<>(
sSELinuxClass, "isSELinuxEnforced");

public static boolean isSELinuxEnabled() {
return sIsSELinuxEnabledMethod.invoke(null);
}

public static boolean isSELinuxEnforced() {
return sIsSELinuxEnforcedMethod.invoke(null);
}
}
```

## License

Copyright 2018 Hai Zhang

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
76 changes: 76 additions & 0 deletions bintray.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2018 Hai Zhang <dreaming.in.code.zh@gmail.com>
* All Rights Reserved.
*/

apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.bintray'

task sourceJar(type: Jar) {
from sourceSets.main.allJava
classifier "sources"
}

task javadocJar(type: Jar, dependsOn: javadoc) {
from javadoc.destinationDir
classifier 'javadoc'
javadoc.failOnError = false
}

publishing {
publications {
myPublication(MavenPublication) {
from components.java
artifact sourceJar
artifact javadocJar
pom {
name = project.name
description = POM_DESCRIPTION
url = POM_URL
licenses {
license {
name = POM_LICENCE_NAME
url = POM_LICENCE_URL
}
}
developers {
developer {
id = POM_DEVELOPER_ID
name = POM_DEVELOPER_NAME
email = POM_DEVELOPER_EMAIL
}
}
scm {
connection = POM_SCM_CONNECTION
developerConnection = POM_SCM_DEVELOPER_CONNECTION
url = POM_SCM_URL
}
}
}
}
}

def getBintrayKey() {
return hasProperty('BINTRAY_KEY') ? BINTRAY_KEY : System.getenv('BINTRAY_KEY') ?:
System.console() != null ? System.console().readLine("\nBintray key: ") : ""
}

bintray {
user = BINTRAY_USER
key = getBintrayKey()
publications = ['myPublication']
pkg {
repo = BINTRAY_REPO
name = project.name
desc = POM_DESCRIPTION
websiteUrl = POM_URL
issueTrackerUrl = BINTRAY_ISSUE_TRACKER_URL
vcsUrl = BINTRAY_VCS_URL
githubRepo = BINTRAY_GITHUB_REPO
publicDownloadNumbers = true
licenses = [BINTRAY_LICENSE]
version {
name = VERSION
}
}
}
19 changes: 19 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
allprojects {

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.+'
}
}

group GROUP
version VERSION

repositories {
google()
jcenter()
}
}
19 changes: 19 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
GROUP=me.zhanghai.java.reflected
VERSION=1.0.0

POM_DESCRIPTION=A Java library for easy-to-use reflection
POM_URL=https://github.com/zhanghai/Reflected
POM_LICENCE_NAME=The Apache Software License, Version 2.0
POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
POM_DEVELOPER_ID=zhanghai
POM_DEVELOPER_NAME=Hai Zhang
POM_DEVELOPER_EMAIL=dreaming.in.code.zh@gmail.com
POM_SCM_CONNECTION=scm:git@github.com:zhanghai/Reflected.git
POM_SCM_DEVELOPER_CONNECTION=scm:git@github.com:zhanghai/Reflected.git
POM_SCM_URL=https://github.com/zhanghai/Reflected

BINTRAY_REPO=Reflected
BINTRAY_ISSUE_TRACKER_URL=https://github.com/zhanghai/Reflected/issues
BINTRAY_VCS_URL=https://github.com/zhanghai/Reflected
BINTRAY_GITHUB_REPO=zhanghai/Reflected
BINTRAY_LICENSE=Apache-2.0
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Thu Nov 22 22:06:25 PST 2018
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Loading

0 comments on commit 56fe136

Please sign in to comment.