Folge dem Video um zu sehen, wie unsere Website als Web-App auf dem Startbildschirm installiert werden kann.
Anmerkung: Diese Funktion ist in einigen Browsern möglicherweise nicht verfügbar.
package de.tutorials.training;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface CustomAnnotation {
}
package de.tutorials.training;
import javax.swing.table.TableModel;
public class CustomAnnotationTest {
@CustomAnnotation
static Object /* TableModel */ bubu;
}
package de.tutorials.training.apt;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
import javax.swing.table.TableModel;
import javax.tools.Diagnostic.Kind;
import de.tutorials.training.CustomAnnotation;
@SupportedAnnotationTypes(value = { "de.tutorials.training.CustomAnnotation" })
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class CustomAnnotationProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Set<? extends Element> annotatedElements = roundEnv.getElementsAnnotatedWith(CustomAnnotation.class);
for (Element annotatedElement : annotatedElements) {
TypeMirror typeMirror = annotatedElement.asType();
if (!TableModel.class.getName().equals(typeMirror.toString())) {
processingEnv.getMessager().printMessage(Kind.ERROR, "CustomAnnotation is only allowed for fields of Type " + TableModel.class.getName(), annotatedElement);
}
}
return true;
}
}
C:\dev\workspaces\training\de.tutorials.training\src>javac -cp ../bin -processor de.tutorials.training.apt.CustomAnnotationProcessor de/tutorials/training/CustomAnnotationTest.java
de\tutorials\training\CustomAnnotationTest.java:7: CustomAnnotation is only allowed for fields of Type javax.swing.table.TableModel
static Object /* TableModel*/ bubu;
^
1 error
C:\dev\workspaces\training\de.tutorials.training\src>javac -cp ../bin -processor de.tutorials.training.apt.CustomAnnotationProcessor de/tutorials/training/CustomAnnotationTest.java
C:\dev\workspaces\training\de.tutorials.training\src>javac -J-agentlib:jdwp=transport=dt_socket,server=y,address=8001 -cp ../bin -processor de.tutorials.training.apt.CustomAnnotationProcessor de/tutorials/training/CustomAnnotationTest.java
Listening for transport dt_socket at address: 8001