Skip to content

Commit

Permalink
Removed skip-annotation-class-list file
Browse files Browse the repository at this point in the history
- the exclusions are hard-coded now as they were never overrided (and they
  could not be)

Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Sep 18, 2022
1 parent ec2685f commit 27ed3ff
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 120 deletions.
12 changes: 0 additions & 12 deletions appserver/common/annotation-framework/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,4 @@
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/java/org/glassfish/apf</directory>
<includes>
<include>skip-annotation-class-list</include>
</includes>
</resource>
</resources>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,87 +17,51 @@

package org.glassfish.apf.factory;

import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.IOException;

import org.glassfish.apf.Scanner;
import org.glassfish.apf.AnnotationProcessor;
import org.glassfish.apf.AnnotationHandler;
import org.glassfish.apf.impl.AnnotationProcessorImpl;
import org.glassfish.apf.impl.AnnotationUtils;

/**
* The Factory is responsible for initializing a ready to use AnnotationProcessor.
*
* @author Jerome Dochez
*/
public abstract class Factory {

private static Set<String> skipAnnotationClassList = null;
private static final String SKIP_ANNOTATION_CLASS_LIST_URL =
"skip-annotation-class-list";
private static final Set<String> skipAnnotationClassList = Set.of(
"jakarta.servlet.GenericServlet",
"jakarta.servlet.http.HttpServlet",
"org.glassfish.wasp.servlet.JspServlet",
"org.apache.catalina.servlets.DefaultServlet"
);

/** we do no Create new instances of Factory */
protected Factory() {
}


/**
* Return a empty AnnotationProcessor with no annotation handlers registered
*
* @return initialized AnnotationProcessor instance
*/
public static AnnotationProcessorImpl getDefaultAnnotationProcessor() {
return new AnnotationProcessorImpl();
}

// initialize the list of class files we should skip annotation processing
private synchronized static void initSkipAnnotationClassList() {
if (skipAnnotationClassList == null) {
skipAnnotationClassList = new HashSet<String>();
InputStream is = null;
BufferedReader bf = null;
try {
is = AnnotationProcessorImpl.class.getClassLoader().getResourceAsStream(SKIP_ANNOTATION_CLASS_LIST_URL);
if (is==null) {
AnnotationUtils.getLogger().log(Level.FINE, "no annotation skipping class list found");
return;
}
bf = new BufferedReader(new InputStreamReader(is));
String className;
while ( (className = bf.readLine()) != null ) {
skipAnnotationClassList.add(className.trim());
}
} catch (IOException ioe) {
AnnotationUtils.getLogger().log(Level.WARNING,
ioe.getMessage(), ioe);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ioe2) {
// ignore
}
}
if (bf != null) {
try {
bf.close();
} catch (IOException ioe2) {
// ignore
}
}
}
}
}

