Skip to content

Commit

Permalink
Merge branch 'EsotericSoftware:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
tchemit authored Nov 27, 2023
2 parents 315d899 + 8f35506 commit 04b1607
Show file tree
Hide file tree
Showing 47 changed files with 2,799 additions and 402 deletions.
4 changes: 2 additions & 2 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<classpath>
<classpathentry excluding="**/.svn/*" kind="src" path="src"/>
<classpathentry excluding="**/.svn/*" kind="src" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/junit-4.4.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="lib" path="lib/junit-4.8.2.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
37 changes: 37 additions & 0 deletions .github/workflows/maven-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This workflow will build a Java project with Maven
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven

name: Java CI with Maven

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
jdk8:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build with Maven
run: mvn test
jdk7:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.7
uses: actions/setup-java@v1
with:
java-version: 1.7
- name: Build with Maven
run: mvn test

2 changes: 2 additions & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8
7 changes: 7 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
2 changes: 1 addition & 1 deletion CN_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
**YamlBeans**则可以让Java对象和YAML格式之间的转换(序列化和反序列化)变得更容易。

Maven 仓库:
http://repo1.maven.org/maven2/com/esotericsoftware/yamlbeans/yamlbeans/
https://repo1.maven.org/maven2/com/esotericsoftware/yamlbeans/yamlbeans/

## 基本的反序列化

Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
## YamlBeans

