Ver Mensaje Individual
  #3 (permalink)  
Antiguo 17/11/2012, 15:10
danirebollo
 
Fecha de Ingreso: noviembre-2012
Ubicación: Madrid
Mensajes: 6
Antigüedad: 11 años, 5 meses
Puntos: 0
Respuesta: Evaluar si .java de un directorio forman parte d el proyecto y acceder a m

Cita:
Iniciado por LuisChavezB Ver Mensaje
Puedes utilizar anotaciones:
Código Javascript:
Ver original
  1. /*
  2.  * Copyright 2012 Leviathan.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package mx.com.leviathan.data.filter;
  17.  
  18. import java.lang.annotation.ElementType;
  19. import java.lang.annotation.Retention;
  20. import java.lang.annotation.RetentionPolicy;
  21. import java.lang.annotation.Target;
  22.  
  23. /**
  24.  *
  25.  * @author Leviathan
  26.  */
  27. @Target(ElementType.TYPE)
  28. @Retention(RetentionPolicy.RUNTIME)
  29. public @interface Filter {
  30.  
  31.     String author();
  32.  
  33.     String name();
  34.  
  35.     String version();
  36. }

Código Javascript:
Ver original
  1. /*
  2.  * Copyright 2012 Leviathan.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package mx.com.leviathan.data.filter;
  17.  
  18. import java.lang.annotation.ElementType;
  19. import java.lang.annotation.Retention;
  20. import java.lang.annotation.RetentionPolicy;
  21. import java.lang.annotation.Target;
  22.  
  23. /**
  24.  *
  25.  * @author Leviathan
  26.  */
  27. @Target(ElementType.METHOD)
  28. @Retention(RetentionPolicy.RUNTIME)
  29. public @interface FilterFunction {
  30.  
  31.     String[] params();
  32.  
  33.     Class<?>[] paramTypes();
  34.  
  35.     Class<?> returnType();
  36. }

Aplicarlas:
Código Javascript:
Ver original
  1. @Filter(author = "Leviathan", version = "0.1", name = "Example")
  2. public class FilterExample {
  3.  
  4.     @FilterFunction(params = {"firstName", "lastName"}, paramTypes = {String.class, String.class}, returnType = String.class)
  5.     public String testString(String firstName, String lastName) {
  6.         return firstName + lastName;
  7.     }
  8. }

y por medio de reflexión trabajar con la información proporcionada en las anotaciones.
Uhh... XD no conocia eso de las anotaciones.
Miro a ver y te cuento...

Lo que he consegido, que me funciona parcialmente, es esto:
En main:
Código:
String str = "HELLO";
    Diodo dod;
    dod=new Diodo("","",0,"","");

    try {
 
    Method method = str.getClass().getMethod("toString"); //getMethod("toString", null);
    Object retVal = method.invoke(str); //invoke(str, null);
    System.out.println(retVal);
 
    } catch ...............(a lot of catchs xd)
Este codigo, sea con 'str.getClass()...' y 'method.invoke(str)' tratando de obtener el toString de un String, o con 'dod.getClass()...' y 'method.invoke(dod)' tratando de obtener uno de los metodos de la clase Diodo, funcionan bien, pero cuando trato de obtener un metodo de 'aClass', declarado como
Código:
Class aClass = null;
, e instanciado con:
Código:
aClass= classLoader.loadClass("componentespkg.cat2.Diodo");
, que deberia ser en este caso la clase Diodo, no funciona y me devuelve la excepcion
Código:
java.lang.NoSuchMethodException: java.lang.Class.toCB2CB()
at java.lang.Class.getMethod(Class.java:1622)
at inventarionb.InventarionbTEST.main(InventarionbTEST.java:83)
Hay una cosa, no se si tiene algo que ver con el error, cuando pone java.lang.Class.toCB2CB() en la excepcion. ¿Esa ruta es correcta? No creo que el metodo de mi clase derive del metodo Class, pero no sabria que poner... en el caso de que la clase se encuentre dentro de la clase principal, donde esta el main, no hay que poner mas cosas, pero lo mismo al estar fuera hay que poner algo en la ruta... no he conseguido que me funcione poniendo la sucesion de packages (separada por '.')