Click here to Skip to main content
15,888,065 members
Home / Discussions / Java
   

Java

 
GeneralRe: string compare java problem Pin
TorstenH.8-Nov-11 19:07
TorstenH.8-Nov-11 19:07 
GeneralRe: string compare java problem Pin
anshul.zunke26-Nov-11 17:28
anshul.zunke26-Nov-11 17:28 
QuestionNurse Rostering using Clonal Selection Algorithm Pin
rohaya887-Nov-11 19:58
rohaya887-Nov-11 19:58 
JokeRe: Nurse Rostering using Clonal Selection Algorithm Pin
Gerben Jongerius7-Nov-11 20:30
Gerben Jongerius7-Nov-11 20:30 
AnswerHints Pin
TorstenH.7-Nov-11 21:43
TorstenH.7-Nov-11 21:43 
GeneralRe: Hints Pin
rohaya8810-Nov-11 3:21
rohaya8810-Nov-11 3:21 
AnswerRe: Nurse Rostering using Clonal Selection Algorithm Pin
Richard MacCutchan7-Nov-11 22:44
mveRichard MacCutchan7-Nov-11 22:44 
QuestionCompile-time Annotation Processing [Solved] Pin
Skippums7-Nov-11 16:35
Skippums7-Nov-11 16:35 
I am attempting to generate a compile-time error if a subclass does not define a specific static field with a type subclassed from a defined Class field in the annotation. For example:
// RequiredStaticField.java
import java.lang.annotation.*;
 
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
@Inherited
public @interface RequiredStaticField {
    public String name();
    public Class<?> type();
}

// MyClass.java
@RequiredStaticField(name="StaticField", type=Number.class)
public class MyClass {
    private static Integer StaticField;
}

The classes above should validate and compile. However, I am having trouble determining if "Integer" is a subtype of "Number" in my AbstractProcessor implementation...
// RequiredStaticFieldProcessor.java
import java.util.Set;
 
import javax.annotation.processing.*;
import javax.lang.model.element.*;
import javax.lang.model.util.Types;
 
@SupportedAnnotationTypes("NAMESPACE.RequiredStaticField");
public class RequiredStaticFieldProcessor extends AbstractProcessor {
    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment environment) {
        // Get all classes that have the annotation
        Set<? extends Element> elementsWithAnnotation =
            environment.getElementsAnnotatedWith(RequiredStaticField.class);
        for (Element elementWithAnnotation : elementsWithAnnotation) {
            // If the element kind is not ElementKind.CLASS, display warning and continue
            // If the class is abstract, continue
            boolean isFound = false;
            RequiredStaticMethod annotation = elementWithAnnotation
                .getAnnotation(RequiredStaticMethod.class);
            for (Element member : elementWithAnnotation.getEnclosedElements()) {
                // If not a field, continue
                // If the field has the wrong name, continue

                // THIS IS WHERE I NEED HELP!  The following does not compile.
                // In the following, I either need to get the Class<?>
                // of member.asType(), or I need to iterate through the superclasses
                // of member.asType(), or I need to convert annotation.type()
                // to a TypeMirror object. Any Ideas?
                isFound = Types.isSubType(member.asType(), annotation.type());
 
                if (isFound) {
                    break;
                } else {
                    // Display error about declaring with invalid type
                }
            }
            if (!isFound) {
                // Display error about missing field
            }
        }
        return true;
    }
}

I just can't figure out how to write a test to determine if a TypeMirror object is a subclass of a Class object. Thanks for any help,
Sounds like somebody's got a case of the Mondays

-Jeff


modified 15-Nov-11 20:33pm.

AnswerRe: Compile-time Annotation Processing Pin
Skippums15-Nov-11 14:32
Skippums15-Nov-11 14:32 
QuestionTransfer File between client and server with TCP Socket Pin
lolostar7-Nov-11 5:39
lolostar7-Nov-11 5:39 
AnswerRe: Transfer File between client and server with TCP Socket Pin
Luc Pattyn7-Nov-11 5:58
sitebuilderLuc Pattyn7-Nov-11 5:58 
GeneralRe: Transfer File between client and server with TCP Socket Pin
David Skelly7-Nov-11 6:17
David Skelly7-Nov-11 6:17 
GeneralRe: Transfer File between client and server with TCP Socket Pin
Nagy Vilmos7-Nov-11 7:43
professionalNagy Vilmos7-Nov-11 7:43 
GeneralRe: Transfer File between client and server with TCP Socket Pin
lolostar7-Nov-11 6:26
lolostar7-Nov-11 6:26 
GeneralRe: Transfer File between client and server with TCP Socket Pin
Luc Pattyn7-Nov-11 6:38
sitebuilderLuc Pattyn7-Nov-11 6:38 
GeneralRe: Transfer File between client and server with TCP Socket Pin
Richard MacCutchan7-Nov-11 7:46
mveRichard MacCutchan7-Nov-11 7:46 
GeneralRe: Transfer File between client and server with TCP Socket Pin
Nagy Vilmos7-Nov-11 7:50
professionalNagy Vilmos7-Nov-11 7:50 
AnswerRe: Transfer File between client and server with TCP Socket Pin
Nagy Vilmos7-Nov-11 7:49
professionalNagy Vilmos7-Nov-11 7:49 
GeneralRe: Transfer File between client and server with TCP Socket Pin
lolostar7-Nov-11 8:28
lolostar7-Nov-11 8:28 
AnswerRe: Transfer File between client and server with TCP Socket Pin
David Skelly7-Nov-11 22:15
David Skelly7-Nov-11 22:15 
AnswerRe: Transfer File between client and server with TCP Socket Pin
anshul.zunke19-Nov-11 3:30
anshul.zunke19-Nov-11 3:30 
QuestionLicensing Java Application?!? Pin
Firo Atrum Ventus6-Nov-11 16:09
Firo Atrum Ventus6-Nov-11 16:09 
AnswerRe: Licensing Java Application?!? Pin
TorstenH.6-Nov-11 19:31
TorstenH.6-Nov-11 19:31 
GeneralRe: Licensing Java Application?!? Pin
Firo Atrum Ventus6-Nov-11 19:43
Firo Atrum Ventus6-Nov-11 19:43 
AnswerRe: Licensing Java Application?!? Pin
Bernhard Hiller6-Nov-11 22:46
Bernhard Hiller6-Nov-11 22:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.