![License](https://img.shields.io/badge/License-BSD%202--Clause-orange.svg)
![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.esotericsoftware.yamlbeans/yamlbeans/badge.svg)

Please use the [YamlBeans discussion group](http://groups.google.com/group/yamlbeans-users) for support.
Expand All @@ -12,7 +11,7 @@ Chinese document [中文翻译文档](CN_README.md) .
YamlBeans makes it easy to serialize and deserialize Java object graphs to and from YAML, a human-friendly data format. Replace XML and properties files with YAML for more expressive power (lists, maps, anchors, etc) and easier hand-editing.

Maven Central:
http://repo1.maven.org/maven2/com/esotericsoftware/yamlbeans/yamlbeans/
https://repo1.maven.org/maven2/com/esotericsoftware/yamlbeans/yamlbeans/

## Basic deserialization

Expand Down
Binary file removed lib/junit-4.4.jar
Binary file not shown.
Binary file added lib/junit-4.8.2.jar
Binary file not shown.
108 changes: 62 additions & 46 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
<name>YamlBeans</name>
<groupId>com.esotericsoftware.yamlbeans</groupId>
<artifactId>yamlbeans</artifactId>
<version>1.13</version>
<version>1.18-SNAPSHOT</version>

<description>Java object graphs, to and from YAML</description>
<url>https://github.com/EsotericSoftware/yamlbeans</url>

<licenses>
<license>
<name>New BSD License</name>
<url>http://www.opensource.org/licenses/bsd-license.php</url>
<name>MIT License</name>
<url>https://opensource.org/licenses/MIT</url>
<distribution>repo</distribution>
</license>
</licenses>
Expand Down Expand Up @@ -144,49 +144,65 @@
</contributors>

<profiles>
<profile>
<id>release-sign-artifacts</id>
<activation>
<property>
<name>performRelease</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.nuiton</groupId>
<artifactId>helper-maven-plugin</artifactId>
<executions>
<execution>
<id>get-pgp-passphrase</id>
<goals>
<goal>share-server-secret</goal>
</goals>
<phase>verify</phase>
<configuration>
<serverId>${gpg.serverId}</serverId>
<usernameOut>gpg.keyname</usernameOut>
<passwordOut>gpg.passphrase</passwordOut>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-gpg-plugin</artifactId>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>release-sign-artifacts</id>
<distributionManagement>
<snapshotRepository>
<id>nexus-release</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>nexus-release</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
<build>
<plugins>
<!-- Source -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Javadoc -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
2 changes: 1 addition & 1 deletion project.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
classpath: lib|*.jar|!junit*.jar
version: 1.13
version: 1.15
112 changes: 48 additions & 64 deletions src/com/esotericsoftware/yamlbeans/Beans.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,46 +112,12 @@ static public Object createObject (Class type, boolean privateConstructors) thro

static public Set<Property> getProperties (Class type, boolean beanProperties, boolean privateFields, YamlConfig config) {
if (type == null) throw new IllegalArgumentException("type cannot be null.");
Class[] noArgs = new Class[0], oneArg = new Class[1];
Set<Property> properties = config.writeConfig.keepBeanPropertyOrder ? new LinkedHashSet() : new TreeSet();
for (Field field : getAllFields(type)) {
String name = field.getName();

if (beanProperties) {
DeferredConstruction deferredConstruction = getDeferredConstruction(type, config);
boolean constructorProperty = deferredConstruction != null && deferredConstruction.hasParameter(name);

String nameUpper = Character.toUpperCase(name.charAt(0)) + name.substring(1);
Method getMethod = null, setMethod = null;
try {
oneArg[0] = field.getType();
setMethod = type.getMethod("set" + nameUpper, oneArg);
} catch (Exception ignored) {
}
try {
getMethod = type.getMethod("get" + nameUpper, noArgs);
} catch (Exception ignored) {
}
if (getMethod == null && (field.getType().equals(Boolean.class) || field.getType().equals(boolean.class))) {
try {
getMethod = type.getMethod("is" + nameUpper, noArgs);
} catch (Exception ignored) {
}
}
if (getMethod != null && (setMethod != null || constructorProperty)) {
properties.add(new MethodProperty(name, setMethod, getMethod));
continue;
}
Property property = getProperty(type, beanProperties, privateFields, config, field);
if (property != null) {
properties.add(property);
}

int modifiers = field.getModifiers();
if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers)) continue;
if (!Modifier.isPublic(modifiers) && !privateFields) continue;
try {
field.setAccessible(true);
} catch (Exception ignored) {
}
properties.add(new FieldProperty(field));
}
return properties;
}
Expand All @@ -171,45 +137,63 @@ static public Property getProperty (Class type, String name, boolean beanPropert
if (name == null || name.length() == 0) throw new IllegalArgumentException("name cannot be null or empty.");
name = toJavaIdentifier(name);

for (Field field : getAllFields(type))
if (field.getName().equals(name)) return getProperty(type, beanProperties, privateFields, config, field);

// If a property is not found, try prepending `_` for reserved field names (eg "class" in the data can be a "_class" field).
if (!name.startsWith("_")) return getProperty(type, "_" + name, beanProperties, privateFields, config);

return null;
}

private static Property getProperty(Class<?> type, boolean beanProperties, boolean privateFields, YamlConfig config,
Field field) {
Property property = null;
if (beanProperties) {
String name = field.getName();
DeferredConstruction deferredConstruction = getDeferredConstruction(type, config);
boolean constructorProperty = deferredConstruction != null && deferredConstruction.hasParameter(name);

String nameUpper = Character.toUpperCase(name.charAt(0)) + name.substring(1);
String setMethodName = "set" + nameUpper;
String getMethodName = "get" + nameUpper;
if (field.getType().equals(Boolean.class) || field.getType().equals(boolean.class)) {
setMethodName = name.startsWith("is")
? "set" + Character.toUpperCase(name.charAt(2)) + name.substring(3)
: setMethodName;
getMethodName = name.startsWith("is") ? name : "is" + nameUpper;
}

Method getMethod = null;
Method setMethod = null;
try {
getMethod = type.getMethod("get" + nameUpper);
setMethod = type.getMethod(setMethodName, field.getType());
} catch (Exception ignored) {
}
if (getMethod == null) {
try {
getMethod = type.getMethod("is" + nameUpper);
} catch (Exception ignored) {
}
try {
getMethod = type.getMethod(getMethodName);
} catch (Exception ignored) {
}
if (getMethod != null) {
Method setMethod = null;

// field.getType() is boolean, but getMethodName is getFieldName(E.g: getBool).
if (getMethod == null && (field.getType().equals(Boolean.class) || field.getType().equals(boolean.class))) {
try {
setMethod = type.getMethod("set" + nameUpper, getMethod.getReturnType());
getMethod = type.getMethod("get" + nameUpper);
} catch (Exception ignored) {
}
if (getMethod != null && (setMethod != null || constructorProperty))
return new MethodProperty(name, setMethod, getMethod);
}
if (getMethod != null && (setMethod != null || constructorProperty)) {
return new MethodProperty(name, setMethod, getMethod);
}
}

for (Field field : getAllFields(type)) {
if (!field.getName().equals(name)) continue;
int modifiers = field.getModifiers();
if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers)) continue;
if (!Modifier.isPublic(modifiers) && !privateFields) continue;
try {
field.setAccessible(true);
} catch (Exception ignored) {
}
return new FieldProperty(field);
int modifiers = field.getModifiers();
if (!Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers)
&& (Modifier.isPublic(modifiers) || privateFields)) {
field.setAccessible(true);
property = new FieldProperty(field);
}
return null;
return property;
}

static private ArrayList<Field> getAllFields (Class type) {
Expand Down Expand Up @@ -292,15 +276,15 @@ private Class getElementTypeFromGenerics (Type type) {
if (actualTypeArguments.length > 0) {
final Type cType = actualTypeArguments[actualTypeArguments.length - 1];
if (cType instanceof Class) {
return (Class) cType;
return (Class)cType;
} else if (cType instanceof WildcardType) {
WildcardType t = (WildcardType) cType;
WildcardType t = (WildcardType)cType;
final Type bound = t.getUpperBounds()[0];
return bound instanceof Class ? (Class) bound : null;
return bound instanceof Class ? (Class)bound : null;
} else if (cType instanceof ParameterizedType) {
ParameterizedType t = (ParameterizedType) cType;
ParameterizedType t = (ParameterizedType)cType;
final Type rt = t.getRawType();
return rt instanceof Class ? (Class) rt : null;
return rt instanceof Class ? (Class)rt : null;
}
}
}
Expand Down
Loading

0 comments on commit 04b1607

Please sign in to comment.