Olá, tenho mais de 100 classes cada uma com mais de 100 campos…
E gostaria de fazer algo assim:
Alterar todos os métodos dessas classes que retornam String, e fazer com que eles retornem a String com um trim()
É possível fazer isso?
Se não via reflection, tem algum outro jeito, ou vou ter que fazer manualmente pra todos os metodos?
não sei se compreendi direito, mas pq vc não usa o trim qnd faz a chamada do metodo? assim com certeza só seria dado o trim qnd fosse realmente necessario.
[quote=mateusviccari]Olá, tenho mais de 100 classes cada uma com mais de 100 campos…
E gostaria de fazer algo assim:
Alterar todos os métodos dessas classes que retornam String, e fazer com que eles retornem a String com um trim()
É possível fazer isso?
Se não via reflection, tem algum outro jeito, ou vou ter que fazer manualmente pra todos os metodos?[/quote]
você quis dizer reflection ou refactor?
Reflection é uma API que “enxerga” as suas classes à um nível mais baixo, fazendo com que vc consiga, por exemplo, por meio de um programa saber os métodos que uma determinada classe tem, seus atributos, tipos de retorno, etc…
AOP
Mas como foi dito, será que não dá para acertar isso na chamada dos métodos?
lembre-se também que se você adicionar o trim em todos os métodos que retornam String, a possibilidade de estourar um NPE é enorme…
Tem isso tambem, teria que testar se a String != null
Achei uma “amarra” que da pra resolver isso, porem so pra quem usa hibernate.
Voce cria essa classe aqui:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hibernateUtil;
/*
* Copyright (C) 2010 Glauco
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.type.StringType;
import org.hibernate.usertype.UserType;
/**
* Custom class for trimming strings on the way out from the database,
* Hibernate 3.5 version.
*
* @author Paul Newport
* @author Glauco Knihs
*/
public class TrimmedString implements UserType {
private static final StringType STRING = new StringType();
/**
* default constructor
*/
public TrimmedString() {
}
/**
* @see org.hibernate.usertype.UserType#sqlTypes()
*/
@Override
public int[] sqlTypes() {
return new int[] { Types.CHAR };
}
/**
* @see org.hibernate.usertype.UserType#returnedClass()
*/
@Override
public Class returnedClass() {
return String.class;
}
/**
* @see org.hibernate.usertype.UserType#equals(java.lang.Object, java.lang.Object)
*/
@Override
public boolean equals(Object x, Object y) {
return (x == y) || (x != null && y != null && (x.equals(y)));
}
/**
* @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
*/
@Override
public Object nullSafeGet(ResultSet inResultSet, String[] names, Object o)
throws SQLException {
String val = (String)
//Hibernate.STRING.nullSafeGet(inResultSet, names[0]);
STRING.nullSafeGet(inResultSet, names[0]);
if (val == null)
return val;
return val.trim();
}
/**
* @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
*/
@Override
public void nullSafeSet(PreparedStatement inPreparedStatement,
Object o,int i) throws SQLException {
String val = (String) o;
inPreparedStatement.setString(i, val);
}
/**
* @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
*/
@Override
public Object deepCopy(Object o) {
if (o == null) {
return null;
}
return ((String) o);
}
/**
* @see org.hibernate.usertype.UserType#isMutable()
*/
@Override
public boolean isMutable() {
return false;
}
/**
* @see org.hibernate.usertype.UserType#assemble(java.io.Serializable, java.lang.Object)
*/
@Override
public Object assemble(Serializable cached, Object owner) {
return cached;
}
/**
* @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
*/
@Override
public Serializable disassemble(Object value) {
return (Serializable) value;
}
/**
* @see org.hibernate.usertype.UserType#replace(java.lang.Object, java.lang.Object, java.lang.Object)
*/
@Override
public Object replace(Object original, Object target, Object owner) {
return original;
}
/**
* @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
*/
@Override
public int hashCode(Object x) {
return x.hashCode();
}
}
E depois voce cria essa anotação em cada campo String dos beans:
@Type(type=“hibernateUtil.TrimmedString”)
Porém isso ainda é meio que uma gambiarra, gostaria que tivesse um jeito mais facil, mas vi que reflection nao da pra alterar uma classe, somente ler e obter valores da classe/instancias.