Skip to content

Commit

Permalink
completed class beginning
Browse files Browse the repository at this point in the history
  • Loading branch information
sporniket committed Nov 28, 2017
1 parent 74fe18b commit acfd4de
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,54 @@ private List<FieldSpecs> extractFields(ClassDoc from, Map<String, String> transl
.collect(toList());
}

private String extractInterfaceList(ClassDoc from, Map<String, String> translations, Set<String> shortables)
{
StringBuilder _result = new StringBuilder();
final ClassDoc[] _interfaces = from.interfaces();
if (_interfaces.length > 0)
{
for (int _i = 0; _i < _interfaces.length; _i++)
{
_result.append((0 == _i) ? "" : ", ")
.append(computeOutputClassname(_interfaces[_i].name(), translations, shortables));
}
}

return _result.toString();
}

public ClassSpecs extractSpecs(ClassDoc from, Map<String, String> translations, DocletOptions options)
{
final Collection<ImportSpecs> _knownClasses = updateKnownClasses(from);

Map<String, String> _shortNameMapping = new HashMap<>(_knownClasses.size() + translations.size());
updateShortClassnameMappingFromClassnames(_shortNameMapping, _knownClasses.stream().map(ImportSpecs::getClassName).collect(toList()));
updateShortClassnameMappingFromClassnames(_shortNameMapping,
_knownClasses.stream().map(ImportSpecs::getClassName).collect(toList()));
updateShortClassnameMappingFromClassnames(_shortNameMapping, translations.values());
Set<String> _shortables = new HashSet<>(_shortNameMapping.values());

return new ClassSpecs_Builder()//
.withImports(_knownClasses)//
.withClassName(computeOutputClassname(from.qualifiedTypeName(), translations, _shortables))//
.withDeclaredTypeArguments(extractClassDeclaredTypeArguments(from, translations, _shortables))//
.withInvokedTypeArguments(extractClassInvokedTypeArguments(from, translations, _shortables))//
.withFields(extractFields(from, translations, _shortables, options))//
.withAbstractRequired(shouldBeAbstract(from))
.withAbstractRequired(shouldBeAbstract(from))//
.withSuperClassName(extractSuperClassName(from.superclass(), translations, _shortables))//
.withInterfaceList(extractInterfaceList(from, translations, _shortables))//
.done();
}

private String extractSuperClassName(ClassDoc superclass, Map<String, String> translations, Set<String> shortables)
{
StringBuilder _result = new StringBuilder();
if (!Object.class.getName().equals(superclass.qualifiedName()))
{
_result//
.append(computeOutputClassname(superclass.qualifiedTypeName(), translations, shortables))//
.append(extractClassInvokedTypeArguments(superclass, translations, shortables));
}

return _result.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.sporniket.libre.javabeans.doclet.JavabeanGenerator;
import com.sporniket.libre.javabeans.doclet.UtilsFieldname;
import com.sporniket.libre.javabeans.doclet.codespecs.FieldSpecs;
import com.sporniket.libre.lang.string.StringTools;
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.FieldDoc;
Expand Down Expand Up @@ -43,57 +44,37 @@
*/
public class BasicJavabeanGenerator extends BasicGenerator implements JavabeanGenerator
{
private void outputAccessor(FieldDoc field)
private void outputAccessor(FieldSpecs field)
{
final boolean _noPrefix = StringTools.isEmptyString(getOptions().getBeanFieldPrefix());
final String _fieldPrefix = _noPrefix ? "this." : getOptions().getBeanFieldPrefix();
final String _accessorSuffix = UtilsFieldname.computeFieldAccessorSuffix(field.name());
final String _fieldSuffix = _noPrefix ? field.name() : UtilsFieldname.computeFieldAccessorSuffix(field.name());
final String _type = computeOutputType_invocation(field.type(), getTranslations(), getShortables());

// getter
getOut().printf(" public %s get%s() {return %s%s ;}\n", _type, _accessorSuffix, _fieldPrefix, _fieldSuffix);
getOut().printf(" public %s get%s() {return %s%s ;}\n", field.getTypeInvocation(), field.getNameForAccessor(),
field.getFieldPrefix(), field.getNameForField());

// setter
getOut().printf(" public void set%s(%s value) {%s%s = value;}\n", _accessorSuffix, _type, _fieldPrefix, _fieldSuffix);
getOut().printf(" public void set%s(%s value) {%s%s = value;}\n", field.getNameForAccessor(), field.getTypeInvocation(),
field.getFieldPrefix(), field.getNameForField());

getOut().println();
}

@Override
public void outputAccessors()
{
getAccessibleDeclaredFields(getSource()).forEach(_field -> {
outputAccessor(_field);
});

getClassSpecs().getFields().stream().filter(f -> f.getDirectlyRequired()).forEach(f -> outputAccessor(f));
}

@Override
public void outputClassBegin()
{
final StringBuilder _classDecl = new StringBuilder(
shouldBeAbstract(getSource()) ? "public abstract class " : "public class ");
outputClassName__classDeclaration(_classDecl, getSource(), getTranslations(), getShortables());

final String _supername = getSource().superclass().qualifiedName();
if (!Object.class.getName().equals(_supername))
{
_classDecl.append(" extends ").append(computeOutputClassname(_supername, getTranslations(), getShortables()));
}
final ClassDoc[] _interfaces = getSource().interfaces();
if (_interfaces.length > 0)
{
for (int _i = 0; _i < _interfaces.length; _i++)
{
_classDecl.append((0 == _i) ? " implements " : ", ")
.append(computeOutputClassname(_interfaces[_i].name(), getTranslations(), getShortables()));
}
}

getOut().printf(_classDecl.append("\n{\n").toString());

getOut().println();
// last preparations
String _abstractMarker = getClassSpecs().getAbstractRequired() ? " abstract" : "";
String _extendsMarker = StringTools.isEmptyString(getClassSpecs().getSuperClassName()) ? "" : "\n extends ";
String _implementsMarker = StringTools.isEmptyString(getClassSpecs().getInterfaceList()) ? "" : "\n implements ";

getOut().printf("public%s class %s %s%s%s%s\n{\n\n", //
_abstractMarker, getClassSpecs().getDeclaredTypeArguments()//
,_extendsMarker, getClassSpecs().getSuperClassName()//
,_implementsMarker, getClassSpecs().getInterfaceList());
}

private void outputField(FieldDoc field)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@

class ClassSpecsRaw
{
Boolean abstractRequired;

List<AnnotationSpecsRaw> annotations;

/**
* Class simple name (without package).
*/
String className;

/**
* Type arguments when defining the class (<code>class</code> keyword).
*/
String declaredTypeArguments;

List<FieldSpecsRaw> fields;

Collection<ImportSpecsRaw> imports;
Expand All @@ -22,9 +29,14 @@ class ClassSpecsRaw
String invokedTypeArguments;

/**
* Class simple name (without package).
* (NOT NULL) Simple name of the extended class (MUST be imported), or empty if the super class is <code>Object</code>.
*/
String className;
String superClassName ;

Boolean abstractRequired;
/**
* (NOT NULL) Comma separated values of interfaces names.
*
* Each name SHOULD be simple, unless the type name is not in the list of shortable types.
*/
String interfaceList ;
}

0 comments on commit acfd4de

Please sign in to comment.