// check whether a certain class can skip annotation processing
public static boolean isSkipAnnotationProcessing(String cName) {
if (skipAnnotationClassList == null) {
initSkipAnnotationClassList();
/**
* Check whether a certain class can skip annotation processing
*
* @return true if the class should not be processed
*/
public static boolean isSkipAnnotationProcessing(Class<?> clazz) {
if (clazz.getPackage() == null) {
return false;
}
return skipAnnotationClassList.contains(cName);
if (clazz.getPackage().getName().startsWith("java.lang")) {
return true;
}
return skipAnnotationClassList.contains(clazz.getCanonicalName());
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,20 +17,19 @@

package org.glassfish.apf.impl;

import org.glassfish.apf.ComponentInfo;
import org.glassfish.apf.factory.Factory;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Objects;

import org.glassfish.apf.ComponentInfo;
import org.glassfish.apf.factory.Factory;


/**
Expand All @@ -38,73 +38,81 @@
* @author Shing Wai Chan
*/
public class ComponentDefinition implements ComponentInfo {
final private Class clazz;
final private List<Constructor> constructors = new ArrayList<Constructor>();
final private List<Class> classes = new ArrayList<Class>();
final private List<Field> fields = new ArrayList<Field>();
final private Map<MethodKey, Method> methodMap = new HashMap<MethodKey, Method>();

public ComponentDefinition(Class clazz) {
private final Class<?> clazz;
private final List<Constructor<?>> constructors = new ArrayList<>();
private final List<Class<?>> classes = new ArrayList<>();
private final List<Field> fields = new ArrayList<>();
private final Map<MethodKey, Method> methodMap = new HashMap<>();

public ComponentDefinition(Class<?> clazz) {
this.clazz = clazz;
constructClassList();
initializeConstructors();
initializeFields();
initializeMethods();
}


@Override
public Field[] getFields() {
return fields.toArray(new Field[fields.size()]);
}


@Override
public Method[] getMethods() {
return methodMap.values().toArray(new Method[methodMap.size()]);
}

public Constructor[] getConstructors() {

@Override
public Constructor<?>[] getConstructors() {
return constructors.toArray(new Constructor[constructors.size()]);
}


private void constructClassList() {
// check whether this class is in the skip list
if (!Factory.isSkipAnnotationProcessing(clazz.getName())) {
if (!Factory.isSkipAnnotationProcessing(clazz)) {
classes.add(clazz);
}
Class parent = clazz;
Class<?> parent = clazz;
while ((parent = parent.getSuperclass()) != null) {
if (parent.getPackage() == null ||
!parent.getPackage().getName().startsWith("java.lang")) {
// always check whether this class is in the class list
// for skipping annotation processing
if (!Factory.isSkipAnnotationProcessing(parent.getName())) {
classes.add(0, parent);
}
// always check whether this class is in the class list
// for skipping annotation processing
if (!Factory.isSkipAnnotationProcessing(parent)) {
classes.add(0, parent);
}
}
}


/**
* In P.148 of "The Java Langugage Specification 2/e",
* Constructors, static initializers, and instance initializers are not
* members and therefore not inherited.
*/
private void initializeConstructors() {
for (Class cl : classes) {
for (Constructor constr : cl.getConstructors()) {
for (Class<?> cl : classes) {
for (Constructor<?> constr : cl.getConstructors()) {
constructors.add(constr);
}
}
}


private void initializeFields() {
for (Class cl : classes) {
for (Class<?> cl : classes) {
for (Field f : cl.getDeclaredFields()) {
fields.add(f);
}
}
}


private void initializeMethods() {
for (Class cl : classes) {
for (Class<?> cl : classes) {
for (Method method : cl.getDeclaredMethods()) {
if (!method.isBridge()) {
methodMap.put(new MethodKey(method), method);
Expand All @@ -114,57 +122,53 @@ private void initializeMethods() {
}

private static class MethodKey {
private Method m = null;
private int hashCode;
private String className = null;
private Package classPackage = null;
private final Method m;
private final Package classPackage;
private final String className;
private final int hashCode;

private MethodKey(Method m) {
this.m = m;
hashCode = m.getName().hashCode();
// store className and classPackage as getters are native
className = m.getDeclaringClass().getName();
classPackage = m.getDeclaringClass().getPackage();
this.className = m.getDeclaringClass().getName();
this.classPackage = m.getDeclaringClass().getPackage();
this.hashCode = Objects.hash(m.getName(), className);
}

public int hashCode() {

@Override
public int hashCode() {
return hashCode;
}


/**
* This equals method is defined in terms of inheritance overriding.
* We depends on java compiler to rule out irrelvant cases here.
*
* @return true for overriding and false otherwise
*/
@Override
public boolean equals(Object o) {
if (!(o instanceof MethodKey)) {
return false;
}

MethodKey mk2 = (MethodKey)o;
MethodKey mk2 = (MethodKey) o;
Method m2 = mk2.m;
if (m.getName().equals(m2.getName()) && Arrays.equals(
m.getParameterTypes(), m2.getParameterTypes())) {
if (m.getName().equals(m2.getName()) && Arrays.equals(m.getParameterTypes(), m2.getParameterTypes())) {
int modifiers = m.getModifiers();
int modifiers2 = m2.getModifiers();
boolean isPackageProtected2 = !Modifier.isPublic(modifiers2) &&
!Modifier.isProtected(modifiers2) &&
!Modifier.isPrivate(modifiers2);
boolean isSamePackage =
(classPackage == null && mk2.classPackage == null) ||
(classPackage != null && mk2.classPackage != null &&
classPackage.getName().equals(
mk2.classPackage.getName()));
boolean isPackageProtected2 = !Modifier.isPublic(modifiers2) && !Modifier.isProtected(modifiers2)
&& !Modifier.isPrivate(modifiers2);
boolean isSamePackage = (classPackage == null && mk2.classPackage == null) || (classPackage != null
&& mk2.classPackage != null && classPackage.getName().equals(mk2.classPackage.getName()));
if (Modifier.isPrivate(modifiers)) {
// need exact match
return Modifier.isPrivate(modifiers2) && isSamePackage
&& className.equals(mk2.className);
} else { // public, protected, package protected
return Modifier.isPublic(modifiers2) ||
Modifier.isProtected(modifiers2) ||
isPackageProtected2 && isSamePackage;
return Modifier.isPrivate(modifiers2) && isSamePackage && className.equals(mk2.className);
}
return Modifier.isPublic(modifiers2) || Modifier.isProtected(modifiers2)
|| isPackageProtected2 && isSamePackage;
}

return false;
Expand Down
1 change: 0 additions & 1 deletion appserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,6 @@
<pattern>connectors/descriptors/src/main/resources/glassfish/lib/schemas/</pattern>
<pattern>admingui/war/src/main/webapp/3rd-party-license</pattern>
<pattern>packager/legal/</pattern>
<pattern>common/annotation-framework/src/main/java/org/glassfish/apf/skip-annotation-class-list</pattern>
<pattern>admingui/common/src/main/resources/applications/appEdit/</pattern>
<pattern>.layout</pattern>
<pattern>admingui/war/src/main/webapp/j_security_check</pattern>
Expand Down

0 comments on commit 27ed3ff

Please sign in to comment.