These days, I would not cut it close, but 5 hours is almost certainly overkill.
As noted, you can stay in downtown Barcelona and take Metro/train/Aerobus/taxi
to airport in reasonable time.
If you have only carry-on bags, can check in online, or have airline status that
allows you to avoid the main economy class check-in line, that will reduce your
time spent in airline line. If your airline has kiosks for printing luggage tags and
you can self-tag and drop off, that saves time.
I don't know your itinerary, but if you are flying into BCN, it may be worth a quick
trip up to departures level after you exit immigration just to see what's there.
Star Alliance has Gold Track option for Star Alliance Gold members that allows
you to access a special security line. If BCN has e-passport lines and you have a
biometric passport, you may be able to avoid the majority of the immigration queue
(I don't know about this, and it changes often, so someone else can hopefully comment).
Finally, I would assume that any cruise ship-driven lines at BCN are going to mostly
occur the day after most cruises dock. Time of season and day of week would seem
to be major factors, and you don't specify either of those.