SlideShare una empresa de Scribd logo
1 de 43
Descargar para leer sin conexión
The Java Chamber of Horrors
Andreas Zitzelsberger
@andreasz82
QAware
1≅0
if (true == lUsingHttps) {
// ...
}
if (true == lUsingHttps) {
//..
if (lUsingSSLContext) { /* ... */ }
//..
if (true == lUsingHttps) {
if (true == lUsingSSLContext) { /* ... */ }
}
// ...
}
Better double
check!
Interleave
control flows
„true".equals(request.getParameter("statsRequired"))
? true: false
Make the result
extra-boolean
If only there was a
parseBoolean
method
if (dateCache.after(dateUser) || dateCache.equals(dateUser)) {
return false;
} else {
return true;
}
if (dateCache.after(dateUser) || dateCache.equals(dateUser)) {
return true;
} else {
return false;
}
Strings!
if (clazz != null) {
header.concat(LayoutConst.CLASS_NAME_STR);
header.concat(clazz.getName());
header.concat(LayoutConst.NEW_LINE);
}
Early implementation of
github.com/kelseyhightower/noco
de
public final static String NAME = new String("PIE DIAGRAM");
String surplus = new String(" ");
new String(Long.toString(rightPoint));
new String("col" + String.valueOf(z + 2));
Me want objects!
GC
Objects.equals(
query.getHost(),
hostName)
Which of these are equal?
• Localhost
• LoCaLhOsT
• localhost.localdomain
• 127.0.0.1
• 127.0.0.2
• 192.168.178.5
• my-computer
• my-computer.my-domain
Exceptional
exception
handling
} catch (Exception ex) {
throw ex;
}
} catch (Exception e) {
// improbable exceptions, throw a runtime exception
// to avoid the declaration in the method-signature
throw new RuntimeException( "... failed", e);
}
} catch (Exception ex) {
// this exception will be ignored
// because it cannot be sensibly handled
;
} Why is there a
semicolon?
} catch (SQLException sqlException) {
; // to be compliant with rule NoEmptyCatchBlocks
}
Ah, that’s why!
try {
Thread.sleep(retryWaitTime);
} catch (InterruptedException e) {
; // nothing to do
}
Unless you want your
thread to be able to
shut down …
try {
nextLogger.setLogger(
logManager.currentLogger(nextLoggerName));
loggerTable.remove(nextLoggerName);
} catch (Throwable t) {
failed = true;
}
@SuppressFBWarnings(value=„REC_CATCH_EXCEPTION“,
justification="silly rule")
Security!
HttpServletResponseWrapper {
/* ... */
public void setHeader(String arg0, String arg1) {
// in this case do nothing here,
// because we are setting it in the filter
}
public void setContentType(String arg0) {
// in this case do nothing here,
// because we are setting it in the filter
}
Breaks contract of
HttpServletResponse
public class PasswordEncryptor {
private static final byte[] ALGO = new byte[]{68, 69, 83};
public static String encrypt(String str) throws
BadPaddingException, IllegalBlockSizeException,
NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, UnsupportedEncodingException {
// ...
Cipher cipher = Cipher.getInstance(new String(ALGO));
cipher.init(1, new SecretKeySpec(key,
new String(ALGO)));
// ...
}
}
D E S
Misoverengineerin
g!
public class ServiceInvocationHandler implements InvocationHandler {
private final BusinessService underlyingBusinessService;
private final BusinessService derivedFromBusinessService;
public Object invoke(/* ... */ ) throws Throwable {
/* ... */
}
public static Object getService(/* ... */) {
/* ... */
}
}
Let’s call these
delegate and
caller for the sake
of clarity…
public Object invoke(final Object proxy, final Method method,
final Object[] args) throws Throwable {
try {
if (caller == null)
delegate.setSession(HibernateUtil.getSessionFactory()
.getCurrentSession());
else {
Assert.isNotNull(caller.getSession());
delegate.setSession(caller.getSession());
}
delegate.beginTransaction();
final Object ret = method.invoke(delegate, args);
// continued ...
No Caller → New
Hibernate session
Otherwise, use
the caller’s
session
Wait, what if there
already is an open
transaction?
if (caller == null)
delegate.commit();
if (delegate instanceof ServiceWithAssociatedThread
&& ((ServiceWithAssociatedThread) delegate)
.hasAssociatedThread()) {
((ServiceWithAssociatedThread) delegate)
.startAssociateThread();
((ServiceWithAssociatedThread) delegate)
.removeAssociateThread();
}
return ret;
} catch (final Throwable throwable) {
// continued …
}
Unfinished
transaction if
there is a caller
What could go
wrong?
} catch (final Throwable throwable) {
Logger.error(ServiceInvocationHandler.class,
„Error ...", throwable);
if (caller == null && delegate.hasTransaction())
delegate.rollback();
else if (caller != null && caller.hasTransaction())
caller.rollback();
throw throwable.getCause();
} finally {
if (delegate.hasSession()) {
//if (caller != null)
// delegate.close();
delegate.setSession(null);
}
Wait, what?
Wait for the GC
to clean up after
us
Modify stack trace,
possible
NullPointerException
Integration
Business
What (probably) has happened
Presentation
Service Service Service
Transaction Facade
The setting:
A massive custom framework for
web apps.
Think JEE built in-house
protected void reset() {
String[] classesForReset =
{
„com.acmecorp...legitimaton...XService",
„com.acmecorp...legitimaton...YService",
"com.acmecorp...legitimaton...ZService",
// ...
};
// continued ...
Boss music
starts…
for (int i = 0; i < classesForReset.length; i++) {
try {
Class class = class.forName(classesForReset[i]);
Method method = class.getDeclaredMethod(
„reset", new Class[0]);
method.invoke(null, null);
} catch Exception e) {
System.err.println(/* ... */);
e.printStackTrace(System.err);
}
}
Call a static reset
method on each class
for each request
How bad can it be?
public static void reset() {
ldapFilters = new HashMap();
}
Filters for per-
user LDAP
queries
public static void reset() {
sslSocketFactory = null;
}
New connections
for each request
What (probably) has happened
• Stored session state in static variables (maybe a former C programmer?)
• We need a reset() method, but it has to be static
• Can’t use an interface for static methods
• Call them by reflection
• Forget about this code and have it run in production...
• ...for over 10 years
• ...in almost 300 applications
What language do you want to
have bad code in?
Thank you!
@andreasz82
Bonus:
More bad code
private static void copyHashMap(HashMap<String,String> source, HashMap<String,String> destination) {
if ((source != null) && (destination != null)) {
if (!destination.isEmpty()) {
destination.clear();
}
for(HashMap.Entry<String, String> entry: source.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
destination.put(new String(key), new String(value));
}
}
}
return super.isIdentical(msg) && germanMsg != null ?
germanMsg.getMsg().equals(msg.getMsg(Locale.GERMAN)):
true && defaultMsg != null ?
defaultMsg.getMsg().equals(msg.getMsg(DEFAULT_LOCALE)):
true;
public AcmeCorpInteger(int intValue) {
this();
try {
setValue(new BigInteger("" + intValue));
} catch (AcmeCorpDatatypeConditionError ex) {
}
}
Integer enterpriseNo = new Integer(
enterpriseDTO.getEnterpriseNo().getValue().intValue());
setCookie(
(new Long(userSession.getAccessTime())).toString(),
response);
public URLConnection(String[] urls, int maxRetries, long waitMs) {
super(getUrlFromString(urls[0]));
if ((null == urls) || (urls.length <= 0)) {
throw new RuntimeException("empty urls");
}
// ...
}
//NOBUG
//Only to satisfy constructor

Más contenido relacionado

Más de QAware GmbH

Cloud Migration mit KI: der Turbo
Cloud Migration mit KI: der Turbo Cloud Migration mit KI: der Turbo
Cloud Migration mit KI: der Turbo QAware GmbH
 
Migration von stark regulierten Anwendungen in die Cloud: Dem Teufel die See...
 Migration von stark regulierten Anwendungen in die Cloud: Dem Teufel die See... Migration von stark regulierten Anwendungen in die Cloud: Dem Teufel die See...
Migration von stark regulierten Anwendungen in die Cloud: Dem Teufel die See...QAware GmbH
 
Aus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-Cluster
Aus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-Cluster Aus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-Cluster
Aus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-Cluster QAware GmbH
 
Endlich gute API Tests. Boldly Testing APIs Where No One Has Tested Before.
Endlich gute API Tests. Boldly Testing APIs Where No One Has Tested Before.Endlich gute API Tests. Boldly Testing APIs Where No One Has Tested Before.
Endlich gute API Tests. Boldly Testing APIs Where No One Has Tested Before.QAware GmbH
 
Kubernetes with Cilium in AWS - Experience Report!
Kubernetes with Cilium in AWS - Experience Report!Kubernetes with Cilium in AWS - Experience Report!
Kubernetes with Cilium in AWS - Experience Report!QAware GmbH
 
50 Shades of K8s Autoscaling
50 Shades of K8s Autoscaling50 Shades of K8s Autoscaling
50 Shades of K8s AutoscalingQAware GmbH
 
Kontinuierliche Sicherheitstests für APIs mit Testkube und OWASP ZAP
Kontinuierliche Sicherheitstests für APIs mit Testkube und OWASP ZAPKontinuierliche Sicherheitstests für APIs mit Testkube und OWASP ZAP
Kontinuierliche Sicherheitstests für APIs mit Testkube und OWASP ZAPQAware GmbH
 
Service Mesh Pain & Gain. Experiences from a client project.
Service Mesh Pain & Gain. Experiences from a client project.Service Mesh Pain & Gain. Experiences from a client project.
Service Mesh Pain & Gain. Experiences from a client project.QAware GmbH
 
50 Shades of K8s Autoscaling
50 Shades of K8s Autoscaling50 Shades of K8s Autoscaling
50 Shades of K8s AutoscalingQAware GmbH
 
Blue turns green! Approaches and technologies for sustainable K8s clusters.
Blue turns green! Approaches and technologies for sustainable K8s clusters.Blue turns green! Approaches and technologies for sustainable K8s clusters.
Blue turns green! Approaches and technologies for sustainable K8s clusters.QAware GmbH
 
Per Anhalter zu Cloud Nativen API Gateways
Per Anhalter zu Cloud Nativen API GatewaysPer Anhalter zu Cloud Nativen API Gateways
Per Anhalter zu Cloud Nativen API GatewaysQAware GmbH
 
Aus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-Cluster
Aus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-Cluster Aus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-Cluster
Aus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-Cluster QAware GmbH
 
How to speed up Spring Integration Tests
How to speed up Spring Integration TestsHow to speed up Spring Integration Tests
How to speed up Spring Integration TestsQAware GmbH
 
Aus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-Cluster
Aus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-ClusterAus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-Cluster
Aus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-ClusterQAware GmbH
 
Cloud Migration – Eine Strategie die funktioniert
Cloud Migration – Eine Strategie die funktioniertCloud Migration – Eine Strategie die funktioniert
Cloud Migration – Eine Strategie die funktioniertQAware GmbH
 
Policy Driven Microservices mit Open Policy Agent
Policy Driven Microservices mit Open Policy AgentPolicy Driven Microservices mit Open Policy Agent
Policy Driven Microservices mit Open Policy AgentQAware GmbH
 
Make Developers Fly: Principles for Platform Engineering
Make Developers Fly: Principles for Platform EngineeringMake Developers Fly: Principles for Platform Engineering
Make Developers Fly: Principles for Platform EngineeringQAware GmbH
 
Security Lab: OIDC in der Praxis
Security Lab: OIDC in der PraxisSecurity Lab: OIDC in der Praxis
Security Lab: OIDC in der PraxisQAware GmbH
 
Die nächsten 100 Microservices
Die nächsten 100 MicroservicesDie nächsten 100 Microservices
Die nächsten 100 MicroservicesQAware GmbH
 
Enterprise-level Kubernetes Security mit Open Source Tools - geht das?
Enterprise-level Kubernetes Security mit Open Source Tools - geht das?Enterprise-level Kubernetes Security mit Open Source Tools - geht das?
Enterprise-level Kubernetes Security mit Open Source Tools - geht das?QAware GmbH
 

Más de QAware GmbH (20)

Cloud Migration mit KI: der Turbo
Cloud Migration mit KI: der Turbo Cloud Migration mit KI: der Turbo
Cloud Migration mit KI: der Turbo
 
Migration von stark regulierten Anwendungen in die Cloud: Dem Teufel die See...
 Migration von stark regulierten Anwendungen in die Cloud: Dem Teufel die See... Migration von stark regulierten Anwendungen in die Cloud: Dem Teufel die See...
Migration von stark regulierten Anwendungen in die Cloud: Dem Teufel die See...
 
Aus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-Cluster
Aus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-Cluster Aus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-Cluster
Aus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-Cluster
 
Endlich gute API Tests. Boldly Testing APIs Where No One Has Tested Before.
Endlich gute API Tests. Boldly Testing APIs Where No One Has Tested Before.Endlich gute API Tests. Boldly Testing APIs Where No One Has Tested Before.
Endlich gute API Tests. Boldly Testing APIs Where No One Has Tested Before.
 
Kubernetes with Cilium in AWS - Experience Report!
Kubernetes with Cilium in AWS - Experience Report!Kubernetes with Cilium in AWS - Experience Report!
Kubernetes with Cilium in AWS - Experience Report!
 
50 Shades of K8s Autoscaling
50 Shades of K8s Autoscaling50 Shades of K8s Autoscaling
50 Shades of K8s Autoscaling
 
Kontinuierliche Sicherheitstests für APIs mit Testkube und OWASP ZAP
Kontinuierliche Sicherheitstests für APIs mit Testkube und OWASP ZAPKontinuierliche Sicherheitstests für APIs mit Testkube und OWASP ZAP
Kontinuierliche Sicherheitstests für APIs mit Testkube und OWASP ZAP
 
Service Mesh Pain & Gain. Experiences from a client project.
Service Mesh Pain & Gain. Experiences from a client project.Service Mesh Pain & Gain. Experiences from a client project.
Service Mesh Pain & Gain. Experiences from a client project.
 
50 Shades of K8s Autoscaling
50 Shades of K8s Autoscaling50 Shades of K8s Autoscaling
50 Shades of K8s Autoscaling
 
Blue turns green! Approaches and technologies for sustainable K8s clusters.
Blue turns green! Approaches and technologies for sustainable K8s clusters.Blue turns green! Approaches and technologies for sustainable K8s clusters.
Blue turns green! Approaches and technologies for sustainable K8s clusters.
 
Per Anhalter zu Cloud Nativen API Gateways
Per Anhalter zu Cloud Nativen API GatewaysPer Anhalter zu Cloud Nativen API Gateways
Per Anhalter zu Cloud Nativen API Gateways
 
Aus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-Cluster
Aus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-Cluster Aus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-Cluster
Aus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-Cluster
 
How to speed up Spring Integration Tests
How to speed up Spring Integration TestsHow to speed up Spring Integration Tests
How to speed up Spring Integration Tests
 
Aus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-Cluster
Aus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-ClusterAus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-Cluster
Aus blau wird grün! Ansätze und Technologien für nachhaltige Kubernetes-Cluster
 
Cloud Migration – Eine Strategie die funktioniert
Cloud Migration – Eine Strategie die funktioniertCloud Migration – Eine Strategie die funktioniert
Cloud Migration – Eine Strategie die funktioniert
 
Policy Driven Microservices mit Open Policy Agent
Policy Driven Microservices mit Open Policy AgentPolicy Driven Microservices mit Open Policy Agent
Policy Driven Microservices mit Open Policy Agent
 
Make Developers Fly: Principles for Platform Engineering
Make Developers Fly: Principles for Platform EngineeringMake Developers Fly: Principles for Platform Engineering
Make Developers Fly: Principles for Platform Engineering
 
Security Lab: OIDC in der Praxis
Security Lab: OIDC in der PraxisSecurity Lab: OIDC in der Praxis
Security Lab: OIDC in der Praxis
 
Die nächsten 100 Microservices
Die nächsten 100 MicroservicesDie nächsten 100 Microservices
Die nächsten 100 Microservices
 
Enterprise-level Kubernetes Security mit Open Source Tools - geht das?
Enterprise-level Kubernetes Security mit Open Source Tools - geht das?Enterprise-level Kubernetes Security mit Open Source Tools - geht das?
Enterprise-level Kubernetes Security mit Open Source Tools - geht das?
 

Último

Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorTier1 app
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITmanoharjgpsolutions
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4jGraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4jNeo4j
 
What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesVictoriaMetrics
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingShane Coughlan
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...OnePlan Solutions
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogueitservices996
 
Data modeling 101 - Basics - Software Domain
Data modeling 101 - Basics - Software DomainData modeling 101 - Basics - Software Domain
Data modeling 101 - Basics - Software DomainAbdul Ahad
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptxVinzoCenzo
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLionel Briand
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...Bert Jan Schrijver
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsJean Silva
 
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingOpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingShane Coughlan
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonApplitools
 
Copilot para Microsoft 365 y Power Platform Copilot
Copilot para Microsoft 365 y Power Platform CopilotCopilot para Microsoft 365 y Power Platform Copilot
Copilot para Microsoft 365 y Power Platform CopilotEdgard Alejos
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfmaor17
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxRTS corp
 

Último (20)

Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryError
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh IT
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4jGraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
 
What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 Updates
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogue
 
Data modeling 101 - Basics - Software Domain
Data modeling 101 - Basics - Software DomainData modeling 101 - Basics - Software Domain
Data modeling 101 - Basics - Software Domain
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptx
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and Repair
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero results
 
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingOpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
 
Copilot para Microsoft 365 y Power Platform Copilot
Copilot para Microsoft 365 y Power Platform CopilotCopilot para Microsoft 365 y Power Platform Copilot
Copilot para Microsoft 365 y Power Platform Copilot
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdf
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
 

The Java Chamber of Horrors

  • 1. The Java Chamber of Horrors Andreas Zitzelsberger @andreasz82 QAware
  • 3. if (true == lUsingHttps) { // ... }
  • 4. if (true == lUsingHttps) { //.. if (lUsingSSLContext) { /* ... */ } //.. if (true == lUsingHttps) { if (true == lUsingSSLContext) { /* ... */ } } // ... } Better double check! Interleave control flows
  • 5. „true".equals(request.getParameter("statsRequired")) ? true: false Make the result extra-boolean If only there was a parseBoolean method
  • 6. if (dateCache.after(dateUser) || dateCache.equals(dateUser)) { return false; } else { return true; }
  • 7. if (dateCache.after(dateUser) || dateCache.equals(dateUser)) { return true; } else { return false; }
  • 9. if (clazz != null) { header.concat(LayoutConst.CLASS_NAME_STR); header.concat(clazz.getName()); header.concat(LayoutConst.NEW_LINE); } Early implementation of github.com/kelseyhightower/noco de
  • 10. public final static String NAME = new String("PIE DIAGRAM"); String surplus = new String(" "); new String(Long.toString(rightPoint)); new String("col" + String.valueOf(z + 2)); Me want objects! GC
  • 11. Objects.equals( query.getHost(), hostName) Which of these are equal? • Localhost • LoCaLhOsT • localhost.localdomain • 127.0.0.1 • 127.0.0.2 • 192.168.178.5 • my-computer • my-computer.my-domain
  • 13. } catch (Exception ex) { throw ex; }
  • 14. } catch (Exception e) { // improbable exceptions, throw a runtime exception // to avoid the declaration in the method-signature throw new RuntimeException( "... failed", e); }
  • 15. } catch (Exception ex) { // this exception will be ignored // because it cannot be sensibly handled ; } Why is there a semicolon?
  • 16. } catch (SQLException sqlException) { ; // to be compliant with rule NoEmptyCatchBlocks } Ah, that’s why!
  • 17. try { Thread.sleep(retryWaitTime); } catch (InterruptedException e) { ; // nothing to do } Unless you want your thread to be able to shut down …
  • 21. HttpServletResponseWrapper { /* ... */ public void setHeader(String arg0, String arg1) { // in this case do nothing here, // because we are setting it in the filter } public void setContentType(String arg0) { // in this case do nothing here, // because we are setting it in the filter } Breaks contract of HttpServletResponse
  • 22. public class PasswordEncryptor { private static final byte[] ALGO = new byte[]{68, 69, 83}; public static String encrypt(String str) throws BadPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException { // ... Cipher cipher = Cipher.getInstance(new String(ALGO)); cipher.init(1, new SecretKeySpec(key, new String(ALGO))); // ... } } D E S
  • 24. public class ServiceInvocationHandler implements InvocationHandler { private final BusinessService underlyingBusinessService; private final BusinessService derivedFromBusinessService; public Object invoke(/* ... */ ) throws Throwable { /* ... */ } public static Object getService(/* ... */) { /* ... */ } } Let’s call these delegate and caller for the sake of clarity…
  • 25. public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { try { if (caller == null) delegate.setSession(HibernateUtil.getSessionFactory() .getCurrentSession()); else { Assert.isNotNull(caller.getSession()); delegate.setSession(caller.getSession()); } delegate.beginTransaction(); final Object ret = method.invoke(delegate, args); // continued ... No Caller → New Hibernate session Otherwise, use the caller’s session Wait, what if there already is an open transaction?
  • 26. if (caller == null) delegate.commit(); if (delegate instanceof ServiceWithAssociatedThread && ((ServiceWithAssociatedThread) delegate) .hasAssociatedThread()) { ((ServiceWithAssociatedThread) delegate) .startAssociateThread(); ((ServiceWithAssociatedThread) delegate) .removeAssociateThread(); } return ret; } catch (final Throwable throwable) { // continued … } Unfinished transaction if there is a caller What could go wrong?
  • 27. } catch (final Throwable throwable) { Logger.error(ServiceInvocationHandler.class, „Error ...", throwable); if (caller == null && delegate.hasTransaction()) delegate.rollback(); else if (caller != null && caller.hasTransaction()) caller.rollback(); throw throwable.getCause(); } finally { if (delegate.hasSession()) { //if (caller != null) // delegate.close(); delegate.setSession(null); } Wait, what? Wait for the GC to clean up after us Modify stack trace, possible NullPointerException
  • 28. Integration Business What (probably) has happened Presentation Service Service Service Transaction Facade
  • 29. The setting: A massive custom framework for web apps. Think JEE built in-house
  • 30. protected void reset() { String[] classesForReset = { „com.acmecorp...legitimaton...XService", „com.acmecorp...legitimaton...YService", "com.acmecorp...legitimaton...ZService", // ... }; // continued ... Boss music starts…
  • 31. for (int i = 0; i < classesForReset.length; i++) { try { Class class = class.forName(classesForReset[i]); Method method = class.getDeclaredMethod( „reset", new Class[0]); method.invoke(null, null); } catch Exception e) { System.err.println(/* ... */); e.printStackTrace(System.err); } } Call a static reset method on each class for each request
  • 32. How bad can it be? public static void reset() { ldapFilters = new HashMap(); } Filters for per- user LDAP queries public static void reset() { sslSocketFactory = null; } New connections for each request
  • 33.
  • 34. What (probably) has happened • Stored session state in static variables (maybe a former C programmer?) • We need a reset() method, but it has to be static • Can’t use an interface for static methods • Call them by reflection • Forget about this code and have it run in production... • ...for over 10 years • ...in almost 300 applications
  • 35. What language do you want to have bad code in?
  • 38. private static void copyHashMap(HashMap<String,String> source, HashMap<String,String> destination) { if ((source != null) && (destination != null)) { if (!destination.isEmpty()) { destination.clear(); } for(HashMap.Entry<String, String> entry: source.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); destination.put(new String(key), new String(value)); } } }
  • 39. return super.isIdentical(msg) && germanMsg != null ? germanMsg.getMsg().equals(msg.getMsg(Locale.GERMAN)): true && defaultMsg != null ? defaultMsg.getMsg().equals(msg.getMsg(DEFAULT_LOCALE)): true;
  • 40. public AcmeCorpInteger(int intValue) { this(); try { setValue(new BigInteger("" + intValue)); } catch (AcmeCorpDatatypeConditionError ex) { } }
  • 41. Integer enterpriseNo = new Integer( enterpriseDTO.getEnterpriseNo().getValue().intValue());
  • 43. public URLConnection(String[] urls, int maxRetries, long waitMs) { super(getUrlFromString(urls[0])); if ((null == urls) || (urls.length <= 0)) { throw new RuntimeException("empty urls"); } // ... } //NOBUG //Only to satisfy constructor