Skip to content

Instantly share code, notes, and snippets.

@macintoxic
Created November 25, 2011 14:48
Show Gist options
  • Save macintoxic/1393684 to your computer and use it in GitHub Desktop.
Save macintoxic/1393684 to your computer and use it in GitHub Desktop.
Classe main
public class Calculadora {
public int soma(int a, int b)
{
return a+b;
}
public int dobro(int a)
{
return a * 2;
}
}
import java.io.File;
import java.io.Writer;
import java.lang.reflect.*;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.concurrent.FutureTask;
public class Persistencia {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ClasseTeste ct =new ClasseTeste();
int orig = 42;
Persistencia p = new Persistencia();
int y = p.go(orig);
CartaoWeb cw = new DiaDosNamorados("mané ");
System.out.println("-------------------");
System.out.println(orig + " " + y);
System.out.println("-------------------");
System.out.println(Exercicio1(ct));
System.out.println("-------------------");
System.out.println(Exercicio2(ct));
System.out.println("-------------------");
System.out.println(Exercicio3(ct));
System.out.println("-------------------");
System.out.println(Exercicio4(ct));
System.out.println("-------------------");
System.out.println(Exercicio5(ct));
System.out.println("-------------------");
System.out.println(Exercicio9(ct));
System.out.println("-------------------");
System.out.println(Exercicio10(ct));
System.out.println("-------------------");
System.out.println(Exercicio11(ct, ct, ct));
System.out.println("-------------------");
System.out.println(Exercicio12(ct));
System.out.println("-------------------");
try {
Exercicio13(new Calculadora(), 1,2,3,4);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("-------------------");
}
private static void TestaPositivo(Object o)
{
for (Field field : o.getClass().getDeclaredFields())
{
if(field.getType().toString().toUpperCase() == "INT" || field.getType().toString().toUpperCase() == "INTEGER" )
{
try {
Exercicio6(field, o);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private static String Exercicio2(Object o) {
String retorno = "";
for (Field field : o.getClass().getDeclaredFields()) {
retorno += " " + field.getName() + " " + Modifier.toString(field.getModifiers()) + " - ";
}
for(Method method : o.getClass().getDeclaredMethods())
{
retorno += " " + method.getName() + " " + Modifier.toString(method.getModifiers()) + " - ";
}
return retorno;
}
private static String Exercicio1(Object o)
{
return "drop table " + o.getClass().getSimpleName();
}
private static String Exercicio3(Object o)
{
String retorno = "";
for (Class cl : o.getClass().getInterfaces() ) {
retorno += " " + cl.getSimpleName() + " ";
}
return retorno;
}
private static String Exercicio4(Object o)
{
String retorno = "";
for(Method method : o.getClass().getDeclaredMethods())
{
//esses enunciados do caraio...
//vai saber se ele quer os metodos que sao protected E static juntos ou protected E static separados.
if(Modifier.isProtected(method.getModifiers()) && Modifier.isStatic(method.getModifiers()))
retorno += " " + method.getName() + " " +
Modifier.toString(method.getModifiers()) + " - ";
}
return retorno;
}
private static String Exercicio5(Object o)
{
String retorno = "";
for(Method method : o.getClass().getDeclaredMethods())
{
//esses enunciados do caraio...
//vai saber se ele quer os metodos que sao protected E static juntos ou protected E static separados.
if(Modifier.isProtected(method.getModifiers()) && Modifier.isPublic(method.getModifiers()))
retorno += " " + method.getName() + " " +
Modifier.toString(method.getModifiers()) + " - ";
}
if (o.getClass().isInstance(AbstractList.class)) {
for(Field field : o.getClass().getDeclaredFields())
{
}
}
return retorno;
}
private static String Exercicio9(Object o)
{
if(o.getClass().getDeclaredFields().length >0 )
return o.getClass().getDeclaredFields()[0].getType().toString();
else return "";
}
private static String Exercicio10(Object o)
{
String retorno = "select ";
for(Field field: o.getClass().getFields())
{
retorno += field.getName() + ", ";
}
retorno = retorno.substring(0, retorno.length() - 2);
retorno += " from " + o.getClass().getSimpleName();
retorno += " where " + o.getClass().getFields()[0].getName() + "=?";
return retorno;
}
private static String Exercicio11(Object... o)
{
String retorno = "select ";
String fields = "";
String where = "";
String tabelas = "";
String field0 = o[0].getClass().getFields()[0].getName();
for(Object obj : o)
{
tabelas += obj.getClass().getSimpleName() + ", ";
where += field0 + " = " + obj.getClass().getFields()[0].getName() + " and ";
for (Field field : obj.getClass().getFields()) {
fields += field.getName() + ", ";
}
}
where = where .substring(0, where.length() - 4);
tabelas = tabelas.substring(0, tabelas.length() - 2);
fields = fields.substring(0, fields.length() - 2);
System.out.println("tabelas: " + tabelas);
System.out.println("fields: " + fields);
System.out.println("where : " + where);
return retorno + fields + " from " + tabelas + " where " + where;
}
private static String Exercicio12(Object o)
{
String retorno = "";
System.out.println("**--*-*-*-*-*-*-*-****");
for(Field field: o.getClass().getDeclaredFields())
{
if(Modifier.isPrivate( field.getModifiers()) ||
Modifier.isProtected( field.getModifiers()))
{
retorno += field.getName() +", ";
}
}
return retorno;
}
private static void Exercicio13(Calculadora c, int... inteiros) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
{
for (int i : inteiros) {
System.out.println(c.getClass().getMethod("dobro",int.class).invoke(c,i));
}
System.out.println(c.getClass().getMethod("soma",int.class, int.class).invoke(c,1,120));
}
private static Boolean Exercicio6(Field field, Object o) throws IllegalArgumentException, IllegalAccessException
{
if(field.getAnnotation( Positivo.class) != null)
{
field.setAccessible(true);
return field.getInt(o) > 0;
}
return null;
}
int go(int arg)
{
arg = arg * 2;
return arg;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment