This repository has been archived on 2022-12-20. You can view files and clone it, but cannot push or open issues or pull requests.
sdm01/report.tex

287 lines
12 KiB
TeX
Raw Normal View History

% vim: set ts=2 sw=2 et tw=80:
2022-10-19 09:35:50 +00:00
\documentclass[10pt,a4paper]{scrartcl}
\usepackage[utf8]{inputenc}
2022-10-19 09:35:50 +00:00
\usepackage[margin=2.25cm]{geometry}
\usepackage{hyperref}
2022-10-19 08:03:50 +00:00
\usepackage{listings}
\usepackage{xcolor}
\usepackage{lmodern}
\usepackage{listings}
\setlength{\parindent}{0cm}
2022-10-19 09:35:50 +00:00
\setlength{\parskip}{0.3em}
\hypersetup{pdfborder={0 0 0}}
\usepackage[nomessages]{fp}
\renewcommand*{\titlepagestyle}{empty}
2022-10-19 08:03:50 +00:00
\lstset{
basicstyle=\small\ttfamily,
%frame=shadowbox,
rulesepcolor=\color{black},
columns=fullflexible,
commentstyle=\color{gray},
keywordstyle=\color{blue},
mathescape=true,
aboveskip=1em,
captionpos=b,
abovecaptionskip=1em,
belowcaptionskip=1em
}
\title{Assginment 1 -- Software Design and Modelling}
\author{Volodymyr Karpenko \and Claudio Maggioni}
\begin{document}
2022-10-19 09:35:50 +00:00
\begin{titlepage}
\maketitle
2022-10-19 09:35:50 +00:00
\pagenumbering{roman}
\tableofcontents
\lstlistoflistings
\end{titlepage}
\section{Project selection process}
2022-10-19 09:35:50 +00:00
\pagenumbering{arabic}
We need to find a project that is a single unit in terms of compilation
2022-10-19 12:33:44 +00:00
modules\footnote{A problem for Pattern4J as compiled \textit{.class} files are
distributed across several directories and would have to be merged manually for
analyzing them}
self contained and with as little external dependencies as possible to ease the
analysis project. Additionally, it would be nice if we choose a project that we
already know as library clients.
\subsection {Projects Considered}
2022-10-17 12:10:39 +00:00
We considered the following GitHub repositories:
\begin{description}
\item[vavr-io/vavr] a Java library for functional programming, discarded as
the project is less than 20K LOC and doesn't meet the selection criteria;
\item[bitcoin4j/bitcoin4j] a Java implementation of the bitcoin protocol,
discarded as the project is distributed in several subprojects;
2022-10-17 12:10:39 +00:00
\item[FasterXML/jackson-core] a Java JSON serialization and
deserialization library. We chose this library because it meets the
selection criteria, it doesn't rely on external components for its
execution, and its project structure uses a single Maven module for its
sources and thus easy to analyze.
\end{description}
2022-10-17 12:10:39 +00:00
\subsection {The Jackson Core Library}
2022-10-19 12:33:44 +00:00
As already mentioned, \textit{Jackson} is a library that offers serialization
2022-10-17 12:10:39 +00:00
and deseralization capabilities in JSON format. The library is highly extensible
and customizable through a robust but flexible API and module suite that allows
to change the serialization and deserialization rules, or in the case of the
2022-10-19 12:33:44 +00:00
\textit{jackson-dataformat-xml} module, to allow to target XML instead of JSON.
2022-10-17 12:10:39 +00:00
The chosen repository contains only the \textit{core} module of Jackson. The
\textit{core} module implements the necessary library abstractions and
interfaces to allow other modules to be plugged-in. Additionally, the
\textit{core} module implements the tokenizer and low-level abstractions to work
with the JSON format.
2022-10-19 09:35:50 +00:00
We chose to analyze version 2.13.4 of the module (i.e.\ the code
2022-10-19 12:33:44 +00:00
under the git tag \textit{jackson-core-2.13.4}) because it is the latest stable
2022-10-17 12:10:39 +00:00
version available at the time of writing.
2022-10-18 11:30:26 +00:00
\section{Analysis Implementation}
We use
2022-10-19 12:33:44 +00:00
\href{https://users.encs.concordia.ca/~nikolaos/pattern\_detection.html}{\textit{Pattern4J}}
as a pattern detection tool. This tool needs compiled \textit{.class} files in
order to perform analysis. Therefore, as \textit{jackson-core} is a standard
Maven project, we compile the sources using the command \textit{mvn clean
compile}. The \textit{pom.xml} of the library specifies Java 1.6 as a
compilation target, which is not supported by JDK 17 or above. We used JDK 11
instead, as it is the previous LTS version.
An XML dump of the \textit{Pattern4j} analysis results are included in the
2022-10-19 12:33:44 +00:00
submission as the file \textit{analysis.xml}.
2022-10-18 11:30:26 +00:00
\section{Structural Patterns}
\subsection{Singleton Pattern}
2022-10-18 09:14:17 +00:00
Lots of false positives for the Singleton pattern. Example,
com.fasterxml.jackson.core.sym.Name1 has a package private constructor and a
public static final instance of it, but reading the documentation the class
represents (short) JSON string literals and therefore is clearly
initialized by client code.
2022-10-17 12:10:39 +00:00
2022-10-18 09:14:17 +00:00
(com.fasterxml.jackson.core omitted for brevity)
2022-10-17 12:10:39 +00:00
2022-10-18 09:14:17 +00:00
\begin{description}
\item[sym.Name1, JsonLocation, DefaultIndenter,
util.DefaultPrettyPrinter\$FixedSpaceIndenter] not a singleton (detected
cause of "convenient" default instance given as static final field), the
constructor is not used but the class is extensible
\item[JsonPointer, filter.TokenFilter] like above, but constructors are protected
\item[JsonpCharacterEscapes, util.DefaultPrettyPrinter\$NopIndenter,
Version] a singleton but with a public constructor that is never called
in the module code, may be called in tests
\item[io.JsonStringEncoder] like above, but the class is final
\item[util.InternCache, io.CharTypes\$AltEscapes]
actual singleton, thread-unsafe initialization
\item[io.ContentReference] like above, but constructor is protected
\end{description}
\subsection{Abstract Factory Pattern}
2022-10-19 12:33:44 +00:00
\textit{Pattern4J} detects only two instances of the abstract factory pattern:
2022-10-18 09:14:17 +00:00
\begin{description}
\item[TokenStreamFactory] which indeed is a factory for \textbf{JsonParser} and
\textbf{JsonGenerator} objects, although two overloaded factory methods
exist on this class (one for each class) catering for different combination of
arguments. A concrete implementation of this factory is included in the form
of the \textbf{JsonFactory} class, although other modules may add additional
implementations to cater for different encodings (like the
2022-10-19 12:33:44 +00:00
\textit{jackson-dataformat-xml} module for XML);
2022-10-18 09:14:17 +00:00
\item[TSFBuilder] which is also a factory for concrete implementations of
\textbf{TokenStreamFactory} to allow slight changes in the serialization and
deserialization rules (e.g. changing the quote character used in JSON keys
2022-10-19 12:33:44 +00:00
from \textit{"} to \textit{'}). Like \textbf{TokenStreamFactory}, this class
2022-10-18 09:14:17 +00:00
is only implemented by one class, namely \textbf{JsonFactoryBuilder}, whitin
the scope of this module. And as mentioned previously, this abstract factory
is also likely to be extended by concrete implementations in other
\textit{Jackson} modules.
\end{description}
\subsection{Builder Pattern}
The builder pattern does not seem to be analyzed by
2022-10-19 12:33:44 +00:00
\textit{Pattern4J}, as the analysis output does not mention the pattern, even
2022-10-18 09:14:17 +00:00
just to report that no instances of it have been found (as it is the case with
other patterns, e.g. the observer pattern). A manual search in the source code
produced the following results:
\begin{description}
\item[TSFBuilder] is also a builder other than an abstract factory. As mentioned
previously, this class allows to alter slightly the serialization and
deserialization rules used to build outputtting \textbf{JsonFactory}
objects. Each rule is represented by an object or enum instance implementing
2022-10-19 12:33:44 +00:00
the \textbf{util.JacksonFeature} interface. \textit{TSFBuilder} then
2022-10-18 09:14:17 +00:00
provides several overloaded methods to enable and disable features
represented by the interface. Enabled features are stored in several
2022-10-19 12:33:44 +00:00
bitmask \textit{protected int} fields, which are then directly accessed by
2022-10-18 09:14:17 +00:00
the constructor of the \textbf{TokenStreamFactory} concrete implementation
to build;
2022-10-19 09:35:50 +00:00
\marginpar[right text]{\color{white}\url{https://youtu.be/72b2nH-kdbU}}
2022-10-18 09:14:17 +00:00
\item[JsonFactoryBuilder] an concrete factory implementation of
\textbf{TSFBuilder} that builds \textbf{JsonFactory} instances;
2022-10-19 12:33:44 +00:00
\item[util.ByteArrayBuilder] provides facilities to build \textit{byte[]} objects
2022-10-18 09:14:17 +00:00
of varying length, akin to \textbf{StringBuilder} building \textbf{String}
objects. This is not a strict implementation of the builder pattern per se
(as Java arrays do not have a ``real'' constructor),
but it is nevertheless included since the features it exposes (namely
dynamic sizing while building) are decoupled by the underlying (fixed-size)
array representation.
\end{description}
2022-10-18 11:30:26 +00:00
\section{Creational Patterns}
\subsection{Adapter Pattern}
TBD
2022-10-19 09:35:50 +00:00
\subsection{Decorator Pattern}
Only in Pattern4j
2022-10-18 11:30:26 +00:00
\subsection{Bridge Pattern}
TBD
\subsection{Composite Pattern}
2022-10-19 09:35:50 +00:00
None found
2022-10-18 11:30:26 +00:00
\subsection{Facade Pattern}
2022-10-19 12:33:44 +00:00
TBD -- \textit{Pattern4J} does not detect this pattern
2022-10-18 11:30:26 +00:00
\subsection{Proxy Pattern}
2022-10-19 09:35:50 +00:00
None found
2022-10-18 11:30:26 +00:00
\section{Behavioral Patterns}
\subsection{Command Pattern}
2022-10-19 09:35:50 +00:00
None found
2022-10-18 11:30:26 +00:00
\subsection{Observer Pattern}
2022-10-19 09:35:50 +00:00
None found
2022-10-18 11:30:26 +00:00
\subsection{Strategy Pattern}
2022-10-19 09:35:50 +00:00
None found
\subsection{State Pattern}
2022-10-19 12:33:44 +00:00
Among the design patterns \textit{Pattern4J} detects, the state pattern is
2022-10-19 09:35:50 +00:00
detected in 5 classes. The state pattern is a variation of the strategy pattern
where the concrete strategy used by the matching context is determined by the
state of a finite state machine the context class implements. In other words,
the state pattern chooses the concrete strategy to use through the state of the
context.
2022-10-19 12:33:44 +00:00
\begin{description}
\item[util.DefaultPrettyPrinter] false positive, strategy pattern;
\item[JsonFactory] false positive, \textit{InputDecorator \_inputDecorator},
\textit{OutputDecorator \_outputDecorator}, \textit{SerializedString
\_rootValueSeparator} are strategy instances (more
specifically a way to pre-process input before Jackson parses it, labeled as
``decorator'' by Jackson developers but not really a decorator pattern
application since InputDecorator is not a subclass of any component to
decorate);
\item[json.WriterBasedJsonGenerator] false positive,
\textit{SerializableString \_currentEscape} is a simple \textit{String}-like
object that gets updated based on the parsing state.
\item[util.DefaultPrettyPrinter] ??? WIP
\end{description}
2022-10-19 09:35:50 +00:00
2022-10-19 12:33:44 +00:00
WIP instances and examples
2022-10-18 11:30:26 +00:00
\subsection{Template Method Pattern}
2022-10-19 08:03:50 +00:00
Due to the extendibility of Jackson, it is of no surprise that the template
method pattern is used extensively to create a class hierarchy that provides
2022-10-19 12:33:44 +00:00
rich interfaces while maintaining behavioural flexibility. \textit{Pattern4J}
2022-10-19 08:03:50 +00:00
correctly detects several instances of the pattern, namely
\textbf{JsonStreamContext}, \textbf{JsonGenerator}, \textbf{type.ResolvedType},
\textbf{JsonParser}, \textbf{base.ParserBase}, \textbf{base.GeneratorBase},
\textbf{base.ParserMinimalBase}. All these classes implement several concrete
2022-10-19 12:33:44 +00:00
\textit{public} methods throwgh the use of \textit{protected abstract} methods.
2022-10-19 09:35:50 +00:00
Although the concrete (i.e.\ the template) methods are usually not vary complex
2022-10-19 08:03:50 +00:00
(as the pattern example shown in class), they still follow the principles of the
template method pattern. We show as an example some template methods found in
\textbf{base.ParserBase}:
2022-10-19 12:33:44 +00:00
\begin{lstlisting}[caption=Template method \textit{void close()} and step
methods \textit{void \_closeInput()} and \textit{void \_releaseBuffers()} in
2022-10-19 08:03:50 +00:00
\textbf{base.ParserBase}., language=java]
@Override public void close() throws IOException {
if (!_closed) {
// 19-Jan-2018, tatu: as per [core#440] need to ensure no more data
// assumed available
_inputPtr = Math.max(_inputPtr, _inputEnd);
_closed = true;
try {
_closeInput();
} finally {
// as per [JACKSON-324], do in finally block
// Also, internal buffer(s) can now be released as well
_releaseBuffers();
}
}
}
protected abstract void _closeInput() throws IOException;
protected void _releaseBuffers() throws IOException {
/* implementation omitted */
}
\end{lstlisting}
Here the pattern is slightly modified by providing a default implementation of
2022-10-19 12:33:44 +00:00
\textit{void \_releaseBuffers()}. In this case, child classes occasionally
override the method with a body first calling \textit{super()} and then adding
2022-10-19 08:03:50 +00:00
additional buffer release code after.
2022-10-18 11:30:26 +00:00
\subsection{Visitor Pattern}
2022-10-19 09:35:50 +00:00
None found
2022-10-18 09:14:17 +00:00
\end{document}