All Questions
Tagged with design-by-contract java
45 questions
0
votes
0
answers
42
views
How should I document a bean that's only supposed to be managed by CDI?
Here is my class:
@Stateless
@Transactional
public class PostService {
@Inject private PostRepository postRepo;
@Inject private UserRepository userRepo;
@Inject private SectionRepository ...
1
vote
1
answer
863
views
Strengthening and Weakening of pre and post conditions
I have been reading into the topic of Design By Contract and so far have the following notes written:
When strengthening a condition, it means to make it
more restrictive and weakening a condition is ...
0
votes
2
answers
141
views
Design Dilemma - Context or Contract? (Java/Kotlin)
I have an Activity, a Presenter, and a Contract which both the Activity and the Presenter implement. The Activity will have some unique UI stuff, which must be called from within the Presenter. The ...
0
votes
2
answers
117
views
Returning Null vs Exception vs Contract
What would be considered an acceptable way of dealing with returning a record from a DB with the following 3 potential outcomes:
Db connection works, finds a user and returns a populated user object
...
0
votes
1
answer
316
views
Java Cofoja not working in eclipse
I tried to setup Cofoja in my eclipse project as described here.
When I try to run my project with Cofoja I always get an IllegalArgumentException.
[com.google.java.contract:agent FATAL ERROR while ...
2
votes
1
answer
126
views
How to configure Eclipse (Java) to recognize custom assertion when checking against potential null pointer
I have the following piece of Java code:
public void silence(final Object key) {
final Chain chain = (Chain)getChain(key);
checkPrecondition(chain != null);
chain.silence();
}
The ...
2
votes
1
answer
2k
views
Making sense of non-null and nullability annotations
I am not clear about how we properly use the annotation about nullable i.e. NonNull and Nullable.
I am not really comfortable with something like the following:
public void foo(@NonNull ArrayList&...
1
vote
1
answer
179
views
Design-by-contract finding pre-conditions
I have to create a Calculator in Java, based on this Interface.
public interface CalculatorIF {
int add(int x, int y);
int sub(int x, int y);
int mult(int x, int y);
//double div(int ...
0
votes
1
answer
215
views
Iterate through a matrix with openJML
I have a class with a matrix initialized with all 0 and 1 in a specific position:
public class MatrixTest {
/*@ spec_public @*/ int[][] griglia;
//@requires true;
//@ensures griglia[2][3]...
1
vote
1
answer
72
views
XML inside an string element vs independent elements
What are the conceptual and technical disadvantages of this request/response structure:
A)
<xs:element name="OrderRequest">
<xs:complexType>
<xs:sequence>
...
2
votes
2
answers
2k
views
How can a unit test "test the contract" on a method that returns void?
Java 8 here but this is a general unit testing question that (is likely) language-agnostic.
The syntax of writing a JUnit test is easy, but deciding on what tests to write and how to test main/...
1
vote
1
answer
137
views
How to use offline bytecode writer with Cofoja?
I'm trying to do offline instrumentation of my code using Cofoja (contracts for Java). I cannot seem to get the contracts in the compiled class file using the offline bytecode writer (this feature is ...
3
votes
1
answer
1k
views
Using cofoja annotations in IntelliJ
I'm fairly new to IntelliJ. When attempting to use cofoja code annotations I receive the following errors when building.
Error:java: error in contract: package com.google.java.contract.core.agent ...
2
votes
3
answers
2k
views
Java: Design by Contract annotations
I've recently started to read up on the Design by Contract design method but I don't understand some aspects of it. When using @pre etc, in a javadoc style comment, what purpose do these tags serve ...
3
votes
1
answer
3k
views
Hibernate Validator annotations not working
I just added hibernate-validator-5.1.3.Final.jar to my classpath and annotated a POJO:
public class Person {
@NotNull
private String name;
// etc...
}
Then I went to test it out with a ...
2
votes
2
answers
1k
views
Is a return of 'false' a valid postcondition of a method?
I've been learning about postconditions, preconditions, and design by contract recently and I haven't been able to find an exact answer to this question.
To me, a postcondition seems to essentially ...
1
vote
1
answer
484
views
How to enforce custom class instance invariants in Java
What is the best way in Java to enforce class instance invariants (i.e. to ensure that certain statements are true right before and after calling any public method)?
I will include below an example (...
0
votes
1
answer
395
views
Does LSP also make sense for dynamic typed language like Ruby? [closed]
Consider the classic example in Java
// Violation of Likov's Substitution Principle
class Rectangle
{
protected int m_width;
protected int m_height;
public void setWidth(int width){
...
2
votes
3
answers
1k
views
not able to configure cofoja on eclipse
Eclipse is up to date, cofoja too, and so are java jre/jdk.
ASM should be included in cofoja, but I downloaded the 3.3 just in case.
I followed all the procedures I could find in internet, but the ...
2
votes
1
answer
598
views
How to combine Java assert and (JUnit) Tests for public postconditions?
I read the Oracle recommendations concerning Java's assert and it says that you should use assert for public postconditions, too
(http://docs.oracle.com/javase/1.4.2/docs/guide/lang/assert.html#...
5
votes
3
answers
2k
views
Design by contract and assert statements
I am interested in the Design by Contract approach. It seems that for preconditions checked exceptions must be used to enforce them.
But for post-conditions and class-invariants I think that ...
7
votes
5
answers
16k
views
How do I insert a precondition in a java class method or constructor?
This is for a java class I'm taking. The book mentions preconditions and postconditions but doesn't give any examples how to code them. It goes on to talk about asserts, I have that down, but the ...
5
votes
1
answer
2k
views
Usage of javaagent with Android
I've recently became interested in DBC methodology. As Java is my "native" language I've looked at cofoja and more recently java-on-contracts. I mainly create Android software, so naturally I'd like ...
-1
votes
1
answer
974
views
When JSR-308 will become part of JDK? [closed]
See my blog post for excerpt from Java Posse #386 - Newscast for May 31st 2012 that expands this topic.
Does JSR 308 promotes design-by-contract to Java? It is step further from assertions? When it ...
4
votes
2
answers
303
views
What kinds of pre-conditions are sensible in Design by Contract?
Let's assume we have a class Student with the following constructor:
/** Initializes a student instance.
* @param matrNr matriculation number (allowed range: 10000 to 99999)
* @param firstName ...
5
votes
5
answers
134
views
Should my classes restrict developers from doing wrong things with them?
I am trying to understand where good contracts end and paranoia starts.
Really, I just have no idea what good developer should care about and what shall he leave out :)
Let's say I have a class that ...
0
votes
1
answer
712
views
Using Cofoja with Wicket (or even with just Maven)
I am trying my darnedest to get Google Cofoja to run in my Apache Wicket application which uses Maven2 as seems to be standard.
The project was initially generated using Leg Up with the Archetype "...
1
vote
1
answer
307
views
How to scan Annotations used in contracts written using COFOJA?
I am working on a project, where I have to write a contract for a method using COFOJA , and I have to generate code for method from the contracts using heuristics.
1) How will I be able to scan ...
9
votes
2
answers
3k
views
When to use assert in client & common GWT code
There are several questions on StackOverflow discussing the question of when one should use an assert statement versus throwing some exception. (Examples here, here, here, here, and here.
However, I ...
4
votes
3
answers
9k
views
When to add a precondition and when to (only) throw an exception?
I am learning about preconditions and when to use them. I have been told
that the precondition
@pre fileName must be the name of a valid file
does not suit in the following code:
/**
Creates a new ...
1
vote
3
answers
611
views
partial design by contract with assertions
I would like to get some opinions on an idea for a partial implementation of design by contract.
The goal is to add to the languages that don't offer it a light version of contracts (invariants and ...
2
votes
1
answer
255
views
eXtreme Design-by-Contract with Java, other than XINS?
Are there any technical Design-by-Contract solutions for Java projects similar to XINS? I'm looking for projects/frameworks that enforce developers to first author a contract for their application and ...
4
votes
4
answers
2k
views
How can I write a contract for an abstract method?
I am using contracts in my Java project. (Contract = doing checks at the start and end of methods)
I am wondering if there is a nice way/pattern to write a contract for a generic method. For example:
...
25
votes
12
answers
3k
views
Is Java assert broken?
While poking around the questions, I recently discovered the assert keyword in Java. At first, I was excited. Something useful I didn't already know! A more efficient way for me to check the ...
4
votes
4
answers
451
views
Do you have any tips to effectively use Java Assert?
I don't see much of the developer using Java Assert, but I am very keen in using them. Could you share some tips to effectively use them?
9
votes
2
answers
640
views
Why isn't JML implemented as Annotations in Java?
Contrary to Code Contracts in C#, in JML Code Contracts are just text that's used in the form of comments in the header of a method. Wouldn't it be better to have them exposed as Annotations, then? ...
14
votes
4
answers
7k
views
Java: clean way to automatically throw UnsupportedOperationException when calling hashCode() and equals()?
We've got an OO codebase where in quite a lot of cases hashcode() and equals() simply don't work, mostly for the following reason:
There is no way to extend an
instantiable class and add a value
...
4
votes
3
answers
4k
views
Design by contract and class invariant
I'm reading about dbc (http://en.wikipedia.org/wiki/Design_by_contract)
Can someone please give me a simple example of using class invariants in relation to inheritance?
10
votes
8
answers
25k
views
How can I place validating constraints on my method input parameters?
Here is the typical way of accomplishing this goal:
public void myContractualMethod(final String x, final Set<String> y) {
if ((x == null) || (x.isEmpty())) {
throw new ...
47
votes
10
answers
22k
views
A good Design-by-Contract library for Java? [closed]
A few years ago, I did a survey of DbC packages for Java, and I wasn't wholly satisfied with any of them. Unfortunately I didn't keep good notes on my findings, and I assume things have changed. Would ...
1
vote
3
answers
678
views
Argument checking or Design-by-Contract in java (GWT). Where to start?
I am playing GWT. I am looking for basic argument checking. I do not require invariants or result ensures.
What I am interested about it best practises on the topic.
For example, in c# I use one of ...
1
vote
3
answers
595
views
Design by Contract library (interface) thoughts?
I am looking at design by contract for a Java library, this what I came up with so far in terms of the interface.
The user could call executeContract and executeContract invokes invokeContract after ...
55
votes
11
answers
39k
views
How to show if a method may return null
After posting this question and reading that one I realized that it is very important to know if a method is supposed to return null, or if this is considered an error condition and an exceptions ...
68
votes
18
answers
13k
views
How much null checking is enough?
What are some guidelines for when it is not necessary to check for a null?
A lot of the inherited code I've been working on as of late has null-checks ad nauseam. Null checks on trivial functions, ...
19
votes
8
answers
4k
views
Ruby and duck typing: design by contract impossible?
Method signature in Java:
public List<String> getFilesIn(List<File> directories)
similar one in ruby
def get_files_in(directories)
In the case of Java, the type system gives me ...