public class BeanGenerator extends Object
Schema schema = new Schema(new DefaultConnectionFactory( "ldap://directory.ldaptive.org")); BeanGenerator generator = new BeanGenerator( schema, "com.my.package", new String[] {"inetOrgPerson"}); generator.generate(); generator.write();
Modifier and Type | Field and Description |
---|---|
private com.sun.codemodel.JCodeModel |
codeModel
Code model for java class creation.
|
private String[] |
excludedNames
Excluded names.
|
private boolean |
includeSuperiorClasses
Whether to include superior classes for each object class.
|
private Map<String,String> |
nameMappings
Name mappings.
|
private String[] |
objectClasses
Object classes to build beans for.
|
private String |
packageName
Package to create beans in.
|
private Schema |
schema
Schema to generate beans from.
|
private Map<String,Class<?>> |
typeMappings
Mapping to determine attribute value type.
|
private boolean |
useOperationalAttributes
Whether to include operational attributes.
|
private boolean |
useOptionalAttributes
Whether to include optional attributes.
|
Constructor and Description |
---|
BeanGenerator()
Default constructor.
|
BeanGenerator(Schema s,
String name,
String[] oc)
Creates a new bean generator.
|
Modifier and Type | Method and Description |
---|---|
protected com.sun.codemodel.JDefinedClass |
createClass(String classPackage,
String className)
Creates a class in the supplied package.
|
private void |
createEquals(com.sun.codemodel.JDefinedClass clazz)
Creates the equals method on the supplied class.
|
private void |
createHashCode(com.sun.codemodel.JDefinedClass clazz)
Creates the hashCode method on the supplied class.
|
protected void |
createMutators(com.sun.codemodel.JDefinedClass clazz,
String name,
Class<?> syntaxType,
boolean multivalue)
Creates the getter and setter methods on the supplied class for the supplied name.
|
private void |
createToString(com.sun.codemodel.JDefinedClass clazz)
Creates the toString method on the supplied class.
|
private String |
formatAttributeName(String name)
Formats the supplied name for use as a Java property.
|
void |
generate()
Generates a class for each configured object class.
|
private Set<String> |
getAttributeNames(ObjectClass objectClass)
Returns the attribute names to use for the supplied object class.
|
private Set<String> |
getAttributeNames(ObjectClass objectClass,
Set<ObjectClass> processed)
Returns the attribute names to use for the supplied object class.
|
protected static Map<String,Class<?>> |
getDefaultTypeMappings()
Returns the default syntax types used to determine attribute property type.
|
String[] |
getExcludedNames()
Returns the attribute names to exclude from bean generation.
|
Map<String,String> |
getNameMappings()
Returns the mapping of directory attribute name to bean property.
|
String[] |
getObjectClasses()
Returns the object classes.
|
String |
getPackageName()
Returns the package name where beans will be generated.
|
Schema |
getSchema()
Returns the schema.
|
protected Class<?> |
getSyntaxType(AttributeType type,
Syntax syntax)
Returns the class for the supplied attribute type and syntax.
|
Map<String,Class<?>> |
getTypeMappings()
Returns the type mappings.
|
boolean |
isIncludeSuperiorClasses()
Returns whether to include superior classes in bean generation.
|
private boolean |
isNameExcluded(AttributeType type)
Returns whether the supplied attribute type has a matching OID or name in the excluded names list.
|
boolean |
isUseOperationalAttributes()
Returns whether to include operational attributes in bean generation.
|
boolean |
isUseOptionalAttributes()
Returns whether to include optional attributes in bean generation.
|
static void |
main(String[] args)
Provides command line access to a
BeanGenerator . |
void |
setExcludedNames(String... names)
Sets the attribute names to exclude from bean generation.
|
void |
setIncludeSuperiorClasses(boolean b)
Sets whether to include superior classes in bean generation.
|
void |
setNameMappings(Map<String,String> m)
Sets the mapping of directory attribute name to bean property.
|
void |
setObjectClasses(String... oc)
Sets the object classes.
|
void |
setPackageName(String name)
Sets the package name where beans will be generated.
|
void |
setSchema(Schema s)
Sets the schema.
|
void |
setTypeMappings(Map<String,Class<?>> m)
Sets the type mappings.
|
void |
setUseOperationalAttributes(boolean b)
Sets whether to include operational attributes in bean generation.
|
void |
setUseOptionalAttributes(boolean b)
Sets whether to include optional attributes in bean generation.
|
void |
write()
Writes the generated classes to disk.
|
void |
write(String path)
Writes the generated classes to disk at the supplied path.
|
private final com.sun.codemodel.JCodeModel codeModel
private Schema schema
private String packageName
private String[] objectClasses
private boolean useOptionalAttributes
private boolean useOperationalAttributes
private boolean includeSuperiorClasses
private String[] excludedNames
public BeanGenerator()
public BeanGenerator(Schema s, String name, String[] oc)
s
- schema containing directory data for generationname
- package name to place the generated classes inoc
- object classes to generate beans forpublic Schema getSchema()
public void setSchema(Schema s)
s
- schemapublic String getPackageName()
public void setPackageName(String name)
name
- package namepublic String[] getObjectClasses()
public void setObjectClasses(String... oc)
oc
- object classespublic boolean isUseOptionalAttributes()
public void setUseOptionalAttributes(boolean b)
b
- whether to include optional attributespublic boolean isUseOperationalAttributes()
public void setUseOperationalAttributes(boolean b)
b
- whether to include operational attributespublic boolean isIncludeSuperiorClasses()
public void setIncludeSuperiorClasses(boolean b)
b
- whether to include superior classespublic Map<String,Class<?>> getTypeMappings()
public void setTypeMappings(Map<String,Class<?>> m)
m
- type mappingspublic Map<String,String> getNameMappings()
public void setNameMappings(Map<String,String> m)
m
- name mappingsNullPointerException
- if m is nullpublic String[] getExcludedNames()
public void setExcludedNames(String... names)
names
- to excludeNullPointerException
- if names is nullprotected static Map<String,Class<?>> getDefaultTypeMappings()
protected Class<?> getSyntaxType(AttributeType type, Syntax syntax)
type
- attribute typesyntax
- associated with the attribute typepublic void generate()
objectClasses
. write(String)
must be
invoked to write the classes to disk.private Set<String> getAttributeNames(ObjectClass objectClass)
getAttributeNames(ObjectClass, Set)
.objectClass
- to retrieve names fromprivate Set<String> getAttributeNames(ObjectClass objectClass, Set<ObjectClass> processed)
objectClass
- to retrieve names fromprocessed
- object classes that have already been processedprivate String formatAttributeName(String name)
name
- to formatprivate boolean isNameExcluded(AttributeType type)
type
- to compareprotected com.sun.codemodel.JDefinedClass createClass(String classPackage, String className)
classPackage
- to place the class inclassName
- to createIllegalArgumentException
- if the class already existsprotected void createMutators(com.sun.codemodel.JDefinedClass clazz, String name, Class<?> syntaxType, boolean multivalue)
clazz
- to put getter and setter methods onname
- of the propertysyntaxType
- of the propertymultivalue
- whether this property is a collectionprivate void createHashCode(com.sun.codemodel.JDefinedClass clazz)
LdapUtils.computeHashCode(int,
Object...)
.clazz
- to put hashCode method onprivate void createEquals(com.sun.codemodel.JDefinedClass clazz)
LdapUtils.areEqual(Object, Object)
.clazz
- to put equals method onprivate void createToString(com.sun.codemodel.JDefinedClass clazz)
clazz
- to put toString method onpublic void write() throws IOException
write(String)
with ".".IOException
- if the write failspublic void write(String path) throws IOException
path
- to write the classes toIOException
- if the write failspublic static void main(String[] args) throws Exception
BeanGenerator
. Expects two arguments:
A sample configuration property file looks like:
org.ldaptive.packageName=my.package.ldap.beans org.ldaptive.objectClasses=eduPerson org.ldaptive.nameMappings=c=countryName,l=localityName org.ldaptive.excludedNames=userPassword org.ldaptive.ldapUrl=ldap://directory.ldaptive.org
args
- command line argumentsException
- if any error occursCopyright © 2003-2019 Virginia Tech. All Rights Reserved.