See St. Thomas Aquinas's Summa Theologiæ I q. 21 a. 1:
Article 1. Whether there is justice in God?
Objection 1. It seems that there is not justice in God. For justice is
divided against temperance. But temperance does not exist in God:
neither therefore does justice.
Objection 2. Further, he who does whatsoever he wills and pleases does
not work according to justice. But, as the Apostle says: "God worketh
all things according to the counsel of His will" (Ephesians 1:11).
Therefore justice cannot be attributed to Him.
Objection 3. Further, the act of justice is to pay what is due. But
God is no man's debtor. Therefore justice does not belong to God.
Objection 4. Further, whatever is in God, is His essence. But justice
cannot belong to this. For Boethius says (De Hebdom.): "Good regards
the essence; justice the act." Therefore justice does not belong to
God.
On the contrary, It is said (Psalm 10:8): "The Lord is just, and hath
loved justice."
I answer that, There are two kinds of justice. The one consists in
mutual giving and receiving, as in buying and selling, and other kinds
of intercourse and exchange. This the Philosopher (Ethic. v, 4) calls
commutative justice, that directs exchange and intercourse of
business. This does not belong to God, since, as the Apostle says:
"Who hath first given to Him, and recompense shall be made him?"
(Romans 11:35). The other consists in distribution, and is called
distributive justice; whereby a ruler or a steward gives to each what
his rank deserves. As then the proper order displayed in ruling a
family or any kind of multitude evinces justice of this kind in the
ruler, so the order of the universe, which is seen both in effects of
nature and in effects of will, shows forth the justice of God. Hence
Dionysius says (Div. Nom. viii, 4): "We must needs see that God is
truly just, in seeing how He gives to all existing things what is
proper to the condition of each; and preserves the nature of each in
the order and with the powers that properly belong to it."
Reply to Objection 1. Certain of the moral virtues are concerned with
the passions, as temperance with concupiscence, fortitude with fear
and daring, meekness with anger. Such virtues as these can only
metaphorically be attributed to God; since, as stated above (Question
20, Article 1), in God there are no passions; nor a sensitive
appetite, which is, as the Philosopher says (Ethic. iii, 10), the
subject of those virtues. On the other hand, certain moral virtues are
concerned with works of giving and expending; such as justice,
liberality, and magnificence; and these reside not in the sensitive
faculty, but in the will. Hence, there is nothing to prevent our
attributing these virtues to God; although not in civil matters, but
in such acts as are not unbecoming to Him. For, as the Philosopher
says (Ethic. x, 8), it would be absurd to praise God for His political
virtues.
Reply to Objection 2. Since good as perceived by intellect is the
object of the will, it is impossible for God to will anything but what
His wisdom approves. This is, as it were, His law of justice, in
accordance with which His will is right and just. Hence, what He does
according to His will He does justly: as we do justly what we do
according to law. But whereas law comes to us from some higher power,
God is a law unto Himself.
Reply to Objection 3. To each one is due what is his own. Now that
which is directed to a man is said to be his own. Thus the master owns
the servant, and not conversely, for that is free which is its own
cause. In the word debt, therefore, is implied a certain exigence or
necessity of the thing to which it is directed. Now a twofold order
has to be considered in things: the one, whereby one created thing is
directed to another, as the parts of the whole, accident to substance,
and all things whatsoever to their end; the other, whereby all created
things are ordered to God. Thus in the divine operations debt may be
regarded in two ways, as due either to God, or to creatures, and in
either way God pays what is due. It is due to God that there should be
fulfilled in creatures what His will and wisdom require, and what
manifests His goodness. In this respect, God's justice regards what
befits Him; inasmuch as He renders to Himself what is due to Himself.
It is also due to a created thing that it should possess what is
ordered to it; thus it is due to man to have hands, and that other
animals should serve him. Thus also God exercises justice, when He
gives to each thing what is due to it by its nature and condition.
This debt however is derived from the former; since what is due to
each thing is due to it as ordered to it according to the divine
wisdom. And although God in this way pays each thing its due, yet He
Himself is not the debtor, since He is not directed to other things,
but rather other things to Him. Justice, therefore, in God is
sometimes spoken of as the fitting accompaniment of His goodness;
sometimes as the reward of merit. Anselm touches on either view where
he says (Prosolog. 10): "When Thou dost punish the wicked, it is just,
since it agrees with their deserts; and when Thou dost spare the
wicked, it is also just; since it befits Thy goodness."
Reply to Objection 4. Although justice regards act, this does not
prevent its being the essence of God; since even that which is of the
essence of a thing may be the principle of action. But good does not
always regard act; since a thing is called good not merely with
respect to act, but also as regards perfection in its essence. For
this reason it is said (De Hebdom.) that the good is related to the
just, as the general to the special.
And St. Thomas Aquinas's Summa Theologiæ I q. 21 a. 3, especially Objection/Reply #2:
Article 3. Whether mercy can be attributed to God?
Objection 1. It seems that mercy cannot be attributed to God. For
mercy is a kind of sorrow, as Damascene says (De Fide Orth. ii, 14).
But there is no sorrow in God; and therefore there is no mercy in Him.
Objection 2. Further, mercy is a relaxation of justice. But God cannot
remit what appertains to His justice. For it is said (2 Timothy 2:13):
"If we believe not, He continueth faithful: He cannot deny Himself."
But He would deny Himself, as a gloss says, if He should deny His
words. Therefore mercy is not becoming to God.
On the contrary, it is said (Psalm 110:4): "He is a merciful and
gracious Lord."
I answer that, Mercy is especially to be attributed to God, as seen in
its effect, but not as an affection of passion. In proof of which it
must be considered that a person is said to be merciful [misericors],
as being, so to speak, sorrowful at heart [miserum cor]; being
affected with sorrow at the misery of another as though it were his
own. Hence it follows that he endeavors to dispel the misery of this
other, as if it were his; and this is the effect of mercy. To sorrow,
therefore, over the misery of others belongs not to God; but it does
most properly belong to Him to dispel that misery, whatever be the
defect we call by that name. Now defects are not removed, except by
the perfection of some kind of goodness; and the primary source of
goodness is God, as shown above (Question 6, Article 4). It must,
however, be considered that to bestow perfections appertains not only
to the divine goodness, but also to His justice, liberality, and
mercy; yet under different aspects. The communicating of perfections,
absolutely considered, appertains to goodness, as shown above (6, 1,
4); in so far as perfections are given to things in proportion, the
bestowal of them belongs to justice, as has been already said (1); in
so far as God does not bestow them for His own use, but only on
account of His goodness, it belongs to liberality; in so far as
perfections given to things by God expel defects, it belongs to mercy.
Reply to Objection 1. This argument is based on mercy, regarded as an
affection of passion.
Reply to Objection 2. God acts mercifully, not indeed by going against
His justice, but by doing something more than justice; thus a man who
pays another two hundred pieces of money, though owing him only one
hundred, does nothing against justice, but acts liberally or
mercifully. The case is the same with one who pardons an offence
committed against him, for in remitting it he may be said to bestow a
gift. Hence the Apostle calls remission a forgiving: "Forgive one
another, as Christ has forgiven you" (Ephesians 4:32). Hence it is
clear that mercy does not destroy justice, but in a sense is the
fulness thereof. And thus it is said: "Mercy exalteth itself above
judgment" (James 2:13).